Java Utililty Methods XML String to Document

List of utility methods to do XML String to Document

Description

The list of methods to do XML String to Document are organized into topic(s).

Method

DocumentstrToDocument(final String strXml)
str To Document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(strXml)));
} catch (SAXException e) {
    e.printStackTrace();
} catch (IOException e) {
...
ElementstrToXML(final Document doc, final String namespaceURI, final String qname, final String str)
returns XML representation of a string, with specified XML node name
if (str == null ) {
    return null;
final Element xml = doc.createElementNS(namespaceURI, qname);
xml.appendChild(doc.createTextNode(str));
return xml;
DocumenttoDocument(String content)
to Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
return content == null ? null : db.parse(new InputSource(new StringReader(content)));
DocumenttoDocument(String input)
to Document
InputStream is = new ByteArrayInputStream(input.getBytes("UTF-8"));
Document document = toDocument(is);
return document;
DocumenttoDocument(String s)
to Document
return (toDocument(s.getBytes()));
DocumenttoDocument(String str)
Transform string to XML document.
return builders.get().parse(new ByteArrayInputStream(str.getBytes("UTF-8")));
DocumenttoDocument(String string)
Parses XML document
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(string)));
DocumenttoDocument(String xml)
to Document
return toDocument(xml, false);
DocumenttoDocument(String xml)
Method reads in the xml and returns the top level document.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource source = new InputSource(new StringReader(xml));
return builder.parse(source);
DocumenttoXmlDocument(final InputStream inputStream, final String path)
to Xml Document
try {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    return documentBuilder.parse(inputStream);
} catch (final ParserConfigurationException ex) {
    throw new RuntimeException("Unable to read classpath resource: " + path, ex);
} catch (final SAXException ex) {
    throw new RuntimeException("Unable to read classpath resource: " + path, ex);
...