Java XML Element Root getRootTagName(String xml)

Here you can find the source of getRootTagName(String xml)

Description

Returns root tagname

License

Open Source License

Parameter

Parameter Description
xml The xml document to be searched.

Return

String result.

Declaration

public static String getRootTagName(String xml) throws Exception 

Method Source Code

//package com.java2s;
/*/*from   w ww  . java 2s .  c  o  m*/
 * The following methods come from a library written by Tom Fennelly.
 * Here was the header of the licence.
 */

import java.io.StringReader;

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

import org.w3c.dom.*;

import org.xml.sax.InputSource;

public class Main {
    /**
     * Returns root tagname
     *
     * @param xml  The xml document to be searched.
     * @return String result.
     */
    public static String getRootTagName(String xml) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder builder = dbf.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xml)));
        return document.getDocumentElement().getTagName();
    }
}

Related

  1. getRootFromPom(File pomFile)
  2. getRootFromString(String str)
  3. getRootNode(Document doc, String nodeName)
  4. getRootNode(Document document)
  5. getRootNode(final String xmlContent)