Load Xml Document from XML String - Android XML

Android examples for XML:XML Document

Description

Load Xml Document from XML String

Demo Code


//package com.java2s;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        String xml = "java2s.com";
        System.out.println(LoadXml(xml));
    }/*from   w ww. jav  a  2 s .c o m*/

    public static Document LoadXml(String xml) throws Exception {
        // create...
        ByteArrayInputStream stream = new ByteArrayInputStream(
                xml.getBytes());
        try {
            return LoadXml(stream);
        } finally {
            if (stream != null)
                stream.close();
        }
    }

    private static Document LoadXml(InputStream stream) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        factory.setNamespaceAware(true);

        // builder...
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(stream);

        // return...
        return doc;
    }
}

Related Tutorials