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