Hallo Mr Kay,
thank you for the quick help.
I tried adding:
Code:
factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
Unfortunately, it does not changes, I still get ParseError-Exception.
Code:
Exception in thread "main" javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,6]
Message: Verarbeitungsanweisungsziel, das "[xX][mM][lL]" entspricht, ist nicht zulässig.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at transformation.activity.Demo6.weiter(Demo6.java:59)
at transformation.activity.Demo6.main(Demo6.java:40)
pa
My input file looks exactly like the following, and I actually just want to extract each nextTag with all contents nextTag propably has.:
Code:
<?xml version="1.0" encoding="UTF-8"?><firstTag><nextTag><adutan>1</adutan></nextTag></firstTag>
<?xml version="1.0" encoding="UTF-8"?><firstTag><nextTag>2</nextTag></firstTag>
Each line is a well conformed XML document.
By using StaX, if the parser reached <nextTag>, I let this tag be transformed from transformer. Unfortunately, the transformer seems to be irritated from the xml declaration on the 2nd line.
I actually just expect to get:
Code:
<?xml version="1.0" encoding="UTF-8"?><nextTag><adutan>1</adutan></nextTag>
<?xml version="1.0" encoding="UTF-8"?><nextTag>2</nextTag>
The header is in the transformation output is retrieved automatically, because OutputKeys.OMIT_XML_DECLARATION is per default NO.
I actually can try to get each line separately and transform it, also separately, but maybe is it possible to get multiple conform XML documents in a file parsed at one time?
Thank you for your comments.
Regards,
Ratna
My complete code looks like this:
Code:
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
public class Demo6 {
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream("input.xml");
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
XMLStreamReader streamReader = factory
.createXMLStreamReader(inputStream);
while (streamReader.hasNext()) {
streamReader.next();
if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT
&& "nextTag".equals(streamReader.getLocalName())) {
StringWriter writer = new StringWriter();
t.transform(new StAXSource(streamReader), new StreamResult(
writer));
String output = writer.toString();
System.out.println(output);
weiter(streamReader, t);
}
}
}
private static void weiter(XMLStreamReader streamReader,
Transformer transformer) throws Exception {
while (streamReader.hasNext()) {
if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT
&& "nextTag".equals(streamReader.getLocalName())) {
StringWriter writer1 = new StringWriter();
transformer.transform(new StAXSource(streamReader),
new StreamResult(writer1));
String output1 = writer1.toString();
System.out.println(output1);
} else {streamReader.next();}
}
}
}