Create DOM Document out of string : DOM Document « XML « Java






Create DOM Document out of string

    




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;


public class Main {

  public static Document load(String xml) throws Exception {
    DocumentBuilder builder = getDocumentBuilder();         
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    return document;
}
  public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    return builder;
}

}

   
    
    
    
  








Related examples in the same category

1.Copy an XML document
2.Create Document with root QName
3.Create Empty DOM Document
4.Displays a DOM document in a tree control.
5.New Document From InputStream
6.New Document From String
7.load Document by element
8.load Document from InputStream
9.get Document Element from a file
10.Start a new XML Document
11.Document To String
12.Utility class to print out DOM
13.Return a new document, ready to populate
14.Read Xml from InputStream and return Document
15.Read Xml from Reader and return Document
16.Gets the owner document of a node.
17.Creates an element on the given document.
18.Loads a W3C XML document from a file.
19.Your Own XML Reader