Java tutorial
//package com.java2s; import java.io.ByteArrayInputStream; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; public class Main { /** StAX Factory for creating reader to read XML */ private static XMLInputFactory inputFactory; /** * Returns the name of the root element of an XML message. * * @param xml XML message * @return the name of the root element * @exception Exception if an error occurs */ public static String getRootElementName(byte[] xml) throws Exception { ByteArrayInputStream is = new ByteArrayInputStream(xml); XMLEventReader reader = inputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); return startElement.getName().getLocalPart(); } } reader.close(); return ""; } }