static public String getRootNodeName(String xml)
    {
        String rootNodeName = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setExpandEntityReferences(false);
            DocumentBuilder dBuilder = dbf.newDocumentBuilder();
            InputSource inputSource = new InputSource(new StringReader(xml));
            Document doc = dBuilder.parse(inputSource);
            doc.getDocumentElement().normalize();
            rootNodeName = doc.getDocumentElement().getNodeName();
        } catch (SAXException e) {
            System.out.println("SAX exception: " + e);
        } catch (IOException e) {
            System.out.println("IO exception: " + e);
        } catch (ParserConfigurationException e) {
            System.out.println("ParserConfiguration exception: " + e);
        }
        return rootNodeName;
    }
static public String getRootNodeName ( String xml ) { String rootNodeName = null ; try { DocumentBuilderFactory dbf = DocumentBuilderFactory . newInstance ( ) ; dbf . setExpandEntityReferences ( false ) ; DocumentBuilder dBuilder = dbf . newDocumentBuilder ( ) ; InputSource inputSource = new InputSource ( new StringReader ( xml ) ) ; Document doc = dBuilder . parse ( inputSource ) ; doc . getDocumentElement ( ) . normalize ( ) ; rootNodeName = doc . getDocumentElement ( ) . getNodeName ( ) ; } catch ( SAXException e ) { System . out . println ( "SAX exception: " + e ) ; } catch ( IOException e ) { System . out . println ( "IO exception: " + e ) ; } catch ( ParserConfigurationException e ) { System . out . println ( "ParserConfiguration exception: " + e ) ; } return rootNodeName ; }
Retrieve the name of the root node in an XML string.
@param xml The XML string to parse.
@return The name of the root node, or null if parsing failed.