views:

446

answers:

2

Hi, I am using StAX for creating XML files and then validating the file with and XSD.

I am getting an error while creating the XML file:

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)

Here is the code snippet:

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

I am running this code on Unix. Does anybody know how to set the version and encoding style?

A: 

I would try to use the createXMLStreamWriter() with an output parameter too.

[EDIT] Tried, it works by changing the createXMLStreamWriter line:

XMLStreamWriter xtw = xof.createXMLStreamWriter(new FileOutputStream(fileName), "UTF-8");

[EDIT 2] Made a little more complex test, for the record:

String fileName = "Test.xml";
XMLOutputFactory xof =  XMLOutputFactory.newInstance();
XMLStreamWriter xtw = null;
try
{
  xtw = xof.createXMLStreamWriter(new FileOutputStream(fileName), "UTF-8");
  xtw.writeStartDocument("UTF-8", "1.0");
  xtw.writeStartElement("root");
  xtw.writeComment("This is an attempt to create an XML file with StAX");

  xtw.writeStartElement("foo");
  xtw.writeAttribute("order", "1");
    xtw.writeStartElement("meuh");
    xtw.writeAttribute("active", "true");
      xtw.writeCharacters("The cows are flying high this Spring");
    xtw.writeEndElement();
  xtw.writeEndElement();

  xtw.writeStartElement("bar");
  xtw.writeAttribute("order", "2");
    xtw.writeStartElement("tcho");
    xtw.writeAttribute("kola", "K");
      xtw.writeCharacters("Content of tcho tag");
    xtw.writeEndElement();
  xtw.writeEndElement();

  xtw.writeEndElement();
  xtw.writeEndDocument();
}
catch (XMLStreamException e)
{
  e.printStackTrace();
}
catch (IOException ie)
{
  ie.printStackTrace();
}
finally
{
  if (xtw != null)
  {
    try
    {
      xtw.close();
    }
    catch (XMLStreamException e)
    {
      e.printStackTrace();
    }
  }
}
PhiLho
@ PhiLho - How to do that ?
Anurag
@Anurag: I think you should not put a space between @ and the user name: I was not notified of your question. Anyway, being curious, I tried my advice and found a working solution, see my edit.
PhiLho
@PhiLho: sorry for that. I am getting another error 'Prefix can not be null'. My schema does not use any prefixes. Is there any way to ignore this error. Because of this I am getting blank file.
Anurag
@PhiLho: I am trying in a same fashion. Still it gives me error Prefix cannot be null.
Anurag
@Anurag: I don't have any error with the little sample I show, wrapped in a simple class. I don't use a schema or anything else than shown.
PhiLho
@PhiLho: This is resolved by using a blank prefix as :xtw.setPrefix("", "http://www.w3.org/2001/XMLSchema-instance");
Anurag
Thanks all your effort you put in to help me out here. Thanks a lot !!
Anurag
A: 

This should work:

// ...
Writer writer = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
XMLStreamWriter xtw = xof.createXMLStreamWriter(writer);
xtw.writeStartDocument("UTF-8", "1.0");
// ...
chris