diff --git a/xml/pom.xml b/xml/pom.xml
index fc158901e6..9d88bd75eb 100644
--- a/xml/pom.xml
+++ b/xml/pom.xml
@@ -1,139 +1,117 @@
- 4.0.0
- com.baeldung
- xml
- 0.1-SNAPSHOT
+ 4.0.0
+ com.baeldung
+ xml
+ 0.1-SNAPSHOT
- xml
+ xml
-
-
+
+
+
+ dom4j
+ dom4j
+ 1.6.1
+
+
+ jaxen
+ jaxen
+ 1.1.6
+
-
- com.google.guava
- guava
- ${guava.version}
-
-
- commons-io
- commons-io
- 2.4
-
+
+ org.jdom
+ jdom2
+ 2.0.6
+
-
- org.apache.commons
- commons-collections4
- 4.0
-
+
+ xerces
+ xercesImpl
+ 2.9.1
+
-
- org.apache.commons
- commons-lang3
- ${commons-lang3.version}
-
-
-
-
+
+
+ commons-io
+ commons-io
+ 2.4
+
-
- junit
- junit
- ${junit.version}
- test
-
+
+ org.apache.commons
+ commons-collections4
+ 4.0
+
-
- org.hamcrest
- hamcrest-core
- ${org.hamcrest.version}
- test
-
-
- org.hamcrest
- hamcrest-library
- ${org.hamcrest.version}
- test
-
+
+ org.apache.commons
+ commons-lang3
+ ${commons-lang3.version}
+
-
- org.mockito
- mockito-core
- ${mockito.version}
- test
-
-
+
-
- xml
-
-
- src/main/resources
- true
-
-
+
+ junit
+ junit
+ ${junit.version}
+ test
+
-
+
-
- org.apache.maven.plugins
- maven-compiler-plugin
- ${maven-compiler-plugin.version}
-
-
- 1.8
-
-
+
+ xml
+
+
+ src/main/resources
+ true
+
+
-
- org.apache.maven.plugins
- maven-surefire-plugin
- ${maven-surefire-plugin.version}
-
+
-
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${maven-compiler-plugin.version}
+
+
+ 1.8
+
+
-
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ ${maven-surefire-plugin.version}
+
-
-
- 4.3.11.Final
- 5.1.38
+
-
- 2.7.2
+
-
- 1.7.13
- 1.1.3
+
-
- 5.1.3.Final
+
+ 19.0
+ 3.4
-
- 19.0
- 3.4
+
+ 4.12
-
- 1.3
- 4.12
- 1.10.19
+
+ 3.5.1
+ 2.6
+ 2.19.1
+ 2.7
+ 1.4.18
- 4.4.1
- 4.5
-
- 2.9.0
-
-
- 3.5.1
- 2.6
- 2.19.1
- 2.7
- 1.4.18
-
-
+
diff --git a/xml/src/main/java/com/baeldung/xml/DefaultParser.java b/xml/src/main/java/com/baeldung/xml/DefaultParser.java
index 89326f45c9..1f2a7418fb 100644
--- a/xml/src/main/java/com/baeldung/xml/DefaultParser.java
+++ b/xml/src/main/java/com/baeldung/xml/DefaultParser.java
@@ -39,7 +39,7 @@ public class DefaultParser {
XPath xPath = XPathFactory.newInstance().newXPath();
- String expression = "/Tutorials/Tutorial";
+ String expression = "/tutorials/tutorial";
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
@@ -60,7 +60,7 @@ public class DefaultParser {
XPath xPath = XPathFactory.newInstance().newXPath();
- String expression = "/Tutorials/Tutorial[@tutId=" + "'" + id + "'" + "]";
+ String expression = "/tutorials/tutorial[@tutId=" + "'" + id + "'" + "]";
node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
@@ -83,7 +83,7 @@ public class DefaultParser {
XPath xPath = XPathFactory.newInstance().newXPath();
- String expression = "//Tutorial[descendant::title[text()=" + "'" + name + "'" + "]]";
+ String expression = "//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]";
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
@@ -107,7 +107,7 @@ public class DefaultParser {
XPath xPath = XPathFactory.newInstance().newXPath();
- String expression = "//Tutorial[number(translate(date, '/', '')) > " + date + "]";
+ String expression = "//tutorial[number(translate(date, '/', '')) > " + date + "]";
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
@@ -151,7 +151,7 @@ public class DefaultParser {
}
});
- String expression = "/bdn:Tutorials/bdn:Tutorial";
+ String expression = "/bdn:tutorials/bdn:tutorial";
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
diff --git a/xml/src/main/java/com/baeldung/xml/Dom4JParser.java b/xml/src/main/java/com/baeldung/xml/Dom4JParser.java
new file mode 100644
index 0000000000..d9058ba236
--- /dev/null
+++ b/xml/src/main/java/com/baeldung/xml/Dom4JParser.java
@@ -0,0 +1,131 @@
+package com.baeldung.xml;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.dom4j.Node;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+
+public class Dom4JParser {
+
+ private File file;
+
+ public Dom4JParser(File file) {
+ this.file = file;
+ }
+
+ public Element getRootElement() {
+ try {
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(file);
+ return document.getRootElement();
+ } catch (DocumentException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public List getFirstElementList() {
+ try {
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(file);
+ return document.getRootElement().elements();
+ } catch (DocumentException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public Node getNodeById(String id) {
+ try {
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(file);
+ List elements = document.selectNodes("//*[@tutId='" + id + "']");
+ return elements.get(0);
+ } catch (DocumentException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public Node getElementsListByTitle(String name) {
+ try {
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(file);
+ List elements = document
+ .selectNodes("//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]");
+ return elements.get(0);
+ } catch (DocumentException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public void generateModifiedDocument() {
+ try {
+ SAXReader reader = new SAXReader();
+ Document document = reader.read(file);
+ List nodes = document.selectNodes("/tutorials/tutorial");
+ for (Node node : nodes) {
+ Element element = (Element) node;
+ Iterator iterator = element.elementIterator("title");
+ while (iterator.hasNext()) {
+ Element title = (Element) iterator.next();
+ title.setText(title.getText() + " updated");
+ }
+ }
+ XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_updated.xml")));
+ writer.write(document);
+ writer.close();
+ } catch (DocumentException e) {
+ e.printStackTrace();
+
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public void generateNewDocument() {
+ try {
+ Document document = DocumentHelper.createDocument();
+ Element root = document.addElement("XMLTutorials");
+ Element tutorialElement = root.addElement("tutorial").addAttribute("tutId", "01");
+ tutorialElement.addAttribute("type", "xml");
+
+ tutorialElement.addElement("title").addText("XML with Dom4J");
+
+ tutorialElement.addElement("description").addText("XML handling with Dom4J");
+
+ tutorialElement.addElement("date").addText("14/06/2016");
+
+ tutorialElement.addElement("author").addText("Dom4J tech writer");
+
+ OutputFormat format = OutputFormat.createPrettyPrint();
+ XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_new.xml")), format);
+ writer.write(document);
+ writer.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ public void setFile(File file) {
+ this.file = file;
+ }
+
+}
diff --git a/xml/src/main/java/com/baeldung/xml/JDomParser.java b/xml/src/main/java/com/baeldung/xml/JDomParser.java
new file mode 100644
index 0000000000..08676b44f8
--- /dev/null
+++ b/xml/src/main/java/com/baeldung/xml/JDomParser.java
@@ -0,0 +1,62 @@
+package com.baeldung.xml;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import org.jdom2.Document;
+import org.jdom2.Element;
+import org.jdom2.JDOMException;
+import org.jdom2.filter.Filters;
+import org.jdom2.input.SAXBuilder;
+import org.jdom2.xpath.XPathExpression;
+import org.jdom2.xpath.XPathFactory;
+
+
+public class JDomParser {
+
+ private File file;
+
+ public JDomParser(File file) {
+ this.file = file;
+ }
+
+ public List getAllTitles() {
+ try {
+ SAXBuilder builder = new SAXBuilder();
+ Document doc = builder.build(this.getFile());
+ Element tutorials = doc.getRootElement();
+ List titles = tutorials.getChildren("tutorial");
+ return titles;
+ } catch (JDOMException | IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public Element getNodeById(String id) {
+ try {
+ SAXBuilder builder = new SAXBuilder();
+ Document document = (Document) builder.build(file);
+ String filter = "//*[@tutId='" + id + "']";
+ XPathFactory xFactory = XPathFactory.instance();
+ XPathExpression expr = xFactory.compile(filter, Filters.element());
+ List node = expr.evaluate(document);
+
+ return node.get(0);
+ } catch (JDOMException | IOException e ) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ public void setFile(File file) {
+ this.file = file;
+ }
+
+}
diff --git a/xml/src/main/java/com/baeldung/xml/JaxbParser.java b/xml/src/main/java/com/baeldung/xml/JaxbParser.java
new file mode 100644
index 0000000000..9c06ec5fba
--- /dev/null
+++ b/xml/src/main/java/com/baeldung/xml/JaxbParser.java
@@ -0,0 +1,69 @@
+package com.baeldung.xml;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Date;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
+import com.baeldung.xml.binding.Tutorial;
+import com.baeldung.xml.binding.Tutorials;
+
+public class JaxbParser {
+
+ private File file;
+
+ public JaxbParser(File file) {
+ this.file = file;
+ }
+
+ public Tutorials getFullDocument() {
+ try {
+ JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
+ Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
+ Tutorials tutorials = (Tutorials) jaxbUnmarshaller.unmarshal(this.getFile());
+ return tutorials;
+ } catch (JAXBException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public void createNewDocument() {
+ Tutorials tutorials = new Tutorials();
+ tutorials.setTutorial(new ArrayList());
+ Tutorial tut = new Tutorial();
+ tut.setTutId("01");
+ tut.setType("XML");
+ tut.setTitle("XML with Jaxb");
+ tut.setDescription("XML Binding with Jaxb");
+ tut.setDate(new Date());
+ tut.setAuthor("Jaxb author");
+ tutorials.getTutorial().add(tut);
+
+ try {
+ JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
+ Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
+
+ jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+
+ jaxbMarshaller.marshal(tutorials, file);
+
+ } catch (JAXBException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ public void setFile(File file) {
+ this.file = file;
+ }
+
+}
diff --git a/xml/src/main/java/com/baeldung/xml/JaxenDemo.java b/xml/src/main/java/com/baeldung/xml/JaxenDemo.java
new file mode 100644
index 0000000000..0c2dca0573
--- /dev/null
+++ b/xml/src/main/java/com/baeldung/xml/JaxenDemo.java
@@ -0,0 +1,57 @@
+package com.baeldung.xml;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.jaxen.JaxenException;
+import org.jaxen.XPath;
+import org.jaxen.dom.DOMXPath;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+public class JaxenDemo {
+
+ private File file;
+
+ public JaxenDemo(File file) {
+ this.file = file;
+ }
+
+ public List getAllTutorial(){
+ try {
+ FileInputStream fileIS = new FileInputStream(this.getFile());
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+
+ DocumentBuilder builder = builderFactory.newDocumentBuilder();
+
+ Document xmlDocument = builder.parse(fileIS);
+
+ String expression = "/tutorials/tutorial";
+
+ XPath path = new DOMXPath(expression);
+ List result = path.selectNodes(xmlDocument);
+ return result;
+
+ } catch (SAXException | IOException | ParserConfigurationException | JaxenException e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ public void setFile(File file) {
+ this.file = file;
+ }
+
+
+}
diff --git a/xml/src/main/java/com/baeldung/xml/StaxParser.java b/xml/src/main/java/com/baeldung/xml/StaxParser.java
new file mode 100644
index 0000000000..6f83e022f8
--- /dev/null
+++ b/xml/src/main/java/com/baeldung/xml/StaxParser.java
@@ -0,0 +1,120 @@
+package com.baeldung.xml;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+import com.baeldung.xml.model.Tutorial;
+
+public class StaxParser {
+
+ private File file;
+
+ public StaxParser(File file) {
+ this.file = file;
+ }
+
+ public List getAllTutorial() {
+ boolean bTitle = false;
+ boolean bDescription = false;
+ boolean bDate = false;
+ boolean bAuthor = false;
+ List tutorials = new ArrayList();
+ try {
+ XMLInputFactory factory = XMLInputFactory.newInstance();
+ XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
+ Tutorial current = null;
+ while (eventReader.hasNext()) {
+ XMLEvent event = eventReader.nextEvent();
+ switch (event.getEventType()) {
+ case XMLStreamConstants.START_ELEMENT:
+ StartElement startElement = event.asStartElement();
+ String qName = startElement.getName().getLocalPart();
+ if (qName.equalsIgnoreCase("tutorial")) {
+ current = new Tutorial();
+ Iterator attributes = startElement.getAttributes();
+ while (attributes.hasNext()) {
+ Attribute currentAt = attributes.next();
+ if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
+ current.setTutId(currentAt.getValue());
+ } else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
+ current.setType(currentAt.getValue());
+ }
+ }
+ } else if (qName.equalsIgnoreCase("title")) {
+ bTitle = true;
+ } else if (qName.equalsIgnoreCase("description")) {
+ bDescription = true;
+ } else if (qName.equalsIgnoreCase("date")) {
+ bDate = true;
+ } else if (qName.equalsIgnoreCase("author")) {
+ bAuthor = true;
+ }
+ break;
+ case XMLStreamConstants.CHARACTERS:
+ Characters characters = event.asCharacters();
+ if (bTitle) {
+ if (current != null) {
+ current.setTitle(characters.getData());
+ }
+ bTitle = false;
+ }
+ if (bDescription) {
+ if (current != null) {
+ current.setDescription(characters.getData());
+ }
+ bDescription = false;
+ }
+ if (bDate) {
+ if (current != null) {
+ current.setDate(characters.getData());
+ }
+ bDate = false;
+ }
+ if (bAuthor) {
+ if (current != null) {
+ current.setAuthor(characters.getData());
+ }
+ bAuthor = false;
+ }
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ EndElement endElement = event.asEndElement();
+ if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
+ if(current != null){
+ tutorials.add(current);
+ }
+ }
+ break;
+ }
+ }
+
+ } catch (FileNotFoundException | XMLStreamException e) {
+ e.printStackTrace();
+ }
+
+ return tutorials;
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ public void setFile(File file) {
+ this.file = file;
+ }
+
+}
diff --git a/xml/src/main/java/com/baeldung/xml/binding/Tutorial.java b/xml/src/main/java/com/baeldung/xml/binding/Tutorial.java
new file mode 100644
index 0000000000..c4668a9f77
--- /dev/null
+++ b/xml/src/main/java/com/baeldung/xml/binding/Tutorial.java
@@ -0,0 +1,61 @@
+package com.baeldung.xml.binding;
+
+import java.util.Date;
+
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+
+public class Tutorial {
+
+ private String tutId;
+ private String type;
+ private String title;
+ private String description;
+ private Date date;
+ private String author;
+
+
+ public String getTutId() {
+ return tutId;
+ }
+
+ @XmlAttribute
+ public void setTutId(String tutId) {
+ this.tutId = tutId;
+ }
+ public String getType() {
+ return type;
+ }
+ @XmlAttribute
+ public void setType(String type) {
+ this.type = type;
+ }
+ public String getTitle() {
+ return title;
+ }
+ @XmlElement
+ public void setTitle(String title) {
+ this.title = title;
+ }
+ public String getDescription() {
+ return description;
+ }
+ @XmlElement
+ public void setDescription(String description) {
+ this.description = description;
+ }
+ public Date getDate() {
+ return date;
+ }
+ @XmlElement
+ public void setDate(Date date) {
+ this.date = date;
+ }
+ public String getAuthor() {
+ return author;
+ }
+ @XmlElement
+ public void setAuthor(String author) {
+ this.author = author;
+ }
+}
diff --git a/xml/src/main/java/com/baeldung/xml/binding/Tutorials.java b/xml/src/main/java/com/baeldung/xml/binding/Tutorials.java
new file mode 100644
index 0000000000..ab6669c0ad
--- /dev/null
+++ b/xml/src/main/java/com/baeldung/xml/binding/Tutorials.java
@@ -0,0 +1,23 @@
+package com.baeldung.xml.binding;
+
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+public class Tutorials {
+
+ private List tutorial;
+
+ public List getTutorial() {
+ return tutorial;
+ }
+
+ @XmlElement
+ public void setTutorial(List tutorial) {
+ this.tutorial = tutorial;
+ }
+
+
+}
diff --git a/xml/src/main/java/com/baeldung/xml/model/Tutorial.java b/xml/src/main/java/com/baeldung/xml/model/Tutorial.java
new file mode 100644
index 0000000000..40a3f858cf
--- /dev/null
+++ b/xml/src/main/java/com/baeldung/xml/model/Tutorial.java
@@ -0,0 +1,49 @@
+package com.baeldung.xml.model;
+
+public class Tutorial {
+
+ private String tutId;
+ private String type;
+ private String title;
+ private String description;
+ private String date;
+ private String author;
+
+
+ public String getTutId() {
+ return tutId;
+ }
+ public void setTutId(String tutId) {
+ this.tutId = tutId;
+ }
+ public String getType() {
+ return type;
+ }
+ public void setType(String type) {
+ this.type = type;
+ }
+ public String getTitle() {
+ return title;
+ }
+ public void setTitle(String title) {
+ this.title = title;
+ }
+ public String getDescription() {
+ return description;
+ }
+ public void setDescription(String description) {
+ this.description = description;
+ }
+ public String getDate() {
+ return date;
+ }
+ public void setDate(String date) {
+ this.date = date;
+ }
+ public String getAuthor() {
+ return author;
+ }
+ public void setAuthor(String author) {
+ this.author = author;
+ }
+}
diff --git a/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java b/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java
index 451917a5da..734e7a93cb 100644
--- a/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java
+++ b/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java
@@ -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);
diff --git a/xml/src/test/java/com/baeldung/xml/Dom4JParserTest.java b/xml/src/test/java/com/baeldung/xml/Dom4JParserTest.java
new file mode 100644
index 0000000000..277eca8355
--- /dev/null
+++ b/xml/src/test/java/com/baeldung/xml/Dom4JParserTest.java
@@ -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 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());
+
+ }
+}
diff --git a/xml/src/test/java/com/baeldung/xml/JDomParserTest.java b/xml/src/test/java/com/baeldung/xml/JDomParserTest.java
new file mode 100644
index 0000000000..981458469f
--- /dev/null
+++ b/xml/src/test/java/com/baeldung/xml/JDomParserTest.java
@@ -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 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);
+ }
+
+}
diff --git a/xml/src/test/java/com/baeldung/xml/JaxbParserTest.java b/xml/src/test/java/com/baeldung/xml/JaxbParserTest.java
new file mode 100644
index 0000000000..4d6ddf5f65
--- /dev/null
+++ b/xml/src/test/java/com/baeldung/xml/JaxbParserTest.java
@@ -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"));
+ }
+}
diff --git a/xml/src/test/java/com/baeldung/xml/JaxenDemoTest.java b/xml/src/test/java/com/baeldung/xml/JaxenDemoTest.java
new file mode 100644
index 0000000000..2521fcaf8a
--- /dev/null
+++ b/xml/src/test/java/com/baeldung/xml/JaxenDemoTest.java
@@ -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);
+ }
+}
diff --git a/xml/src/test/java/com/baeldung/xml/StaxParserTest.java b/xml/src/test/java/com/baeldung/xml/StaxParserTest.java
new file mode 100644
index 0000000000..3188777a79
--- /dev/null
+++ b/xml/src/test/java/com/baeldung/xml/StaxParserTest.java
@@ -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 tutorials = parser.getAllTutorial();
+
+ assertNotNull(tutorials);
+ assertTrue(tutorials.size() == 4);
+ assertTrue(tutorials.get(0).getType().equalsIgnoreCase("java"));
+ }
+}
diff --git a/xml/src/test/resources/example.xml b/xml/src/test/resources/example.xml
index d546dd137b..0993b6240a 100644
--- a/xml/src/test/resources/example.xml
+++ b/xml/src/test/resources/example.xml
@@ -1,24 +1,24 @@
-
-
+
+
Guava
Introduction to Guava
04/04/2016
GuavaAuthor
-
-
+
+
XML
Introduction to XPath
04/05/2016
XMLAuthor
-
-
+
+
Android
Introduction to Android
04/03/2016
AndroidAuthor
-
-
+
+
Spring
Introduction to Spring
04/02/2016
@@ -28,5 +28,5 @@
-
-
\ No newline at end of file
+
+
\ No newline at end of file
diff --git a/xml/src/test/resources/example_namespace.xml b/xml/src/test/resources/example_namespace.xml
index 26131302ea..f1a880951a 100644
--- a/xml/src/test/resources/example_namespace.xml
+++ b/xml/src/test/resources/example_namespace.xml
@@ -1,24 +1,24 @@
-
-
+
+
Guava
Introduction to Guava
04/04/2016
GuavaAuthor
-
-
+
+
XML
Introduction to XPath
04/05/2016
XMLAuthor
-
-
+
+
Android
Introduction to Android
04/03/2016
AndroidAuthor
-
-
+
+
Spring
Introduction to Spring
04/02/2016
@@ -28,5 +28,5 @@
-
-
\ No newline at end of file
+
+
\ No newline at end of file
diff --git a/xml/src/test/resources/example_new.xml b/xml/src/test/resources/example_new.xml
new file mode 100644
index 0000000000..020760fdd3
--- /dev/null
+++ b/xml/src/test/resources/example_new.xml
@@ -0,0 +1,10 @@
+
+
+
+
+ XML with Dom4J
+ XML handling with Dom4J
+ 14/06/2016
+ Dom4J tech writer
+
+
diff --git a/xml/src/test/resources/example_updated.xml b/xml/src/test/resources/example_updated.xml
new file mode 100644
index 0000000000..962ca0c889
--- /dev/null
+++ b/xml/src/test/resources/example_updated.xml
@@ -0,0 +1,32 @@
+
+
+
+ Guava updated
+ Introduction to Guava
+ 04/04/2016
+ GuavaAuthor
+
+
+ XML updated
+ Introduction to XPath
+ 04/05/2016
+ XMLAuthor
+
+
+ Android updated
+ Introduction to Android
+ 04/03/2016
+ AndroidAuthor
+
+
+ Spring updated
+ Introduction to Spring
+ 04/02/2016
+ SpringAuthor
+
+
+
+
+
+
+
\ No newline at end of file