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

I have a full XML document in a string and also would certainly such as a Document object. Google shows up all type of waste. What is the most basic remedy? (In Java 1.5)

Solution Thanks to Matt McMinn, I have actually decided on this execution. It has the appropriate degree of input adaptability and also exemption granularity for me. (It is excellent to recognize if the mistake originated from misshapen XML - SAXException - or simply negative IO - IOException.)

public static org.w3c.dom.Document loadXMLFrom(String xml)
throws org.xml.sax.SAXException, java.io.IOException {
}

throws org.xml.sax.SAXException, java.io.IOException {
javax.xml.parsers.DocumentBuilderFactory factory =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
javax.xml.parsers.DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
}
catch (javax.xml.parsers.ParserConfigurationException ex) {
}
org.w3c.dom.Document doc = builder.parse(is);
is.close();
return doc;
}

107
2022-06-06 19:58:36
Source Share

Just had a comparable trouble, other than i required a NodeList and also not a Document, below is what I thought of. It is primarily the very same remedy as in the past, boosted to get the origin component down as a NodeList and also making use of erickson is pointer of making use of an InputSource rather for personality inscribing concerns.

private String DOC_ROOT="root";
String xml=getXmlString();
Element template=xmlDoc.getDocumentElement();
NodeList nodes=xmlDoc.getElementsByTagName(DOC_ROOT);

public static Document loadXMLFrom(String xml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
return doc;
}

9
2022-06-08 06:31:50
Source

Whoa there!

There is a possibly significant trouble with this code, due to the fact that it overlooks the personality inscribing defined in the String (which is UTF - 8 by default). When you call String.getBytes() the system default encoding is made use of to inscribe Unicode personalities to bytes. So, the parser might assume it is obtaining UTF - 8 information when actually it is obtaining EBCDIC or something not rather!

Rather, make use of the parse method that takes an InputSource, which can be created with a Reader, similar to this:

import java.io.StringReader;
import org.xml.sax.InputSource;
…


It might not feel like a large bargain, yet lack of knowledge of personality inscribing concerns brings about dangerous code rot comparable to y2k.

155
2022-06-06 21:08:01
Source

This benefits me in Java 1.5 - I removed out details exemptions 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()));
}

88
2022-06-06 20:11:06
Source