Java XML Document from String getDocumentFromString(String xml)

Here you can find the source of getDocumentFromString(String xml)

Description

get Document From String

License

Open Source License

Declaration

public static Document getDocumentFromString(String xml) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.xml.sax.*;

import org.w3c.dom.Document;

public class Main {
    public static Document getDocumentFromString(String xml) {
        Document retVal = null;/*from   w w w .j a v  a2 s.  c o m*/
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            retVal = db.parse(getInputSourceFromString(xml));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return retVal;
    }

    /**
     * Forms an <code>InputSource</code> for parsing XML documents
     * from a string. Returns <code>null</code> if any of the exceptions
     * are thrown.
     * 
     * @param xml String with the XML document.
     * @return An <code>InputSource</code> representation.
     */
    public static InputSource getInputSourceFromString(String xml) {
        InputSource retVal = null;
        try {
            retVal = new InputSource(new StringReader(xml));
        } catch (Exception ex) {
        }
        return retVal;
    }
}

Related

  1. getDocumentFromBytes(byte[] data)
  2. getDocumentFromString(String text)
  3. getDocumentFromString(String xml)
  4. getDocumentFromString(String xml)
  5. getDocumentFromString(String xmlString)
  6. getDocumentFromString(String xmlString)
  7. getXmlDocument(final String container)