Reading XML Data from a Stream
This Example shows you how to Read XML Data via a Stream in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory is used to create new DOM parsers.There are some of the methods used in code given below for Reading XML Data:-
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance():-This method Creates a DocumentBuilderFactory .DocumentBuilderFactory is a Class that enables application to obtain parser for building DOM trees from XML Document
DocumentBuilder builder = Factory.newDocumentBuilder():-This method creates a DocumentBuilder object with the help of a DocumentBuilderFactory.
transformer.transform(source, result):-This method process the source tree to the output result.
Xml code for the program generated is:-
<?xml version="1.0"
encoding="UTF-8"?> <Company> <Employee Id="Rose-2345"> <CompanyName>RoseIndia.net</CompanyName> <City>Haldwani</City>> <name>Girish Tewari</name> <Phoneno>1234567890</Phoneno> <Doj>May 2008</Doj> </Employee> <Employee Id="Rose-2346"> <CompanyName>RoseIndia.net</CompanyName> <City>Lucknow</City> <name>Mahendra Singh</name> <Phoneno>123652314</Phoneno> <Doj>May 2008</Doj> </Employee>> </Company> |
ReadingXmlDataFromStream.java
|
Output of the program:-
<?xml version="1.0" encoding="UTF-8"
standalone="no"?> <Company> <Employee Id="Rose-2345"> <CompanyName>RoseIndia.net</CompanyName> <City>Haldwani</City>> <name>Girish Tewari</name> <Phoneno>1234567890</Phoneno> <Doj>May 2008</Doj> </Employee> <Employee Id="Rose-2346"> <CompanyName>RoseIndia.net</CompanyName> <City>Lucknow</City> <name>Mahendra Singh</name> <Phoneno>123652314</Phoneno> <Doj>May 2008</Doj> </Employee>> </Company> |