Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to set the Xml Encoding ISO-8859-1 to Transformer or DOMSource

843834Feb 6 2008 — edited Feb 13 2008
I have a xml string and it already contains an xml declaration with encoding="ISO-8859-1". (In my real product, since some of the element/attribute value contains a Spanish character, I need to use this encoding instead of UTF-8.) Also, in my program, I need to add more attributes or manipulate the xml string dynamically, so I create a DOM Document object for that. And, then, I use Transformer to convert this Document to a stream.
My problme is: Firstly, once converted through the Transformer, the xml encoding changed to default UTF-8, Secondly, I wanted to check whether the DOM Document created with the xml string maintains the Xml Encoding of ISO-8859-1 or not. So, I called Document.getXmlEncoding(), but it is throwing a runtime error - unknown method.
Is there any way I can maintain the original Xml Encoding of ISO-8859-1 when I use either the DOMSource or Transformer? I am using JDK1.5.0-12.
Following is my sample program you can use.
I would appreciate any help, because so far, I cannot find any answer to this using the JDK documentation at all.
Thanks,
Jin Kim
import java.io.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Attr;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

public class XmlEncodingTest
{
    StringBuffer xmlStrBuf = new StringBuffer();
    TransformerFactory tFactory = null;
    Transformer transformer = null;
    Document document = null;

    public void performTest()
    {
        xmlStrBuf.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n")
                 .append("<TESTXML>\n")
                 .append("<ELEM ATT1=\"Yes\" />\n")
                 .append("</TESTXML>\n");
        // the encoding is set to iso-8859-1 in the xml declaration.
        System.out.println("initial xml = \n" + xmlStrBuf.toString());

        try
        {
            //Test1: Use the transformer to ouput the xmlStrBuf.
            // This shows the xml encoding result from the transformer which will change to UTF-8
            tFactory = TransformerFactory.newInstance();
            transformer = tFactory.newTransformer();

            StreamSource ss = new StreamSource( new StringBufferInputStream( xmlStrBuf.toString()));
            System.out.println("Test1 result = ");
            transformer.transform( ss, new StreamResult(System.out));

            //Test2: Create a DOM document object for xmlStrBuf and manipulate it by adding an attribute ATT2="No"
            DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dfactory.newDocumentBuilder();
            document = builder.parse( new StringBufferInputStream( xmlStrBuf.toString()));
            // skip adding attribute since it does not affect the test result
            //...
            // Use a Transformer to output the DOM document. the encoding becomes UTF-8
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(System.out);
            System.out.println("\n\nTest2 result = ");
            transformer.transform(source, result);
        }
        catch (Exception e)
        {
            System.out.println("<performTest> Exception caught. " + e.toString());
        }

    }


    public static void main( String arg[])
    {
        XmlEncodingTest xmlTest = new XmlEncodingTest();
        xmlTest.performTest();
    }


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 12 2008
Added on Feb 6 2008
3 comments
5,712 views