Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Xml Dom Regenerating XML file

Related Tutorials


 
 

Share on Google+Share on Google+

Regenerating XML file

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

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> Vinod1@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </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> Vinod1@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@gmail.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@hotmail.com </Emp_E-mail>
</Employee>

</Employee-Detail>
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 5, 2007

Related Tutorials

Discuss: Regenerating XML file  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com