Java XML DOM from String stringToDOM(String xmlString)

Here you can find the source of stringToDOM(String xmlString)

Description

Transforms a string representation to a DOM representation

License

Apache License

Parameter

Parameter Description
xmlString XML as string

Exception

Parameter Description
ParserConfigurationException an exception
SAXException an exception
IOException an exception

Return

DOM representation of String

Declaration

public static Element stringToDOM(String xmlString)
        throws ParserConfigurationException, SAXException, IOException 

Method Source Code

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

import java.io.IOException;
import java.io.Reader;
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.w3c.dom.Element;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    /**/*from w w  w  .  j  a  v  a  2  s  .  com*/
     * Transforms a string representation to a DOM representation
     * @param xmlString XML as string
     * @return DOM representation of String
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Element stringToDOM(String xmlString)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);

        DocumentBuilder builder = dbf.newDocumentBuilder();

        Reader reader = new StringReader(xmlString);
        InputSource src = new InputSource(reader);
        Document domDoc = builder.parse(src);
        return domDoc.getDocumentElement();
    }
}

Related

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