Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Getting Dom Tree Elements and their Corresponding XML Fragments

                         

In this section, you will learn to get the elements of a DOM tree and their corresponding XML fragments. Each element of dom tree has a node level starting with '0'. Here the DOM tree elements and their corresponding XML fragments are displayed on the console.

Description of the program:

This program helps you to retrieve the  elements of a DOM tree  and their corresponding XML fragments on the console. To parse the xml file you need the DocumentBuilder and DoucnemtBuilderFactory.  With the help of this you create a NodeList through the getElementByTagName() method. The NodeList helps you in getting the length and Element. For getting the node name you use the getNodeName() method. The transform() method displays the data source and the given destination. This program uses the "System.out" to display the data on the console. 

Here is the XML File: Employee-Detail2.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-Detail>

Here is the Java File: DisplayElementNodes.java

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

public class DisplayElementNodes {
  static public void main(String[] arg){
    try{
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter a 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);
        TransformerFactory tranFactory = TransformerFactory.newInstance()
        Transformer aTransformer = tranFactory.newTransformer()
        // Get nodes list of all elements
        NodeList list = doc.getElementsByTagName("*");
        for (int i=0; i<list.getLength(); i++){
          // Get element
          Element element = (Element)list.item(i);
          Source src = new DOMSource(element)
          System.out.println("Node no: " + i + " is " + element.getNodeName());
          System.out.println"Its corresponding xml representation:");
          Result dest = new StreamResult(System.out);
          aTransformer.transform(src, dest);
          System.out.println("\n");
        }
      }
      else{
        System.out.println(xmlFile + " (file name) doesn't found!");
      }
    }
    catch (Exception e){
      e.getMessage();
    }
  }
}

Download this example.

Output of this program:

C:\vinod\xml>javac DisplayElementNodes.java

C:\vinod\xml>java DisplayElementNodes
Enter a XML file name: Employee-Detail2.xml
Node no: 0 is Employee-Detail
Its corresponding xml representation:
<?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-Detail>

Node no: 1 is Employee
Its corresponding xml representation:
<?xml version="1.0" encoding="UTF-8"?><Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>

Node no: 2 is Emp_Id
Its corresponding xml representation:
<?xml version="1.0" encoding="UTF-8"?><Emp_Id> E-001 </Emp_Id>

Node no: 3 is Emp_Name
Its corresponding xml representation:
<?xml version="1.0" encoding="UTF-8"?><Emp_Name> Vinod </Emp_Name>

Node no: 4 is Emp_E-mail
Its corresponding xml representation:
<?xml version="1.0" encoding="UTF-8"?><Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.