encoding="UTF-8" being deleted when generating file from DOM

M

Mike Kennedy

I have an XML Snippet <?xml version="1.0" encoding="UTF-8"?> and when I
convert the entire xml file to a DOM and then generate a new file from
the DOM, results in <?xml version="1.0"?>.
Any thoughts?

My sample code snippet is:

DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(in.xml);
FileWriter file = new FileWriter(out.xml);

// Create an XMLInputStream using the DOM we've changed

XMLInputStreamFactory ifactory =
XMLInputStreamFactory.newInstance();
XMLInputStream istream = ifactory.newInputStream(doc);

// Create an XMLOutputStream to generate a new output file
// using the file created earlier

XMLOutputStreamFactory ofactory =
XMLOutputStreamFactory.newInstance();
XMLOutputStream output =
ofactory.newOutputStream(new PrintWriter(file,true));
output.add(istream);
output.flush();
output.close();
 
M

Martin Honnen

Mike said:
I have an XML Snippet <?xml version="1.0" encoding="UTF-8"?> and when I
convert the entire xml file to a DOM and then generate a new file from
the DOM, results in <?xml version="1.0"?>.
Any thoughts?

That is allowed, encoding UTF-8 or UTF-16 has to recognized by any XML
parser without the encoding being specified.
 
M

Mike Kennedy

Martin, thanks. But I'm wondering why the encoding="UTF-8" is being
stripped when the DOM is used to regenerate the xml. It's there in the
original xml file and then when I generate a new xml doc from the DOM,
it's not there.

--Mike
 
R

Richard Tobin

Mike Kennedy said:
Martin, thanks. But I'm wondering why the encoding="UTF-8" is being
stripped when the DOM is used to regenerate the xml.

Because it makes no difference to the document. Just as if you had
six spaces between attributes in the input, there's no reason to
suppose that there will be six spaces in the output.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top