Create Xml Document from XML String - Java XML

Java examples for XML:XML Document

Description

Create Xml Document from XML String

Demo Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

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

import org.w3c.dom.Document;

public class Main {
    public static void main(String[] argv) {
        String text = "<a></a>";
        System.out.println(stringToXmlDoc(text));
    }//from   ww  w .  j  a v  a2 s.com

    public static Document stringToXmlDoc(String text) {
        Document document = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputStream inputStream = new ByteArrayInputStream(
                    text.getBytes("UTF-8"));
            document = builder.parse(inputStream);
        } catch (Exception e) {
        }
        return document;
    }
}

Related Tutorials