your xml:
<employee>
<name>swati</name>
<id>1209202</id>
<department>audit</department>
</employee>
program:
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class Util {
public static void main(String[] args) {
String xml="<employee><name>swati</name><id>1209202</id><department>audit</department></employee>"
System.out.println("get root element of xml");
root(xml) ;
}
public static void root(String xmlSource)
throws SAXException, ParserConfigurationException, IOException {
// Parse the given input
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
System.out.println("Root element in xml is:"+doc.getDocumentElement());
}
}
doc.getDocumentElement() will give the root element of your xml document.
No comments:
Post a Comment