Java XML DOM Parse parse_XML_String_and_create_DOM(String xmlData)

Here you can find the source of parse_XML_String_and_create_DOM(String xmlData)

Description

Parses XML data stored in a String and generates a DOM (Document Object Model) document that contains XML data

License

Open Source License

Parameter

Parameter Description
xmlData An string with XML content

Exception

Parameter Description
SAXException org.xml.sax.SAXException
IOException java.io.IOException
ParserConfigurationException javax.xml.parsers.ParserConfigurationException

Return

Document The generated XML document

Declaration

public static Document parse_XML_String_and_create_DOM(String xmlData)
        throws SAXException, IOException, ParserConfigurationException 

Method Source Code


//package com.java2s;
/* gvSIG. Geographic Information System of the Valencian Government
 *
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
 * of the Valencian Government (CIT)/*from  ww  w  .  ja  va 2 s .c o m*/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 * 
 */

import java.io.ByteArrayInputStream;

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 {
    /**
     * Parses XML data stored in a String and generates a DOM (Document Object
     * Model) document that contains XML data
     * 
     * @param xmlData
     *            An string with XML content
     * 
     * @return Document The generated XML document
     * 
     * @throws SAXException
     *             org.xml.sax.SAXException
     * @throws IOException
     *             java.io.IOException
     * @throws ParserConfigurationException
     *             javax.xml.parsers.ParserConfigurationException
     */
    public static Document parse_XML_String_and_create_DOM(String xmlData)
            throws SAXException, IOException, ParserConfigurationException {
        // Document builder factory instance. This builder allows create new
        // documents
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        // Instance of a DOM document builder, this object will allow to parse a
        // document
        DocumentBuilder DOM_document_Builder = factory.newDocumentBuilder();

        // Parses the input data and if it's correct returns a new document
        // (Document) object with that data
        return DOM_document_Builder.parse(new ByteArrayInputStream(xmlData.getBytes()));
    }
}

Related

  1. parseDom(InputStream byteArrayInputStream)
  2. parseDom(Reader reader)
  3. parseDOMConfigFile(String folderPath, String configFileName)
  4. parseDomFromFile(File doc)