Java XML Parse String parseXml(String xml)

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

Description

parse Xml

License

LGPL

Declaration

public static Document parseXml(String xml) 

Method Source Code


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

import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.ByteArrayInputStream;

public class Main {
    public static Document parseXml(String xml) {
        try {/*from   w w  w  .  j a  v  a 2 s .  c  om*/
            DocumentBuilder dBuilder = getDocumentBuilder();
            return dBuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        setUpSecurity(dbFactory);
        return dbFactory.newDocumentBuilder();
    }

    private static void setUpSecurity(DocumentBuilderFactory dbFactory) throws ParserConfigurationException {
        dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        dbFactory.setXIncludeAware(false);
        dbFactory.setExpandEntityReferences(false);
    }
}

Related

  1. parseXML(String path)
  2. parseXML(String pathToMap)
  3. parseXML(String resp, String name)
  4. parseXML(String text)
  5. parseXml(String uri)
  6. parseXML(String xml)
  7. parseXML(String xml)
  8. parseXml(String xml)
  9. parseXml(String xml)