Java XML Document Parse parseDocument(String documentString)

Here you can find the source of parseDocument(String documentString)

Description

Parses the given string to a DOM object.

License

Open Source License

Declaration

public static Document parseDocument(String documentString) 

Method Source Code


//package com.java2s;
/*/*from   w  w w  . j a  v a 2 s. co m*/
 * SafeOnline project.
 *
 * Copyright 2006-2007 Lin.k N.V. All rights reserved.
 * Lin.k N.V. proprietary/confidential. Use is subject to license terms.
 */

import java.io.*;
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 {
    /**
     * Parses the given string to a DOM object.
     */
    public static Document parseDocument(String documentString) {

        try {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
            StringReader stringReader = new StringReader(documentString);
            InputSource inputSource = new InputSource(stringReader);
            return domBuilder.parse(inputSource);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. parseDocument(InputStream is)
  2. parseDocument(Path xmlPath)
  3. parseDocument(Reader reader)
  4. parseDocument(String docText)
  5. parseDocument(String documentString)
  6. parseDocument(String path)
  7. parseDocument(String xmlString)
  8. parseDocumentIntoStringXML(Document document)
  9. parseDocumentr(File f)