Problem with Java XML charset

Discussion in 'Java' started by Erik A. Brandstadmoen, Dec 28, 2004.

  1. I'm having problems building a DOM document with java using a ny other
    character set than utf-8.

    The code snippet at the further down produces the following output. Why?
    I explicitly produce an XML document using iso8859-1 charset, so why
    does Java suddenly think I want utf-8 instead?

    Is there any way to tell the DocumentBuilder classes that I want to work
    with a character set different from utf-8? I haven't been able to figure
    out.

    Thanks in advance.

    Erik B.

    $ java TestXMLEncoding
    Result: <?xml version="1.0" encoding="utf-8"?><definisjonsark/>
    $


    Hvorfor og hvor og hva er det som forandrer tegnsettet?

    Erik.


    Code :

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.StringWriter;

    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;

    import org.w3c.dom.Document;
    import org.xml.sax.SAXException;

    /*
    * Created on Dec 23, 2004
    *
    * TODO To change the template for this generated file go to
    * Window - Preferences - Java - Code Style - Code Templates
    */

    /**
    * @author mfreab
    *
    * TODO To change the template for this generated type comment go to
    * Window - Preferences - Java - Code Style - Code Templates
    */
    public class TestXMLEncoding {

    public static void main(String[] args)
    throws IOException,
    SAXException,
    ParserConfigurationException {

    TestXMLEncoding test = new TestXMLEncoding();

    test.testXML();

    }

    public TestXMLEncoding() {

    }

    public void testXML() throws IOException, SAXException,
    ParserConfigurationException {

    Document xmlDocument = null;


    DocumentBuilderFactory factory =
    DocumentBuilderFactory.newInstance();
    //factory.setValidating(true);
    factory.setNamespaceAware(true);

    final DocumentBuilder builder = factory.newDocumentBuilder();




    System.out.println("Result: " + this.getAsString(xmlDocument));

    }


    private String getAsString(Document doc) {

    String retVal = null;

    try {
    // Prepare the DOM docuemnt for writing
    Source source = new DOMSource(doc);

    // Prepare the result string
    StringWriter w = new StringWriter();
    Result result = new StreamResult(w);

    // Write the DOM document to file
    Transformer xformer =
    TransformerFactory.newInstance().newTransformer();
    xformer.transform(source,result);

    retVal = w.getBuffer().toString();

    } catch (TransformerConfigurationException tce) {
    tce.printStackTrace();
    } catch (TransformerException te) {
    te.printStackTrace();
    }

    return retVal;
    }


    }
     
    Erik A. Brandstadmoen, Dec 28, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    573
    Guest
    Feb 20, 2004
  2. Damon Payne
    Replies:
    0
    Views:
    9,200
    Damon Payne
    Mar 8, 2005
  3. Replies:
    0
    Views:
    3,416
  4. Replies:
    2
    Views:
    1,016
    George Bina
    May 24, 2006
  5. optimistx

    javascript charset <> page charset

    optimistx, Aug 14, 2008, in forum: Javascript
    Replies:
    2
    Views:
    285
    optimistx
    Aug 15, 2008
Loading...

Share This Page