title: Java XML parsing error : Content is not allowed in prolog
body: <p>My code write a XML file with the LSSerializer class :</p>
  
  <pre><code>DOMImplementation impl = doc.getImplementation();
  DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS","3.0");
  
  LSSerializer ser = implLS.createLSSerializer();
  
  String str = ser.writeToString(doc);
  System.out.println(str);
  
  String file = racine+"/"+p.getNom()+".xml";
  OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
  out.write(str);
  out.close();
  </code></pre>
  
  <p>The XML is well-formed, but when I parse it, I get an error.</p>
  
  <p>Parse code :</p>
  
  <pre><code>File f = new File(racine+"/"+filename);
  
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse(f);
  
  XPathFactory xpfactory = XPathFactory.newInstance();
  XPath xp = xpfactory.newXPath();
  
  String expression;
  
  expression = "root/nom";        
  String nom = xp.evaluate(expression, doc);
  </code></pre>
  
  <p>The error :</p>
  
  <pre><code>[Fatal Error] Terray.xml:1:40: Content is not allowed in prolog.
  9 août 2011 19:42:58 controller.MakaluController activatePatient
  GRAVE: null
  org.xml.sax.SAXParseException: Content is not allowed in prolog.
  at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)
  at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
  at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
  at model.MakaluModel.setPatientActif(MakaluModel.java:147)
  at controller.MakaluController.activatePatient(MakaluController.java:59)
  at view.ListePatientsPanel.jButtonOKActionPerformed(ListePatientsPanel.java:92)
  ...
  </code></pre>
  
  <p>Now, with some research, <a href="http://www.judahfrangipane.com/blog/2006/12/13/content-is-not-allowed-in-prolog/" rel="nofollow">I found</a> that this error is dure to a "hidden" character at the very beginning of the XML.</p>
  
  <p>In fact, I can fix the bug by creating a XML file manually.</p>
  
  <p>But where is the error in the XML writing ? (When I try to println the string, there is no space before ths 
  
  <h2>Solution : change the serializer</h2>
  
  <p>I run the solution of UTF-16 encoding for a while, but it was not very stable.
  So I found a new solution : change the serializer of the XML document, so that the encoding is coherent between the XML header and the file encoding. :</p>
  
  <pre><code>    DOMSource domSource = new DOMSource(doc);
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer transformer = tf.newTransformer();
  
      String file = racine+"/"+p.getNom()+".xml";
      OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
  
      transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
      transformer.setOutputProperty(OutputKeys.INDENT,"yes");
      transformer.transform(domSource, new StreamResult(out));
  </code></pre>