Example 1
Create one java project in eclipse and add the following program in your project.
The xmlfile2.xml should be placed under project. To do this Right Click on project name and paste the xmlfile2.xml.
XMLParserAttributesDemo2
xmlfile2.xml
Output
employeedetails
============
employee
dept=A
name=Ramu
salary=1
================
employee
dept=B
name=Ragav
salary=2
================
employee
dept=C
name=Amar
salary=3
================
employee
dept=D
name=Rani
salary=4
================
Create one java project in eclipse and add the following program in your project.
The xmlfile2.xml should be placed under project. To do this Right Click on project name and paste the xmlfile2.xml.
XMLParserAttributesDemo2
package org.chidams.dom;
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
public class XMLParserAttributesDemo2
{
public static void main(String[] args)
{
try
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
File file=new File("xmlfile2.xml");
Document doc=builder.parse(file);
Element root=doc.getDocumentElement();
//Prints the context node name
System.out.println(root.getNodeName());
System.out.println("============");
//To retrieve the nodes under context node.
NodeList nodelist=root.getChildNodes();
for (int i=0;i<nodelist.getLength();i++)
{
//To check whether the current node is element node or not
if (nodelist.item(i).getNodeType()==Node.ELEMENT_NODE)
{
//If element node, the Node object will cast to Element object.
//Because attributes can be printed by using Element object. So we
//cast Node object to Element object
Node subnode=nodelist.item(i);
System.out.println(subnode.getNodeName());
Element element=(Element)subnode;
//Finds the attributes of current node
NamedNodeMap map=element.getAttributes();
//NameNodeMap is the map of attributes of currentnode
for (int j=0;j<map.getLength();j++)
{
//Prints nodename, node value.
System.out.print(map.item(j).getNodeName());
System.out.print("=");
System.out.println(map.item(j).getTextContent());
}//inner
System.out.println("================");
}//if
}//outer for
}//try
catch(Exception e)
{
e.printStackTrace();
}
}
}
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
public class XMLParserAttributesDemo2
{
public static void main(String[] args)
{
try
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
File file=new File("xmlfile2.xml");
Document doc=builder.parse(file);
Element root=doc.getDocumentElement();
//Prints the context node name
System.out.println(root.getNodeName());
System.out.println("============");
//To retrieve the nodes under context node.
NodeList nodelist=root.getChildNodes();
for (int i=0;i<nodelist.getLength();i++)
{
//To check whether the current node is element node or not
if (nodelist.item(i).getNodeType()==Node.ELEMENT_NODE)
{
//If element node, the Node object will cast to Element object.
//Because attributes can be printed by using Element object. So we
//cast Node object to Element object
Node subnode=nodelist.item(i);
System.out.println(subnode.getNodeName());
Element element=(Element)subnode;
//Finds the attributes of current node
NamedNodeMap map=element.getAttributes();
//NameNodeMap is the map of attributes of currentnode
for (int j=0;j<map.getLength();j++)
{
//Prints nodename, node value.
System.out.print(map.item(j).getNodeName());
System.out.print("=");
System.out.println(map.item(j).getTextContent());
}//inner
System.out.println("================");
}//if
}//outer for
}//try
catch(Exception e)
{
e.printStackTrace();
}
}
}
xmlfile2.xml
<?xml version="1.0"?>
<!--XMLTextWriter Example-->
<employeedetails>
<employee name="Ramu" salary="1" dept="A"></employee>
<employee name="Ragav" salary="2" dept="B"></employee>
<employee name="Amar" salary="3" dept="C"></employee>
<employee name="Rani" salary="4" dept="D"></employee>
</employeedetails>
<!--XMLTextWriter Example-->
<employeedetails>
<employee name="Ramu" salary="1" dept="A"></employee>
<employee name="Ragav" salary="2" dept="B"></employee>
<employee name="Amar" salary="3" dept="C"></employee>
<employee name="Rani" salary="4" dept="D"></employee>
</employeedetails>
Output
employeedetails
============
employee
dept=A
name=Ramu
salary=1
================
employee
dept=B
name=Ragav
salary=2
================
employee
dept=C
name=Amar
salary=3
================
employee
dept=D
name=Rani
salary=4
================
No comments:
Post a Comment
Note: only a member of this blog may post a comment.