View Javadoc
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   * ParseXml.java.
23   * <p>
24   * Copyright (c) 2022-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * </p>
27   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
29   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
30   */
31  public class ParseXml
32  {
33      /**
34       * Make a DOM tree and do some xpath.
35       * @throws Exception
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); // no DTD validation
49          // builderFactory.setFeature("http://apache.org/xml/features/validation/schema", true);
50          // builderFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
51          // builderFactory.setFeature("http://apache.org/xml/features/validation/id-idref-checking", true);
52          // builderFactory.setFeature("http://apache.org/xml/features/validation/identity-constraint-checking", true);
53          // builderFactory.setFeature("http://apache.org/xml/features/xinclude", true);
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      * @param args
105      * @throws Exception
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         /** {@inheritDoc} */
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 }