Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do I add the XML Version and Encoding Declaration to XML?

Reply
Thread Tools

How do I add the XML Version and Encoding Declaration to XML?

 
 
eric.jester@gmail.com
Guest
Posts: n/a
 
      04-21-2005
Hello,

I a org.w3c.dom.Document that does not have the <?xml version="1"
encoding="UTF-8"?> declaration on it. Is there a way I can add this to
it?

Thanks,

Eric

 
Reply With Quote
 
 
 
 
Michael Berg
Guest
Posts: n/a
 
      04-22-2005
Hi,

> I a org.w3c.dom.Document that does not have the <?xml version="1"
> encoding="UTF-8"?> declaration on it. Is there a way I can add this to
> it?


What are you using to serialize the Document to XML? In my experience using
the Transformer class from Xerces outputs an XML document that is
appropriately formatted:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// I have to read this stuff manually from time to time so make it pretty
transformer.setOutputProperty(OutputKeys.INDENT,"y es");

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4
");
transformer.transform(new DOMSource(xml),result); // xml is your Document
instance
...
xmlstring = result.toString();

'Result' having been previously created as a StreamResult object - for
example like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult result = new StreamResult(
new java.io.OutputStreamWriter(
baos,
"UTF-8"
)
);

But of course there are many other ways to do this. Your mileage may vary.

/Michael
www.hyperpal.com
www.bergconsult.com



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Where to get stand alone Dot Net Framework version 1.1, version2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? MowGreen [MVP] ASP .Net 5 02-09-2008 01:55 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 AM
Character conversion error: "Malformed UTF-8 char -- is an XML encoding declaration missing?" (line number may be too low). alcor@fibertel.com.ar Java 0 04-03-2006 01:19 PM
"encoding specified in XML declaration is incorrect" Gustaf Liljegren Python 1 12-02-2004 09:20 PM



Advertisments