Java XML Parse String parseXML(String xml)

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

Description

Parses the XML passed in as an argument and returns a Document object that represents the parsed XML

License

Open Source License

Parameter

Parameter Description
xml a parameter

Declaration

public static Document parseXML(String xml) throws SAXException 

Method Source Code


//package com.java2s;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

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

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    /**//  ww  w  . j a v a 2s . c om
     * Parses the XML passed in as an argument and returns a Document object
     * that represents the parsed XML
     *
     * @param xml
     * @return
     */
    public static Document parseXML(String xml) throws SAXException {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            // Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            // parse using builder to get DOM representation of the XML file
            Document domDoc = db.parse(new ByteArrayInputStream(xml.getBytes()));
            domDoc.normalize();

            return domDoc;

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // default for exceptions
        return null;
    }

    /**
     * Parses the XML stored in the given file and returns a Document object
     * that represents the parsed XML file
     *
     * @param xml
     * @return
     */
    public static Document parseXML(File file) {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            // Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            // parse using builder to get DOM representation of the XML file
            FileInputStream fis = new FileInputStream(file);
            Document domDoc = db.parse(fis);
            domDoc.normalize();
            fis.close();

            return domDoc;

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.err.println("File not found: " + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }

        // default for exceptions
        return null;
    }
}

Related

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