Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Can I change the "encoding" attrib. of a org.w3c.dom.Document?

  Asked By: Jaxson    Date: Jan 19    Category: Java    Views: 7058
  

I am building a XML document this way:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.newDocument();

Element root = doc.createElement("ResultSet");
doc.appendChild(root);
...

Then I'm using the following code to "spit" my document into a web
page (this is a servlet):

DOMSource domSourceXml = new DOMSource(doc);
TransformerFactory tf = TransformerFactory.newInstance();
t = tf.newTransformer();
StreamResult streamResultHtml = new StreamResult(res.getWriter());
t.transform(domSourceXml, streamResultHtml);

where "res" is a HttpServletResponse.

The web page built starts with:

<?xml version="1.0" encoding="UTF-8" ?>
...

but I want to change this "encoding" attrib. to "ISO-8859-1".

Does anybody know how to do that?

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Fabia Ferrrari     Answered On: Jan 19

I have used the the IBM XML parser and the code  to set
the encoding  type is this
((TXDocument)gvObjDoc).setEncoding("US-ASCII");

gvObjDoc = Object of type Document
When I use the Xerces Parser I use the following
OutputFormat format = new OutputFormat( doc  ,
"UTF-8", true); //Serialize DOM
StringWriter stringOut = new StringWriter();//Writer
will be a String
XMLSerializer serial = new XMLSerializer( stringOut,
format );


What parser are you using ??

 
Answer #2    Answered By: Anuja Shah     Answered On: Jan 19

Sun's XML parser................................

 
Answer #3    Answered By: Emma Campbell     Answered On: Jan 19

Problem solved!

Here's the code:

TransformerFactory tf = TransformerFactory.newInstance();
t = tf.newTransformer();
t.setOutputProperty("encoding", "ISO-8859-1");

 




Tagged: