diff --git a/pom.xml b/pom.xml index 7a51347af6..ce7dfca62f 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ spring-thymeleaf spring-zuul jsf - + xml diff --git a/xml/pom.xml b/xml/pom.xml new file mode 100644 index 0000000000..fc158901e6 --- /dev/null +++ b/xml/pom.xml @@ -0,0 +1,139 @@ + + 4.0.0 + com.baeldung + xml + 0.1-SNAPSHOT + + xml + + + + + + com.google.guava + guava + ${guava.version} + + + + commons-io + commons-io + 2.4 + + + + org.apache.commons + commons-collections4 + 4.0 + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + + + junit + junit + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + xml + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 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 + + + 1.3 + 4.12 + 1.10.19 + + 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 new file mode 100644 index 0000000000..89326f45c9 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/DefaultParser.java @@ -0,0 +1,193 @@ +package com.baeldung.xml; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Iterator; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class DefaultParser { + + private File file; + + public DefaultParser(File file) { + this.file = file; + } + + public NodeList getFirstLevelNodeList() { + NodeList nodeList = null; + try { + FileInputStream fileIS = new FileInputStream(this.getFile()); + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + + Document xmlDocument = builder.parse(fileIS); + + XPath xPath = XPathFactory.newInstance().newXPath(); + + String expression = "/Tutorials/Tutorial"; + + nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); + + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { + e.printStackTrace(); + } + return nodeList; + } + + public Node getNodeById(String id) { + Node node = null; + try { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + + Document xmlDocument = builder.parse(this.getFile()); + + XPath xPath = XPathFactory.newInstance().newXPath(); + + String expression = "/Tutorials/Tutorial[@tutId=" + "'" + id + "'" + "]"; + + node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE); + + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { + e.printStackTrace(); + } + return node; + } + + public NodeList getNodeListByTitle(String name) { + NodeList nodeList = null; + try { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + + Document xmlDocument = builder.parse(this.getFile()); + + this.clean(xmlDocument); + + XPath xPath = XPathFactory.newInstance().newXPath(); + + String expression = "//Tutorial[descendant::title[text()=" + "'" + name + "'" + "]]"; + + nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); + + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { + e.printStackTrace(); + } + return nodeList; + } + + public NodeList getElementsByDate(String date) { + NodeList nodeList = null; + + try { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + + Document xmlDocument = builder.parse(this.getFile()); + + this.clean(xmlDocument); + + XPath xPath = XPathFactory.newInstance().newXPath(); + + String expression = "//Tutorial[number(translate(date, '/', '')) > " + date + "]"; + + nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); + + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { + e.printStackTrace(); + } + return nodeList; + } + + public NodeList getAllTutorials() { + NodeList nodeList = null; + try { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + builderFactory.setNamespaceAware(true); + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + + Document xmlDocument = builder.parse(this.getFile()); + + this.clean(xmlDocument); + + XPath xPath = XPathFactory.newInstance().newXPath(); + + xPath.setNamespaceContext(new NamespaceContext() { + + @Override + public Iterator getPrefixes(String arg0) { + return null; + } + + @Override + public String getPrefix(String arg0) { + return null; + } + + @Override + public String getNamespaceURI(String arg0) { + if ("bdn".equals(arg0)) { + return "http://www.baeldung.com/full_archive"; + } + return null; + } + }); + + String expression = "/bdn:Tutorials/bdn:Tutorial"; + + nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); + + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { + e.printStackTrace(); + } + return nodeList; + } + + private void clean(Node node) { + + NodeList childs = node.getChildNodes(); + + for (int n = childs.getLength() - 1; n >= 0; n--) { + Node child = childs.item(n); + short nodeType = child.getNodeType(); + + if (nodeType == Node.ELEMENT_NODE) + clean(child); + else if (nodeType == Node.TEXT_NODE) { + String trimmedNodeVal = child.getNodeValue().trim(); + if (trimmedNodeVal.length() == 0) + node.removeChild(child); + else + child.setNodeValue(trimmedNodeVal); + } else if (nodeType == Node.COMMENT_NODE) + node.removeChild(child); + } + } + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + +} diff --git a/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java b/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java new file mode 100644 index 0000000000..451917a5da --- /dev/null +++ b/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java @@ -0,0 +1,83 @@ +package com.baeldung.xml; + +import static org.junit.Assert.*; + +import java.io.File; + +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"; + + final String fileNameSpace = "src/test/resources/example_namespace.xml"; + + DefaultParser parser; + + @Test + public void getFirstLevelNodeListTest() { + parser = new DefaultParser(new File(fileName)); + NodeList list = parser.getFirstLevelNodeList(); + + assertNotNull(list); + assertTrue(list.getLength() == 4); + } + + @Test + public void getNodeListByTitle() { + parser = new DefaultParser(new File(fileName)); + NodeList list = parser.getNodeListByTitle("XML"); + + for (int i = 0; null != list && i < list.getLength(); i++) { + Node nod = list.item(i); + assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent()); + assertEquals("02", nod.getAttributes().getNamedItem("tutId").getTextContent()); + assertEquals("XML", nod.getFirstChild().getTextContent()); + assertEquals("title", nod.getFirstChild().getNodeName()); + assertEquals("description", nod.getChildNodes().item(1).getNodeName()); + assertEquals("Introduction to XPath", nod.getChildNodes().item(1).getTextContent()); + assertEquals("author", nod.getLastChild().getNodeName()); + assertEquals("XMLAuthor", nod.getLastChild().getTextContent()); + } + } + + @Test + public void getNodeById() { + parser = new DefaultParser(new File(fileName)); + Node node = parser.getNodeById("03"); + + String type = node.getAttributes().getNamedItem("type").getNodeValue(); + assertEquals("android", type); + } + + @Test + public void getNodeListByDate(){ + parser = new DefaultParser(new File(fileName)); + NodeList list = parser.getNodeListByTitle("04022016"); + for (int i = 0; null != list && i < list.getLength(); i++) { + Node nod = list.item(i); + assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent()); + assertEquals("04", nod.getAttributes().getNamedItem("tutId").getTextContent()); + assertEquals("Spring", nod.getFirstChild().getTextContent()); + assertEquals("title", nod.getFirstChild().getNodeName()); + assertEquals("description", nod.getChildNodes().item(1).getNodeName()); + assertEquals("Introduction to Spring", nod.getChildNodes().item(1).getTextContent()); + assertEquals("author", nod.getLastChild().getNodeName()); + assertEquals("SpringAuthor", nod.getLastChild().getTextContent()); + } + } + + @Test + public void getNodeListWithNamespace(){ + parser = new DefaultParser(new File(fileNameSpace)); + NodeList list = parser.getAllTutorials(); + assertNotNull(list); + assertTrue(list.getLength() == 4); + } + +} diff --git a/xml/src/test/resources/example.xml b/xml/src/test/resources/example.xml new file mode 100644 index 0000000000..d546dd137b --- /dev/null +++ b/xml/src/test/resources/example.xml @@ -0,0 +1,32 @@ + + + + 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 + SpringAuthor + +
Spring Core
+
Spring MVC
+
Spring Batch
+
+
+
\ No newline at end of file diff --git a/xml/src/test/resources/example_namespace.xml b/xml/src/test/resources/example_namespace.xml new file mode 100644 index 0000000000..26131302ea --- /dev/null +++ b/xml/src/test/resources/example_namespace.xml @@ -0,0 +1,32 @@ + + + + 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 + SpringAuthor + +
Spring Core
+
Spring MVC
+
Spring Batch
+
+
+
\ No newline at end of file