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 org.w3c.dom.Document;
import org.w3c.dom.Element;

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

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

public class Main {
    public static Element getRoot(String xml, String charset) {
        try {//from ww  w  . jav  a  2  s  .  c  o 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("XML invalido");
            }
            return root;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

Related

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