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

DocumenttoXMLDocument(String xmlString)
to XML Document
return toXMLDocument(new InputSource(new StringReader(xmlString)));
StringwriteDocumentToString(Document document)
write Document To String
try {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans;
    trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter writer = new StringWriter();
...
Documentxml2Document(String xml)
xml Document
try {
    DocumentBuilder builder = DOCUMENT_BUILDER_FACTORY.get().newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(xml.getBytes(DEFAULT_CHARSET)));
} catch (ParserConfigurationException ex) {
    throw new RuntimeException(ex);
} catch (SAXException ex) {
    throw new RuntimeException(ex);
} catch (UnsupportedEncodingException ex) {
...
ElementxmlNewDocument(final String tagName)
Xml new document.
final Document doc = XML_BUILDER.get().newDocument();
final Element tag = doc.createElement(tagName);
return tag;
DocumentxmlStringToDocument(String xml)
Converts an XML string into an equivalent XML document.
return DocumentBuilderFactory.newInstance().newDocumentBuilder()
        .parse(new InputSource(new StringReader(xml)));
DocumentxmlStringToDocument(String xmlInputString)
xml String To Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document dom = null;
try {
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource input = new InputSource(new StringReader(xmlInputString));
    dom = db.parse(input); 
} catch (ParserConfigurationException pce) {
    pce.printStackTrace();
...