Bugs item #1156909, was opened at 2005-03-04 20:50
Message generated for change (Comment added) made by maartenc
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=116035&aid=1156909&group_id=16035

Category: None
Group: None
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
>Assigned to: Maarten Coene (maartenc)
Summary: Buggy Document.getXMLEncoding / Document.asXML

Initial Comment:
Try this:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;


public class TestDom4J
{
    public static void main(String[] args) throws
Exception {
        String xml = "<?xml version='1.0'
encoding='iso-8859-1'?><Message>Hi
there</Message>";
        Document doc = DocumentHelper.parseText (xml);
        System.out.println ("The encoding is " +
doc.getXMLEncoding ());
        System.out.println ("As XML: " + doc.asXML ());
    }
}

The result is:

The encoding is null
As XML: <?xml version="1.0" encoding="UTF-8"?>
<Message>Hi there</Message>


Solution:

Step 1: add setXMLEncoding to Interface org.dom4j.Document

Step 2: change DocumentHelper.parseText (String) to

public static Document parseText(String text) throws
DocumentException {
        SAXReader reader = new SAXReader();
        String encoding = getEncoding(text);
        
        InputSource source = new InputSource(new
StringReader(text));
        source.setEncoding(encoding);
        Document doc = reader.read(source);
        doc.setXMLEncoding (encoding);

        return doc;
    }


----------------------------------------------------------------------

>Comment By: Maarten Coene (maartenc)
Date: 2005-03-28 17:05

Message:
Logged In: YES 
user_id=178745

Fixed in CVS now:
- removed the static outputFormat (good catch!)
- added a setXMLEncoding method to the Document interface
- set the encoding manually if the parser doesn't support a
way to retrieve it (like Crimson)

thanks,
Maarten

----------------------------------------------------------------------

Comment By: John Plaxton (jplaxton)
Date: 2005-03-23 09:19

Message:
Logged In: YES 
user_id=1029543

I would agree with above addition to the Document interface 
of the setXMLEncoding() method. But you also need to 
change the line in org.dom4j.tree.AbstractBranch that states:

protected static final OutputFormat outputFormat = new 
OutputFormat();

Its not thread safe!!! It shouldn't be a static variable! This 
causes strange behaviour for the asXML() method and 
multiple threads.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=116035&aid=1156909&group_id=16035


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
dom4j-dev mailing list
dom4j-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-dev

Reply via email to