Creates a DOM from a string representation of an xml record - Java XML

Java examples for XML:DOM

Description

Creates a DOM from a string representation of an xml record

Demo Code


import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main{
    /** Creates a DOM from a string representation of an xml record
     * @param doc the xml string/*from  ww w.j a va2s  .  c  om*/
     * @return the DOM document
     */
    public static Document parseDomFromString(String doc)
            throws org.xml.sax.SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        try {
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(new org.xml.sax.InputSource(
                    new StringReader(doc)));
        } catch (javax.xml.parsers.ParserConfigurationException e) {
            throw new RuntimeException(e);
        } catch (java.io.IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials