XML libraries support (#463)
* dom4j * added more parsers * StaxParser * Jaxb binding * Jaxb binding * Finish article
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
a661e0e3c9
commit
330c8680c9
@@ -8,9 +8,6 @@ import org.junit.Test;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class DefaultParserTest {
|
||||
|
||||
final String fileName = "src/test/resources/example.xml";
|
||||
@@ -29,7 +26,7 @@ public class DefaultParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeListByTitle() {
|
||||
public void getNodeListByTitleTest() {
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
NodeList list = parser.getNodeListByTitle("XML");
|
||||
|
||||
@@ -47,7 +44,7 @@ public class DefaultParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeById() {
|
||||
public void getNodeByIdTest() {
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
Node node = parser.getNodeById("03");
|
||||
|
||||
@@ -56,7 +53,7 @@ public class DefaultParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeListByDate(){
|
||||
public void getNodeListByDateTest(){
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
NodeList list = parser.getNodeListByTitle("04022016");
|
||||
for (int i = 0; null != list && i < list.getLength(); i++) {
|
||||
@@ -73,7 +70,7 @@ public class DefaultParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeListWithNamespace(){
|
||||
public void getNodeListWithNamespaceTest(){
|
||||
parser = new DefaultParser(new File(fileNameSpace));
|
||||
NodeList list = parser.getAllTutorials();
|
||||
assertNotNull(list);
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.baeldung.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.Node;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Dom4JParserTest {
|
||||
|
||||
final String fileName = "src/test/resources/example.xml";
|
||||
|
||||
Dom4JParser parser;
|
||||
|
||||
@Test
|
||||
public void getRootElementTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
Element root = parser.getRootElement();
|
||||
|
||||
assertNotNull(root);
|
||||
assertTrue(root.elements().size() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFirstElementListTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
List<Element> firstList = parser.getFirstElementList();
|
||||
|
||||
assertNotNull(firstList);
|
||||
assertTrue(firstList.size() == 4);
|
||||
assertTrue(firstList.get(0).attributeValue("type").equals("java"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getElementByIdTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
Node element = parser.getNodeById("03");
|
||||
|
||||
String type = element.valueOf("@type");
|
||||
assertEquals("android", type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getElementsListByTitleTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
Node element = parser.getElementsListByTitle("XML");
|
||||
|
||||
assertEquals("java", element.valueOf("@type"));
|
||||
assertEquals("02", element.valueOf("@tutId"));
|
||||
assertEquals("XML", element.selectSingleNode("title").getText());
|
||||
assertEquals("title", element.selectSingleNode("title").getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateModifiedDocumentTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
parser.generateModifiedDocument();
|
||||
|
||||
File generatedFile = new File("src/test/resources/example_updated.xml");
|
||||
assertTrue(generatedFile.exists());
|
||||
|
||||
parser.setFile(generatedFile);
|
||||
Node element = parser.getNodeById("02");
|
||||
|
||||
assertEquals("XML updated", element.selectSingleNode("title").getText());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateNewDocumentTest() {
|
||||
parser = new Dom4JParser(new File(fileName));
|
||||
parser.generateNewDocument();
|
||||
|
||||
File newFile = new File("src/test/resources/example_new.xml");
|
||||
assertTrue(newFile.exists());
|
||||
|
||||
parser.setFile(newFile);
|
||||
Node element = parser.getNodeById("01");
|
||||
|
||||
assertEquals("XML with Dom4J", element.selectSingleNode("title").getText());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.jdom2.Element;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JDomParserTest {
|
||||
|
||||
final String fileName = "src/test/resources/example.xml";
|
||||
|
||||
JDomParser parser;
|
||||
|
||||
@Test
|
||||
public void getFirstElementListTest() {
|
||||
parser = new JDomParser(new File(fileName));
|
||||
List<Element> firstList = parser.getAllTitles();
|
||||
|
||||
assertNotNull(firstList);
|
||||
assertTrue(firstList.size() == 4);
|
||||
assertTrue(firstList.get(0).getAttributeValue("type").equals("java"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getElementByIdTest() {
|
||||
parser = new JDomParser(new File(fileName));
|
||||
Element el = parser.getNodeById("03");
|
||||
|
||||
String type = el.getAttributeValue("type");
|
||||
assertEquals("android", type);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.dom4j.Node;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.xml.binding.Tutorials;
|
||||
|
||||
public class JaxbParserTest {
|
||||
|
||||
|
||||
final String fileName = "src/test/resources/example.xml";
|
||||
|
||||
JaxbParser parser;
|
||||
|
||||
@Test
|
||||
public void getFullDocumentTest(){
|
||||
parser = new JaxbParser(new File(fileName));
|
||||
Tutorials tutorials = parser.getFullDocument();
|
||||
|
||||
assertNotNull(tutorials);
|
||||
assertTrue(tutorials.getTutorial().size() == 4);
|
||||
assertTrue(tutorials.getTutorial().get(0).getType().equalsIgnoreCase("java"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createNewDocumentTest(){
|
||||
File newFile = new File("src/test/resources/example_new.xml");
|
||||
parser = new JaxbParser(newFile);
|
||||
parser.createNewDocument();
|
||||
|
||||
|
||||
assertTrue(newFile.exists());
|
||||
|
||||
Tutorials tutorials = parser.getFullDocument();
|
||||
|
||||
assertNotNull(tutorials);
|
||||
assertTrue(tutorials.getTutorial().size() == 1);
|
||||
assertTrue(tutorials.getTutorial().get(0).getTitle().equalsIgnoreCase("XML with Jaxb"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.xml;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JaxenDemoTest {
|
||||
|
||||
final String fileName = "src/test/resources/example.xml";
|
||||
|
||||
JaxenDemo jaxenDemo;
|
||||
|
||||
@Test
|
||||
public void getFirstLevelNodeListTest() {
|
||||
jaxenDemo = new JaxenDemo(new File(fileName));
|
||||
List<?> list = jaxenDemo.getAllTutorial();
|
||||
|
||||
assertNotNull(list);
|
||||
assertTrue(list.size() == 4);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.xml;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.xml.model.Tutorial;
|
||||
|
||||
public class StaxParserTest {
|
||||
|
||||
final String fileName = "src/test/resources/example.xml";
|
||||
|
||||
StaxParser parser;
|
||||
|
||||
@Test
|
||||
public void getAllTutorialsTest(){
|
||||
parser = new StaxParser(new File(fileName));
|
||||
List<Tutorial> tutorials = parser.getAllTutorial();
|
||||
|
||||
assertNotNull(tutorials);
|
||||
assertTrue(tutorials.size() == 4);
|
||||
assertTrue(tutorials.get(0).getType().equalsIgnoreCase("java"));
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,24 @@
|
||||
<?xml version="1.0"?>
|
||||
<Tutorials>
|
||||
<Tutorial tutId="01" type="java">
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="02" type="java">
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="03" type="android">
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="04" type="java">
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
@@ -28,5 +28,5 @@
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</Tutorial>
|
||||
</Tutorials>
|
||||
</tutorial>
|
||||
</tutorials>
|
||||
@@ -1,24 +1,24 @@
|
||||
<?xml version="1.0"?>
|
||||
<Tutorials xmlns="http://www.baeldung.com/full_archive">
|
||||
<Tutorial tutId="01" type="java">
|
||||
<tutorials xmlns="http://www.baeldung.com/full_archive">
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="02" type="java">
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="03" type="android">
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="04" type="java">
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
@@ -28,5 +28,5 @@
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</Tutorial>
|
||||
</Tutorials>
|
||||
</tutorial>
|
||||
</tutorials>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<XMLTutorials>
|
||||
<tutorial tutId="01" type="xml">
|
||||
<title>XML with Dom4J</title>
|
||||
<description>XML handling with Dom4J</description>
|
||||
<date>14/06/2016</date>
|
||||
<author>Dom4J tech writer</author>
|
||||
</tutorial>
|
||||
</XMLTutorials>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="java">
|
||||
<title>Guava updated</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="02" type="java">
|
||||
<title>XML updated</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="03" type="android">
|
||||
<title>Android updated</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</tutorial>
|
||||
<tutorial tutId="04" type="java">
|
||||
<title>Spring updated</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</tutorial>
|
||||
</tutorials>
|
||||
Reference in New Issue
Block a user