By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

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

P: n/a
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();

Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Mike Kennedy wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

P: n/a
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

Martin Honnen wrote:


Mike Kennedy wrote:
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.


Jul 20 '05 #3

P: n/a
In article <ZK******************@news2.news.adelphia.net>,
Mike Kennedy <mi**@cosx.com> wrote:
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
--
Spam filter: to mail me from a .com/.net site, put my surname in the headers.

FreeBSD rules!
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.