问题
I want to read, manipulate and write a xml file. I wanted to start with just read and write and then manipulate later.
I use the StAX Parser and want to use EventReader and EventWriter.
I encountered my first problem when wanting to read and write the StartDocument element.
Input:
<?xml version="1.0" encoding="iso-8859-15" standalone="yes"?>
Output in file "data_output_test.xml":
<?xml version="1.0" encoding="iso-8859-15"?>
I created the startDocument object explicitly with
eventFactory.createStartDocument("iso-8859-15", "1.0", true);
But the property does not show when I write the new startDocument object to file.
Here is my code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
public class XML_ReadWrite{
@SuppressWarnings("unchecked")
public static void main(String[] args) {
XMLInputFactory factory_in = null;
XMLOutputFactory factory_out = null;
XMLEventReader eventReader = null;
XMLEventFactory eventFactory = null;
XMLEventWriter eventWriter = null;
XMLEvent event_out = null;
XMLEvent event_in = null;
try {
factory_in = XMLInputFactory.newInstance();
eventReader = factory_in.createXMLEventReader (getReader("data_test.xml", "iso-8859-15"));
} catch (XMLStreamException e) {
e.printStackTrace();
return;
} catch (Exception e) {
//UnsupportedEncodingException or FileNotFoundException
e.printStackTrace();
return;
}
try {
factory_out = XMLOutputFactory.newInstance();
eventFactory = XMLEventFactory.newInstance();
eventWriter = factory_out.createXMLEventWriter(getWriter("data_output_test.xml", "iso-8859-15"));
event_out = eventFactory.createStartDocument(null, "1.0", true);
eventWriter.add(event_out);
eventWriter.flush();
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
//UnsupportedEncodingException or FileNotFoundException
e.printStackTrace();
return;
}
while(eventReader.hasNext()){
try {
event_in = eventReader.nextEvent();
} catch (XMLStreamException e) {
e.printStackTrace();
continue;
}
if(event_in.getEventType() == XMLStreamConstants.START_DOCUMENT){
System.out.println("StartElement: " + event_in.isStartDocument());
System.out.println("StartElement: " + event_in.toString());
}
}
}
public static InputStreamReader getReader(String filename, String encoding) throws Exception {
FileInputStream in = new FileInputStream(filename);
InputStreamReader r = null;
r = new InputStreamReader(in, encoding);
return r;
}
public static OutputStreamWriter getWriter(String filename, String encoding) throws Exception {
FileOutputStream out = new FileOutputStream(filename);
OutputStreamWriter w = new OutputStreamWriter(out, encoding);
return w;
}
}
Is there a way to force XMLEventWriter to write the standalone property it?
My second problem is, that the encoding is not recognized when I do
event_in.toString()
on the event startDocument.
Ouput of my program is:
StartElement: true
StartElement: <?xml version="1.0" encoding='null' standalone='yes'?>
Why is that?
Thanks in advance already
来源:https://stackoverflow.com/questions/28629709/java-stax-standalone-property-of-startdocument