I agree with aperkins and here is my javax helper:
/**
* Returns a {@code Document} from the specified XML {@code String}.
*
* @param xmlDocumentString a well-formed XML {@code String}
* @return a {@code org.w3c.dom.Document}
*/
public static Document getDomDocument(String xmlDocumentString)
{
if(StringUtility.isNullOrEmpty(xmlDocumentString)) return null;
InputStream s = null;
try
{
s = new ByteArrayInputStream(xmlDocumentString.getBytes("UTF-8"));
}
catch(UnsupportedEncodingException e)
{
throw new RuntimeException("UnsupportedEncodingException: " + e.getMessage());
}
return XmlDomUtility.getDomDocument(s);
}
This helper depends on another one:
/**
* Returns a {@code Document} from the specified {@code InputStream}.
*
* @param input the {@code java.io.InputStream}
* @return a {@code org.w3c.dom.Document}
*/
public static Document getDomDocument(InputStream input)
{
Document document = null;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(input);
}
catch(ParserConfigurationException e)
{
throw new RuntimeException("ParserConfigurationException: " + e.getMessage());
}
catch(SAXException e)
{
throw new RuntimeException("SAXException: " + e.getMessage());
}
catch(IOException e)
{
throw new RuntimeException("IOException: " + e.getMessage());
}
return document;
}
Update: these are my imports:
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;