Java XML DOM from String string2Dom(String xml)

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

Description

string Dom

License

Apache License

Declaration

public static Document string2Dom(String xml) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;

public class Main {
    public static Document string2Dom(String xml) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {//from  ww  w.j av a2 s  .  c om
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(string2InputSource(xml));
            return doc;
        } catch (Exception e) {
            System.out.println("caught exception while parsing: " + e);
            return null;
        }

    }

    public static InputSource string2InputSource(String xml) {
        return new InputSource(new StringReader(xml));
    }
}

Related

  1. string2Dom(final String fileContent)
  2. stringToDOM(String html)
  3. stringToDom(String xmlSource)
  4. stringToDOM(String xmlString)
  5. stringToDOM(String xmlString)