Java XML Document Parse parseXMLDocument(String xml)

Here you can find the source of parseXMLDocument(String xml)

Description

Parse an XML string into a document.

License

LGPL

Parameter

Parameter Description
xml The xml string fragment

Return

The parsed XML

Declaration

public static Document parseXMLDocument(String xml) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 *                        Copyright Yumetech, Inc (c) 2006
 *                               Java Source
 *
 * This source is licensed under the GNU LGPL v2.1
 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
 *
 * This software comes with the standard NO WARRANTY disclaimer for any
 * purpose. Use it at your own risk. If there's a problem you get to fix it.
 *
 ****************************************************************************/

import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.parsers.*;

public class Main {
    /**/* w  w  w . j  a  va 2s  . com*/
     * Parse an XML string into a document.
     *
     * @param xml The xml string fragment
     * @return The parsed XML
     */
    public static Document parseXMLDocument(String xml) {
        StringReader sr = new StringReader(xml);

        DocumentBuilderFactory builderFactory;
        DocumentBuilder builder;

        try {
            builderFactory = DocumentBuilderFactory.newInstance();
            builder = builderFactory.newDocumentBuilder();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            Document document = builder.parse(new InputSource(sr));
            return document;
        } catch (FileNotFoundException fnfe) {
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. parseResponse(InputSource is, DocumentBuilder documentBuilder)
  2. parseToDocument(InputStream is, boolean debugModeEnabled)
  3. parseXMLDoc(Element element, Document doc, Map oauthResponse)
  4. parseXmlDocToString(Document xmlDoc)
  5. parseXmlDocument(InputStream is, boolean namespaceAware)
  6. parseXmlDocument(String xml, boolean namespaceAware)
  7. parseXMLDocument(String xmlDoc)
  8. parseXMLDocument(String xmlResponse)
  9. parseXmlFragmentStr(Document doc, String fragment)