Java XML Parse String parseXml(String xml)

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

Description

Returns a new document for the given XML string.

License

Open Source License

Parameter

Parameter Description
xml String that represents the XML data.

Return

Returns a new XML document.

Declaration

public static Document parseXml(String xml) 

Method Source Code

//package com.java2s;
import org.w3c.dom.Document;

import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import java.io.StringReader;

public class Main {
    /**/* w w w  .j  a  v  a2 s .  c  o m*/
     * Returns a new document for the given XML string.
     *
     * @param xml String that represents the XML data.
     * @return Returns a new XML document.
     */
    public static Document parseXml(String xml) {
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder = docBuilderFactory
                    .newDocumentBuilder();

            return docBuilder.parse(new InputSource(new StringReader(xml)));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. parseXml(String uri)
  2. parseXml(String xml)
  3. parseXML(String xml)
  4. parseXML(String xml)
  5. parseXml(String xml)
  6. parseXml(String xmlString)
  7. parseXMLResponse(String xmlStr)
  8. parseXMLString(final String xml)