title: java StAX - standalone property of StartDocument
body: <p>I want to read, manipulate and write a xml file. I wanted to start with just read and write and then manipulate later.</p>
  
  <p>I use the StAX Parser and want to use EventReader and EventWriter.</p>
  
  <p>I encountered my first problem when wanting to read and write the StartDocument element.</p>
  
  <p>Input:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="iso-8859-15" standalone="yes"?&gt;
  </code></pre>
  
  <p>Output in file "data_output_test.xml":</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="iso-8859-15"?&gt;
  </code></pre>
  
  <p>I created the startDocument object explicitly with </p>
  
  <pre><code>eventFactory.createStartDocument("iso-8859-15", "1.0", true);
  </code></pre>
  
  <p>But the property does not show when I write the new startDocument object to file.</p>
  
  <p>Here is my code:</p>
  
  <pre><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;
      }
  }
  </code></pre>
  
  <p>Is there a way to force XMLEventWriter to write the standalone property it?</p>
  
  <p>My second problem is, that the encoding is not recognized when I do</p>
  
  <pre><code>event_in.toString()
  </code></pre>
  
  <p>on the event startDocument.</p>
  
  <p>Ouput of my program is:</p>
  
  <pre><code>StartElement: true
  StartElement: &lt;?xml version="1.0" encoding='null' standalone='yes'?&gt;
  </code></pre>
  
  <p>Why is that?</p>
  
  <p>Thanks in advance already</p>