keybee
keybee

Reputation: 1496

xml utf-8 encoding error in java

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?

Upvotes: 2

Views: 1934

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109613

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.

Upvotes: 0

artbristol
artbristol

Reputation: 32437

You need to specify the encoding here

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

otherwise it uses the platform default

Upvotes: 3

Related Questions