Sunday, 4 February 2018

Get root element of a xml/document.getDocumentElement() function in xml

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

Handaling XLS File In Java

This code will help you to handle xls file in java using apache poi. package test; import java.io.File; import java.io.FileInputStream...