Java XML Document Create stringToDocument(String string)

Here you can find the source of stringToDocument(String string)

Description

string To Document

License

Open Source License

Declaration

public static Document stringToDocument(String string)
            throws SAXException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Gabriel Skantze./*  ww  w .  j a va 2  s  . c om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Gabriel Skantze - initial API and implementation
 ******************************************************************************/

import java.io.ByteArrayInputStream;

import java.io.File;

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 {
    public static Document stringToDocument(String string)
            throws SAXException, IOException {
        return getBuilder().parse(
                new ByteArrayInputStream(string.getBytes()));
    }

    public static Document parse(File file) throws SAXException,
            IOException {
        return getBuilder().parse(file);
    }

    public static DocumentBuilder getBuilder() {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        try {
            return dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. string2Document(final String xml)
  2. string2Document(String xml, String encode)
  3. stringToDocument(final String xml)
  4. stringToDocument(String doc)
  5. stringToDocument(String string)
  6. stringToDocument(String string)
  7. stringToDocument(String xml)
  8. stringToDocument(String xmlStr, boolean isValidating)
  9. stringToDocument(String xmlString)