Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6249853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:18:57+00:00 2026-05-24T13:18:57+00:00

My code write a XML file with the LSSerializer class : DOMImplementation impl =

  • 0

My code write a XML file with the LSSerializer class :

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();

The XML is well-formed, but when I parse it, I get an error.

Parse 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);

The error :

[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)
...

Now, with some research, I found that this error is dure to a “hidden” character at the very beginning of the XML.

In fact, I can fix the bug by creating a XML file manually.

But where is the error in the XML writing ? (When I try to println the string, there is no space before ths

Solution : change the serializer

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. :

    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));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:18:58+00:00Added an answer on May 24, 2026 at 1:18 pm

    But where is the error in the XML writing ?

    Looks like the error is not in the writing but the parsing. As you have already discovered there is a blank character at the beginning of the file, which causes the error in the parse call in your stach trace:

    Document doc = builder.parse(f);
    

    The reason you do not see the space when you print it out may be simply the encoding you are using. Try changing this line:

    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
    

    to use ‘UTF-16’ or ‘US-ASCII’

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a .xml file using the following code into the App_Data/posts.
I have the following piece of code to write data to an XML file.
Are there any Code Generators that would take a well-formed XML document, and write
Really stuck on trying to write code to unzip a file or directory on
I have a bit of Java code that outputs an XML file to a
I have to write an XML file using java.The contents should be written from
I am trying to write an XML file using MSXML4. It works fine except
I use SimpleXml to write XML file to internal storage in Android device. The
I write a xml like <app> <html_code> <div id=id-5 class=portlet last> <div class=portlet-header>Weather</div> <div
When I write code like this in VS 2008: .h struct Patterns { string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.