Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would you use Java to handle various XML documents?
    text
    copied!<p>I'm looking for the best method to parse various XML documents using a Java application. I'm currently doing this with SAX and a custom content handler and it works great - zippy and stable. </p> <p>I've decided to explore the option having the same program, that currently recieves a single format XML document, receive two additional XML document formats, with various XML element changes. I was hoping to just swap out the ContentHandler with an appropriate one based on the first "startElement" in the document... but, uh-duh, the ContentHandler is set and <strong>then</strong> the document is parsed!</p> <pre><code>... constructor ... { SAXParserFactory spf = SAXParserFactory.newInstance(); try { SAXParser sp = spf.newSAXParser(); parser = sp.getXMLReader(); parser.setErrorHandler(new MyErrorHandler()); } catch (Exception e) {} ... parse StringBuffer ... try { parser.setContentHandler(pP); parser.parse(new InputSource(new StringReader(xml.toString()))); return true; } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } ... </code></pre> <p>So, it doesn't appear that I can do this in the way I initially thought I could.</p> <p>That being said, am I looking at this entirely wrong? What is the best method to parse multiple, discrete XML documents with the same XML handling code? <a href="https://stackoverflow.com/questions/23106/best-method-to-parse-various-custom-xml-documents-in-java">I tried to ask in a more general post earlier... but, I think I was being too vague</a>. For speed and efficiency purposes I never really looked at DOM because these XML documents are fairly large and the system receives about 1200 every few minutes. It's just a one way send of information</p> <p>To make this question too long and add to my confusion; following is a mockup of some various XML documents that I would like to have a single SAX, StAX, or ?? parser cleanly deal with. </p> <p>products.xml:</p> <pre><code>&lt;products&gt; &lt;product&gt; &lt;id&gt;1&lt;/id&gt; &lt;name&gt;Foo&lt;/name&gt; &lt;product&gt; &lt;id&gt;2&lt;/id&gt; &lt;name&gt;bar&lt;/name&gt; &lt;/product&gt; &lt;/products&gt; </code></pre> <p>stores.xml:</p> <pre><code>&lt;stores&gt; &lt;store&gt; &lt;id&gt;1&lt;/id&gt; &lt;name&gt;S1A&lt;/name&gt; &lt;location&gt;CA&lt;/location&gt; &lt;/store&gt; &lt;store&gt; &lt;id&gt;2&lt;/id&gt; &lt;name&gt;A1S&lt;/name&gt; &lt;location&gt;NY&lt;/location&gt; &lt;/store&gt; &lt;/stores&gt; </code></pre> <p>managers.xml:</p> <pre><code>&lt;managers&gt; &lt;manager&gt; &lt;id&gt;1&lt;/id&gt; &lt;name&gt;Fen&lt;/name&gt; &lt;store&gt;1&lt;/store&gt; &lt;/manager&gt; &lt;manager&gt; &lt;id&gt;2&lt;/id&gt; &lt;name&gt;Diz&lt;/name&gt; &lt;store&gt;2&lt;/store&gt; &lt;/manager&gt; &lt;/managers&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload