|
| 1 | +package com.baeldung.xml; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Iterator; |
| 7 | + |
| 8 | +import javax.xml.namespace.NamespaceContext; |
| 9 | +import javax.xml.parsers.DocumentBuilder; |
| 10 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 11 | +import javax.xml.parsers.ParserConfigurationException; |
| 12 | +import javax.xml.xpath.XPath; |
| 13 | +import javax.xml.xpath.XPathConstants; |
| 14 | +import javax.xml.xpath.XPathExpressionException; |
| 15 | +import javax.xml.xpath.XPathFactory; |
| 16 | + |
| 17 | +import org.w3c.dom.Document; |
| 18 | +import org.w3c.dom.Node; |
| 19 | +import org.w3c.dom.NodeList; |
| 20 | +import org.xml.sax.SAXException; |
| 21 | + |
| 22 | +public class DefaultParser { |
| 23 | + |
| 24 | + private File file; |
| 25 | + |
| 26 | + public DefaultParser(File file) { |
| 27 | + this.file = file; |
| 28 | + } |
| 29 | + |
| 30 | + public NodeList getFirstLevelNodeList() { |
| 31 | + NodeList nodeList = null; |
| 32 | + try { |
| 33 | + FileInputStream fileIS = new FileInputStream(this.getFile()); |
| 34 | + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
| 35 | + |
| 36 | + DocumentBuilder builder = builderFactory.newDocumentBuilder(); |
| 37 | + |
| 38 | + Document xmlDocument = builder.parse(fileIS); |
| 39 | + |
| 40 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 41 | + |
| 42 | + String expression = "/Tutorials/Tutorial"; |
| 43 | + |
| 44 | + nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); |
| 45 | + |
| 46 | + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + return nodeList; |
| 50 | + } |
| 51 | + |
| 52 | + public Node getNodeById(String id) { |
| 53 | + Node node = null; |
| 54 | + try { |
| 55 | + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
| 56 | + |
| 57 | + DocumentBuilder builder = builderFactory.newDocumentBuilder(); |
| 58 | + |
| 59 | + Document xmlDocument = builder.parse(this.getFile()); |
| 60 | + |
| 61 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 62 | + |
| 63 | + String expression = "/Tutorials/Tutorial[@tutId=" + "'" + id + "'" + "]"; |
| 64 | + |
| 65 | + node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE); |
| 66 | + |
| 67 | + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + return node; |
| 71 | + } |
| 72 | + |
| 73 | + public NodeList getNodeListByTitle(String name) { |
| 74 | + NodeList nodeList = null; |
| 75 | + try { |
| 76 | + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
| 77 | + |
| 78 | + DocumentBuilder builder = builderFactory.newDocumentBuilder(); |
| 79 | + |
| 80 | + Document xmlDocument = builder.parse(this.getFile()); |
| 81 | + |
| 82 | + this.clean(xmlDocument); |
| 83 | + |
| 84 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 85 | + |
| 86 | + String expression = "//Tutorial[descendant::title[text()=" + "'" + name + "'" + "]]"; |
| 87 | + |
| 88 | + nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); |
| 89 | + |
| 90 | + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + return nodeList; |
| 94 | + } |
| 95 | + |
| 96 | + public NodeList getElementsByDate(String date) { |
| 97 | + NodeList nodeList = null; |
| 98 | + |
| 99 | + try { |
| 100 | + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
| 101 | + |
| 102 | + DocumentBuilder builder = builderFactory.newDocumentBuilder(); |
| 103 | + |
| 104 | + Document xmlDocument = builder.parse(this.getFile()); |
| 105 | + |
| 106 | + this.clean(xmlDocument); |
| 107 | + |
| 108 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 109 | + |
| 110 | + String expression = "//Tutorial[number(translate(date, '/', '')) > " + date + "]"; |
| 111 | + |
| 112 | + nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); |
| 113 | + |
| 114 | + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + return nodeList; |
| 118 | + } |
| 119 | + |
| 120 | + public NodeList getAllTutorials() { |
| 121 | + NodeList nodeList = null; |
| 122 | + try { |
| 123 | + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
| 124 | + builderFactory.setNamespaceAware(true); |
| 125 | + DocumentBuilder builder = builderFactory.newDocumentBuilder(); |
| 126 | + |
| 127 | + Document xmlDocument = builder.parse(this.getFile()); |
| 128 | + |
| 129 | + this.clean(xmlDocument); |
| 130 | + |
| 131 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 132 | + |
| 133 | + xPath.setNamespaceContext(new NamespaceContext() { |
| 134 | + |
| 135 | + @Override |
| 136 | + public Iterator getPrefixes(String arg0) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String getPrefix(String arg0) { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String getNamespaceURI(String arg0) { |
| 147 | + if ("bdn".equals(arg0)) { |
| 148 | + return "http://www.baeldung.com/full_archive"; |
| 149 | + } |
| 150 | + return null; |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + String expression = "/bdn:Tutorials/bdn:Tutorial"; |
| 155 | + |
| 156 | + nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); |
| 157 | + |
| 158 | + } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) { |
| 159 | + e.printStackTrace(); |
| 160 | + } |
| 161 | + return nodeList; |
| 162 | + } |
| 163 | + |
| 164 | + private void clean(Node node) { |
| 165 | + |
| 166 | + NodeList childs = node.getChildNodes(); |
| 167 | + |
| 168 | + for (int n = childs.getLength() - 1; n >= 0; n--) { |
| 169 | + Node child = childs.item(n); |
| 170 | + short nodeType = child.getNodeType(); |
| 171 | + |
| 172 | + if (nodeType == Node.ELEMENT_NODE) |
| 173 | + clean(child); |
| 174 | + else if (nodeType == Node.TEXT_NODE) { |
| 175 | + String trimmedNodeVal = child.getNodeValue().trim(); |
| 176 | + if (trimmedNodeVal.length() == 0) |
| 177 | + node.removeChild(child); |
| 178 | + else |
| 179 | + child.setNodeValue(trimmedNodeVal); |
| 180 | + } else if (nodeType == Node.COMMENT_NODE) |
| 181 | + node.removeChild(child); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + public File getFile() { |
| 186 | + return file; |
| 187 | + } |
| 188 | + |
| 189 | + public void setFile(File file) { |
| 190 | + this.file = file; |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments