IndexOutOfBoundsException when extracting XML with Transformer



I want to extract specific nodes from a large XML file.


That works well in test cases with small XML files, but when I use my real XML file, I get following output:



FEHLER: ''
javax.xml.transform.TransformerException: java.lang.IndexOutOfBoundsException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:732)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:336)
at xml_test.XML_Test.extractXML2(XML_Test.java:698)
at xml_test.XML_Test.main(XML_Test.java:811)
Caused by: java.lang.IndexOutOfBoundsException
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.getTextCharacters(XMLStreamReaderImpl.java:1143)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleCharacters(StAXStream2SAX.java:261)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(StAXStream2SAX.java:171)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(StAXStream2SAX.java:120)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:674)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:723)
... 3 more
---------
java.lang.IndexOutOfBoundsException
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.getTextCharacters(XMLStreamReaderImpl.java:1143)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleCharacters(StAXStream2SAX.java:261)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(StAXStream2SAX.java:171)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(StAXStream2SAX.java:120)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:674)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:723)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:336)
at xml_test.XML_Test.extractXML2(XML_Test.java:698)
at xml_test.XML_Test.main(XML_Test.java:811)


The code:



InputStream stream = new FileInputStream("C:\\myFile.xml");
StreamSource source = new StreamSource(stream);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(source);

TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();

String extractPath = "/Hotels/Hotel";
String path = "";

while(reader.hasNext()) {
reader.next();

if(reader.isStartElement()) {
path += "/" + reader.getLocalName();

if(path.equals(extractPath)) {
StringWriter writer = new StringWriter();
StAXSource src = new StAXSource(reader);
StreamResult res = new StreamResult(writer);
t.transform(src, res); // Exception thrown

System.out.println(writer.toString());

path = path.substring(0, path.lastIndexOf("/"));
}
}
else if(reader.isEndElement()) {
path = path.substring(0, path.lastIndexOf("/"));
}
}


The XML file is not malformed since I successfully validated it. Pretty-printed it's about 7000 lines long. Maybe that's too much?


No comments:

Post a Comment