views:

11741

answers:

4

I have a complete XML document in a string and would like a Document object. Google turns up all sorts of garbage. What is the simplest solution? (In Java 1.5)

Solution Thanks to Matt McMinn, I have settled on this implementation. It has the right level of input flexibility and exception granularity for me. (It's good to know if the error came from malformed XML - SAXException - or just bad IO - IOException.)

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

public static org.w3c.dom.Document loadXMLFrom(java.io.InputStream is) 
    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;
}
+7  A: 

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()));
}
Matt McMinn
As noted in sylvarking's answer, this code uses `getBytes()` with no consideration for encoding.
McDowell
+12  A: 
erickson
+1  A: 

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;
    }
shsteimer
A: 

Server com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl cannot be cast to org.w3c.dom.Element A serious technical exception has occurred while processing the request. An internal UDDI server error has occurred. Please report this error to the UDDI server administrator.

this is the error message i got when submitting my xml document for get authorisation token for my JUDDI Registry,iam using jdl1.6.0.18,tomcat 6.0.24,juddi0.9rc4,MySQL 1.5.44 win64,MySQL-connector-java-5.1.12,eclipse SDK 3.2.1 win32-x86-64 all 64bit for on my windows vista.

radha
Welcome radha,I am having trouble understanding this, but it seems like a new question. Check out the faq for more info on how Stack Overflow works: http://stackoverflow.com/faq
Adam