Java XML String to Document xmlStringToDocument(String xmlInputString)

Here you can find the source of xmlStringToDocument(String xmlInputString)

Description

xml String To Document

License

Open Source License

Declaration

public static Document xmlStringToDocument(String xmlInputString) 

Method Source Code

//package com.java2s;
/****************************************************
Statistics Online Computational Resource (SOCR)
http://www.StatisticsResource.org//from  ww w .  j  a  v  a 2 s .  c o  m
     
All SOCR programs, materials, tools and resources are developed by and freely disseminated to the entire community.
Users may revise, extend, redistribute, modify under the terms of the Lesser GNU General Public License
as published by the Open Source Initiative http://opensource.org/licenses/. All efforts should be made to develop and distribute
factually correct, useful, portable and extensible resource all available in all digital formats for free over the Internet.
     
SOCR resources are distributed in the hope that they will be useful, but without
any warranty; without any explicit, implicit or implied warranty for merchantability or
fitness for a particular purpose. See the GNU Lesser General Public License for
more details see http://opensource.org/licenses/lgpl-license.php.
     
http://www.SOCR.ucla.edu
http://wiki.stat.ucla.edu/socr
 It s Online, Therefore, It Exists! 
****************************************************/

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;
import org.xml.sax.InputSource;
import java.io.StringReader;

public class Main {
    public static Document xmlStringToDocument(String xmlInputString) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document dom = null;
        try {
            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();
            //Reader in = new BufferedReader(new FileReader(args[0]));
            InputSource input = new InputSource(new StringReader(xmlInputString));

            // Tell the Xerces parser to parse the input source
            //db.parse(input);
            //parse using builder to get DOM representation of the XML file
            //System.out.println("Linear Model fileName = " + fileName);
            //dom = db.parse(fileName); // use an input file of XML.
            dom = db.parse(input); // use String declared at the top.

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return dom;
    }
}

Related

  1. toXMLDocument(String xmlString)
  2. writeDocumentToString(Document document)
  3. xml2Document(String xml)
  4. xmlNewDocument(final String tagName)
  5. xmlStringToDocument(String xml)