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