1 package org.opentrafficsim.demo;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.InputStream;
6
7 import javax.xml.parsers.DocumentBuilder;
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.xpath.XPath;
10 import javax.xml.xpath.XPathConstants;
11 import javax.xml.xpath.XPathFactory;
12
13 import org.djutils.io.URLResource;
14 import org.w3c.dom.Document;
15 import org.w3c.dom.NamedNodeMap;
16 import org.w3c.dom.Node;
17 import org.w3c.dom.NodeList;
18 import org.xml.sax.EntityResolver;
19 import org.xml.sax.InputSource;
20
21
22
23
24
25
26
27
28
29
30
31 public class ParseXml
32 {
33
34
35
36
37 public ParseXml() throws Exception
38 {
39 domTree(URLResource.getResource("/resources/conflict/Simple.xml").getPath());
40 }
41
42 private void domTree(final String path) throws Exception
43 {
44 File file = new File(path);
45 FileInputStream fileIS = new FileInputStream(file);
46 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
47 builderFactory.setXIncludeAware(true);
48 builderFactory.setValidating(false);
49
50
51
52
53
54 builderFactory.setNamespaceAware(true);
55 DocumentBuilder builder = builderFactory.newDocumentBuilder();
56 builder.setEntityResolver(new DefaultsResolver());
57 Document xmlDocument = builder.parse(fileIS);
58 print(xmlDocument, "*");
59 print(xmlDocument, "*/ots:Ots");
60 print(xmlDocument, ".//ots:GTUTYPE");
61 }
62
63 private void print(final Document xmlDocument, final String expression) throws Exception
64 {
65 XPath xPath = XPathFactory.newInstance().newXPath();
66 NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
67 System.out.print(expression + " = [ ");
68 boolean first = true;
69 for (int i = 0; i < nodeList.getLength(); i++)
70 {
71 if (first)
72 {
73 first = false;
74 }
75 else
76 {
77 System.out.print(", ");
78 }
79 Node n = nodeList.item(i);
80 System.out.print("{" + n.getNodeName());
81 NamedNodeMap attMap = n.getAttributes();
82 if (attMap != null)
83 {
84 boolean f2 = true;
85 for (int j = 0; j < attMap.getLength(); j++)
86 {
87 if (f2)
88 {
89 f2 = false;
90 }
91 else
92 {
93 System.out.print(":");
94 }
95 System.out.print(" " + attMap.item(j).getNodeName() + "=" + attMap.item(j).getNodeValue());
96 }
97 }
98 System.out.print("}");
99 }
100 System.out.println(" ]");
101 }
102
103
104
105
106
107 public static void main(final String[] args) throws Exception
108 {
109 new ParseXml();
110 }
111
112 static class DefaultsResolver implements EntityResolver
113 {
114
115 @Override
116 public InputSource resolveEntity(final String publicId, final String systemId)
117 {
118 if (systemId.contains("defaults/"))
119 {
120 System.out.println("\nINCLUDING " + systemId);
121 String location = "/resources/xsd/defaults" + systemId.substring(systemId.lastIndexOf('/'));
122 InputStream stream = URLResource.getResourceAsStream(location);
123 return new InputSource(stream);
124 }
125 else
126 {
127 return new InputSource(URLResource.getResourceAsStream(systemId));
128 }
129 }
130 }
131
132 }