Java XML Parse String parseXml(Object obj)

Here you can find the source of parseXml(Object obj)

Description

parse Xml

License

Open Source License

Parameter

Parameter Description
obj ...

Exception

Parameter Description
SAXException ...
IOException ...
ParserConfigurationException ...

Return

...

Declaration

public static Document parseXml(Object obj) throws SAXException, IOException, ParserConfigurationException 

Method Source Code

//package com.java2s;
/*/*from   ww w. ja v a 2  s  .c  o  m*/
 * Helma License Notice
 *
 * The contents of this file are subject to the Helma License
 * Version 2.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://adele.helma.org/download/helma/license.txt
 *
 * Copyright 1998-2003 Helma Software. All Rights Reserved.
 *
 * $RCSfile: XmlUtils.java,v $
 * $Author: hannes $
 * $Revision: 1.4 $
 * $Date: 2003/10/22 16:34:06 $
 */

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;

import java.io.Reader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class Main {
    private static DocumentBuilderFactory domBuilderFactory = null;

    /**
     *
     *
     * @param obj ...
     *
     * @return ...
     *
     * @throws SAXException ...
     * @throws IOException ...
     * @throws ParserConfigurationException ...
     */
    public static Document parseXml(Object obj) throws SAXException, IOException, ParserConfigurationException {
        if (domBuilderFactory == null) {
            domBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
        }

        DocumentBuilder parser = domBuilderFactory.newDocumentBuilder();
        Document doc = null;

        if (obj instanceof String) {
            try {
                // first try to interpret string as URL
                new URL(obj.toString());

                doc = parser.parse(obj.toString());
            } catch (MalformedURLException nourl) {
                // if not a URL, maybe it is the XML itself
                doc = parser.parse(new InputSource(new StringReader(obj.toString())));
            }
        } else if (obj instanceof InputStream) {
            doc = parser.parse(new InputSource((InputStream) obj));
        } else if (obj instanceof Reader) {
            doc = parser.parse(new InputSource((Reader) obj));
        }

        doc.normalize();

        return doc;
    }
}

Related

  1. parseArgumentString(String argStr)
  2. parseArgumentString(String command)
  3. parseArray(String array)
  4. parseArray(String array)
  5. parseArray(String inputArray)
  6. parseXML(String path)
  7. parseXML(String pathToMap)
  8. parseXML(String resp, String name)
  9. parseXML(String text)