Java XML String to Document toXMLDocument(String xmlString)

Here you can find the source of toXMLDocument(String xmlString)

Description

to XML Document

License

Open Source License

Declaration

public static Document toXMLDocument(String xmlString) 

Method Source Code


//package com.java2s;
/* -*- Java -*-/*from  w  ww.j a va  2 s.  c  o m*/
 *
 * Copyright (c) 2009
 * Spoken Language Systems Group
 * MIT Computer Science and Artificial Intelligence Laboratory
 * Massachusetts Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

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

import org.w3c.dom.Document;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    public static Document toXMLDocument(String xmlString) {
        return toXMLDocument(new InputSource(new StringReader(xmlString)));
    }

    public static Document toXMLDocument(InputStream in) {
        return toXMLDocument(new InputSource(in));
    }

    public static Document toXMLDocument(InputSource source) {
        Document xmlDoc = null;

        try {
            xmlDoc = getBuilder().parse(source);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return xmlDoc;
    }

    public static DocumentBuilder getBuilder() {
        try {
            return factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            return null;
        }
    }
}

Related

  1. toDocument(String str)
  2. toDocument(String string)
  3. toDocument(String xml)
  4. toDocument(String xml)
  5. toXmlDocument(final InputStream inputStream, final String path)
  6. writeDocumentToString(Document document)
  7. xml2Document(String xml)
  8. xmlNewDocument(final String tagName)
  9. xmlStringToDocument(String xml)