Getting Data from XML File (Document)

In this section, you will learn to retrieve the data from a XML file. All xml files store the data. You can add and modify the data in the xml document using the DOM APIs.

Getting Data from XML File (Document)

Getting Data from XML File (Document)

     

In this section, you will learn to retrieve the data from a XML file. All xml files store the data. You can add and modify the data in the  xml document using the DOM APIs.

Description of program:

This program helps you in retrieving the data from a XML file. It takes a xml file on the console with a message "Enter xml file name: ". After getting the xml file it parses. To parse you need DocumentBuilderFactory and DocumentBuilder. Then we create a Transformer. The setOutputProperty() is an abstract method of javax.xml.transform package which invokes the Transformer object and sets an output property.  In setOutputProperty() method we set the property "text" to generate the output in  the text format only.

An object of Document type is passed in the DOMSource() constructor. Finally, we create a Result  type object needed to generate the result. The transform() method takes the Source and Result objects and it processes the source tree to the output . Here the results are displayed at the console from the XML document.

Here is the XML File: Employee-Detail.xml

<?xml version = "1.0" ?>
<Employee-Detail>

<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Sushil </Emp_Name>
<Emp_E-mail>[email protected] </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
</Employee>

</Employee-Detail>

Here is the Java File: GetData.java

import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;

public class GetData{
  static public void main(String[] arg) {
  try{
  BufferedReader bf = new BufferedReader(
   new 
InputStreamReader(System.in));
  System.out.print("Enter XML file name: ");
  String xmlFile = bf.readLine();
  File file = new File(xmlFile);
  if (file.exists()){
  DocumentBuilderFactory factory = 
   DocumentBuilderFactory.newInstance
();
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse(xmlFile);
//  Create transformer
  Transformer tFormer = 
   TransformerFactory.newInstance
().newTransformer();
//  Output text type
  tFormer.setOutputProperty(OutputKeys.METHOD, "text");
//  Write the document to a file
  Source source = new DOMSource(doc);
  Result result = new StreamResult(System.out);
  tFormer.transform(source, result);
  }
  else{
  System.out.println("File not found!");
  }
  }
  catch (Exception e){
  System.err.println(e);
  System.exit(0);
  }  
  }
}

Download this example.

Output of this program:

C:\vinod\xml>javac GetData.java

C:\vinod\xml>java GetData
Enter XML file name: Employee-Detail.xml

   E-001
   Vinod
   [email protected]

   E-002
   Sushil
   [email protected]

   E-003
   Amit
   [email protected]