Java XML String to Document toDocument(String input)

Here you can find the source of toDocument(String input)

Description

to Document

License

Apache License

Declaration

public static Document toDocument(String input) throws SAXException, IOException, ParserConfigurationException 

Method Source Code


//package com.java2s;
/*//  ww w.j a va  2  s  . c o m
    
   Copyright 2011 Udayakumar Dhansingh (Udy)
    
    
    
   Licensed under the Apache License, Version 2.0 (the "License");
    
   you may not use this file except in compliance with the License.
    
   You may obtain a copy of the License at
    
    
    
   http://www.apache.org/licenses/LICENSE-2.0
    
    
    
   Unless required by applicable law or agreed to in writing, software
    
   distributed under the License is distributed on an "AS IS" BASIS,
    
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    
   See the License for the specific language governing permissions and
    
   limitations under the License.
    
       
    
 */

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

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 toDocument(String input) throws SAXException, IOException, ParserConfigurationException {
        InputStream is = new ByteArrayInputStream(input.getBytes("UTF-8"));
        Document document = toDocument(is);
        return document;
    }

    public static Document toDocument(InputStream is)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true); // NOTE: never forget this

        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(is);

        is.close();
        return document;
    }
}

Related

  1. strToDocument(final String strXml)
  2. strToXML(final Document doc, final String namespaceURI, final String qname, final String str)
  3. toDocument(String content)
  4. toDocument(String s)
  5. toDocument(String str)
  6. toDocument(String string)
  7. toDocument(String xml)