2

The code looks like this:

XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

        XMLEventWriter eventWriter = outputFactory
                .createXMLEventWriter(new FileOutputStream("output.xml"));

        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent end = eventFactory.createDTD("\n");
        XMLEvent tab = eventFactory.createDTD("\t");

        StartDocument startDocument = eventFactory.createStartDocument(
                "UTF-8", "1.0");
        eventWriter.add(startDocument);
...

When I open the xml file, google chrome says "encoding error" at the first utf-8 character, and if I look the xml code, it looks like:

?xml version="1.0"?

there is no encoding part in it...

Do you have any ideas what could be the problem?

3
  • UTF-8 is the default; it doesn't need to be specified. How are you opening the file in Chrome? (Locally, or via a web server? Perhaps the server is messing things up?) Commented Jan 18, 2013 at 15:13
  • A missing encoding="..." defaults to UTF-8. So no worry. You might however look whether there is a BOM character as first character of the file, this is a (generally deprecated) Unicode zero width space. You will need a tool for seeing it. You could try deleting beginning chars. Commented Jan 18, 2013 at 15:14
  • I was not using a web server. Now this problem got solved, so I can focus on the others :) thanks for the fast replies! Commented Jan 18, 2013 at 15:28

2 Answers 2

3

You need to specify the encoding here

.createXMLEventWriter(new FileOutputStream("output.xml"), "UTF-8");

otherwise it uses the platform default

Sign up to request clarification or add additional context in comments.

Comments

0

To my understanding

XMLEvent end = eventFactory.createDTD("\n");

is probably not what you meant. A DTD is a text declaring entities and tags; XML in a non-XML script. Try removing them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.