Regenerating XML file

In this section, you will learn to get the elements and its value using DOM APIs.

Regenerating XML file

Regenerating XML  file

     

In this section, you will learn to get the  elements and its value using DOM APIs. 

Description of program:

This example parses a xml file and regenerates it at the console using the DOM APIs. This program takes a XML file and initially checks its availability . If file exists then it creates DocumentBuilderFactory. This object creates a DocumentBuilder which parses the XML document using the parse() method. It invokes the DocumentBuilder object and creates a Document object. Through this object you create DOM tree nodes by using the getDocumentElement() method and passes it to the DOMSource(). The DOMSource constructor creates a new input source with a DOM node. And the destination source (where you recieve the result) uses the StreamResult() constructor. Here the "System.out" shows the  result on the console. 

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> Amit </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
</Employee>

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

</Employee-Detail>

Here is the Java File: GetElementsDOM.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.*; 
import javax.xml.transform.stream.*;

public class GetElementsDOM{
  static public void main(String[] arg)throws Exception{
  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()){
  try {
 
// Create a factory 
  DocumentBuilderFactory factory = 
   DocumentBuilderFactory.newInstance
();
  // Use document builder factory
  DocumentBuilder builder = factory.newDocumentBuilder();
  //Parse the document
  Document doc = builder.parse(xmlFile)
  TransformerFactory tranFact = 
   TransformerFactory.newInstance
()
  Transformer transfor = tranFact.newTransformer()
  Node node =doc.getDocumentElement();
  Source src = new DOMSource(node)
  Result dest = new StreamResult(System.out);
  transfor.transform(src, dest);
  }
  catch (Exception e) {
  System.err.println(e);
  }
  }
  else{
  System.out.print("File not found!");
  }
  }
}

Download this example.

Output of this program:

C:\vinod\xml>javac GetElementsDOM.java

C:\vinod\xml>java GetElementsDOM
Enter XML File Name: Employee-Detail.xml
<?xml version="1.0" encoding="UTF-8"?><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> Amit </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
</Employee>

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

</Employee-Detail>