Reading XML Data from a Stream

In this example, you will learn how to read xml Data via a Stream in a DOM document.

Reading XML Data from a Stream

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


/* 
 * @Program To Read XML Data from a Stream.
 * ReadingXmlDataFromStream.java 
 * Author:-RoseIndia Team
 * Date:-17-July-2008
 */
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class ReadingXmlDataFromStream {

  public static void main(String[] args) throws Exception {
  File xmlfile = new File("Document4.xml");
  DocumentBuilderFactory factory = 
  DocumentBuilderFactory.newInstance();

  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse(xmlfile);
  new ReadingXmlDataFromStream().readdata(doc);
  }

  public void readdata(Document doc) throws Exception {
  TransformerFactory transformerFactory = 
  TransformerFactory.newInstance();

  Transformer transformer = transformerFactory.newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");

  StringWriter writer = new StringWriter();
  StreamResult result = new StreamResult(writer);
  DOMSource source = new DOMSource(doc);
  transformer.transform(source, result);
  String string=writer.toString();
  System.out.println(string); 
  }
}


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>&gt;
  <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>&gt;
</Company>


Download Source Code