Android XML Element Create getRoot(String xml, String charset)

Here you can find the source of getRoot(String xml, String charset)

Description

get Root

Declaration

public static Element getRoot(String xml, String charset) 

Method Source 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;

import org.w3c.dom.Element;

public class Main {
    public static Element getRoot(String xml, String charset) {
        try {/* ww  w  . ja  v  a 2 s. co  m*/
            InputStream in = null;
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            factory.setValidating(false);
            byte[] bytes = xml.getBytes(charset);
            in = new ByteArrayInputStream(bytes);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(in);
            Element root = dom.getDocumentElement();

            if (root == null) {
                throw new RuntimeException("Invalid XML");
            }

            return root;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

Related

  1. getRoot(String xml, String charset)
  2. getRoot(String xml, String charset)