How do I load an org.w3c.dom.Document from XML in a string? How do I load an org.w3c.dom.Document from XML in a string? xml xml

How do I load an org.w3c.dom.Document from XML in a string?


Whoa there!

There's a potentially serious problem with this code, because it ignores the character encoding specified in the String (which is UTF-8 by default). When you call String.getBytes() the platform default encoding is used to encode Unicode characters to bytes. So, the parser may think it's getting UTF-8 data when in fact it's getting EBCDIC or something… not pretty!

Instead, use the parse method that takes an InputSource, which can be constructed with a Reader, like this:

import java.io.StringReader;import org.xml.sax.InputSource;…        return builder.parse(new InputSource(new StringReader(xml)));

It may not seem like a big deal, but ignorance of character encoding issues leads to insidious code rot akin to y2k.


This works for me in Java 1.5 - I stripped out specific exceptions for readability.

import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder;import org.w3c.dom.Document;import java.io.ByteArrayInputStream;public Document loadXMLFromString(String xml) throws Exception{    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();    factory.setNamespaceAware(true);    DocumentBuilder builder = factory.newDocumentBuilder();    return builder.parse(new ByteArrayInputStream(xml.getBytes()));}


Just had a similar problem, except i needed a NodeList and not a Document, here's what I came up with. It's mostly the same solution as before, augmented to get the root element down as a NodeList and using erickson's suggestion of using an InputSource instead for character encoding issues.

private String DOC_ROOT="root";String xml=getXmlString();Document xmlDoc=loadXMLFrom(xml);Element template=xmlDoc.getDocumentElement();NodeList nodes=xmlDoc.getElementsByTagName(DOC_ROOT);public static Document loadXMLFrom(String xml) throws Exception {        InputSource is= new InputSource(new StringReader(xml));        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        factory.setNamespaceAware(true);        DocumentBuilder builder = null;        builder = factory.newDocumentBuilder();        Document doc = builder.parse(is);        return doc;    }