Bugs item #1156909, was opened at 2005-03-04 19:50 Message generated for change (Comment added) made by jplaxton You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=116035&aid=1156909&group_id=16035
Category: None Group: None Status: Open Resolution: None Priority: 5 Submitted By: Nobody/Anonymous (nobody) Assigned to: Nobody/Anonymous (nobody) 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: John Plaxton (jplaxton) Date: 2005-03-23 08: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 ------------------------------------------------------- This SF.net email is sponsored by: 2005 Windows Mobile Application Contest Submit applications for Windows Mobile(tm)-based Pocket PCs or Smartphones for the chance to win $25,000 and application distribution. Enter today at http://ads.osdn.com/?ad_id=6882&alloc_id=15148&op=click _______________________________________________ dom4j-dev mailing list dom4j-dev@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/dom4j-dev