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)
            throws ParserConfigurationException, SAXException, IOException 

Method Source Code

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

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    public static Document getDocumentFromString(String xml)
            throws ParserConfigurationException, SAXException, IOException {

        InputSource source = new InputSource(new StringReader(xml));

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringElementContentWhitespace(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(source);
        document.normalize();//from   w  w  w.ja  va  2  s  . c o  m

        return document;
    }
}

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)