Note that there are some explanatory texts on larger screens.

plurals
  1. POCode for Using StAX in java
    primarykey
    data
    text
    <p>I have an 200 MB xml of the following form:</p> <pre><code> &lt;school name = "some school"&gt; &lt;class standard = "2A"&gt; &lt;student&gt; ..... &lt;/student&gt; &lt;student&gt; ..... &lt;/student&gt; &lt;student&gt; ..... &lt;/student&gt; &lt;/class&gt; &lt;/school&gt; </code></pre> <p>I need to <strong>split this xml into several files</strong> using StAX such <strong>that n students come under each xml file</strong> and the structure is preserved as <code>&lt;school&gt;</code> then <code>&lt;class&gt;</code> and <code>&lt;students&gt;</code> under them. The attributes of School and class also must be preserved in the resultant xmls.</p> <p>Here is the code I am using:</p> <pre><code>XMLInputFactory inputFactory = XMLInputFactory.newInstance(); String xmlFile = "input.XML"; XMLEventReader reader = inputFactory.createXMLEventReader(new FileReader(xmlFile)); XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE); XMLEventWriter writer = null; int count = 0; QName name = new QName(null, "student"); try { while (true) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement element = event.asStartElement(); if (element.getName().equals(name)) { String filename = "input"+ count + ".xml"; writer = outputFactory.createXMLEventWriter(new FileWriter(filename)); writeToFile(reader, event, writer); writer.close(); count++; } } if (event.isEndDocument()) break; } } catch (XMLStreamException e) { throw e; } catch (IOException e) { e.printStackTrace(); } finally { reader.close(); } private static void writeToFile(XMLEventReader reader, XMLEvent startEvent, XMLEventWriter writer) throws XMLStreamException, IOException { StartElement element = startEvent.asStartElement(); QName name = element.getName(); int stack = 1; writer.add(element); while (true) { XMLEvent event = reader.nextEvent(); if (event.isStartElement() &amp;&amp; event.asStartElement().getName().equals(name)) stack++; if (event.isEndElement()) { EndElement end = event.asEndElement(); if (end.getName().equals(name)) { stack--; if (stack == 0) { writer.add(event); break; } } } writer.add(event); } } </code></pre> <p>Please check the function call <code>writeToFile(reader, event, writer)</code> in the try block. Here the reader object has only the <code>student</code> tag. I need the reader has the <code>school</code>, <code>class</code>, and then n <code>students</code> in it. so that the file generated has a similar structure as the original only with lesser children per file.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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