title: StAX - Setting the version and encoding using XMLStreamWriter
body: <p>I am using StAX for creating XML files and then validating the file with and XSD.</p>
  
  <p>I am getting an error while creating the XML file:</p>
  
  <pre><code>javax.xml.stream.XMLStreamException: Underlying stream encoding 'Cp1252' and input paramter for writeStartDocument() method 'UTF-8' do not match.
          at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeStartDocument(XMLStreamWriterImpl.java:1182)
  </code></pre>
  
  <p>Here is the code snippet:</p>
  
  <pre><code>XMLOutputFactory xof =  XMLOutputFactory.newInstance();
  
  try{
  
    XMLStreamWriter xtw = xof.createXMLStreamWriter(new FileWriter(fileName));
    xtw.writeStartDocument("UTF-8","1.0");} catch(XMLStreamException e) {
    e.printStackTrace();
  
  } catch(IOException ie) {
  
    ie.printStackTrace();
  
  }
  </code></pre>
  
  <p>I am running this code on Unix. Does anybody know how to set the version and encoding style?</p>