.xml
.JAVA
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class DomParser {
public static void main(String args[])
{
try
{
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document doc = b.parse("/Volumes/Project/Bhavesh/workspace/DomParser/src/person.xml");
doc.getDocumentElement().normalize();
System.out.println ("Root element: " +
doc.getDocumentElement().getNodeName());
NodeList items = doc.getElementsByTagName("person");
for (int i = 0; i < items.getLength(); i++)
{
Node n = items.item(i);
if (n.getNodeType() != Node.ELEMENT_NODE)
continue;
Element e = (Element) n;
System.out.println("\n");
System.out.println("Person:");
NodeList first =e.getElementsByTagName("first");
Element titleElem = (Element) first.item(0);
Node titleNode = titleElem.getChildNodes().item(0);
System.out.println("First Name :"+titleNode.getNodeValue());
NodeList last =e.getElementsByTagName("last");
Element titleElem1 = (Element) last.item(0);
Node titleNode1 = titleElem1.getChildNodes().item(0);
System.out.println("Last name :"+titleNode1.getNodeValue());
NodeList age =e.getElementsByTagName("age");
Element titleElem11 = (Element) age.item(0);
Node titleNode11 = titleElem11.getChildNodes().item(0);
System.out.println("Age :"+titleNode11.getNodeValue());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<book>
<person><book>
<first>bhavesh</first>
<last>Rangani</last>
<age>22</age>
<last>Rangani</last>
<age>22</age>
</person>
<person>
<first>Vivek</first>
<last>Patoliya</last>
<age>21</age>
</person>
<person>
<first>Vishal</first>
<last>Mangroliya</last>
<age>21</age>
</person>
</book>
<person>
<first>Vivek</first>
<last>Patoliya</last>
<age>21</age>
</person>
<person>
<first>Vishal</first>
<last>Mangroliya</last>
<age>21</age>
</person>
</book>
.JAVA
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class DomParser {
public static void main(String args[])
{
try
{
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document doc = b.parse("/Volumes/Project/Bhavesh/workspace/DomParser/src/person.xml");
doc.getDocumentElement().normalize();
System.out.println ("Root element: " +
doc.getDocumentElement().getNodeName());
NodeList items = doc.getElementsByTagName("person");
for (int i = 0; i < items.getLength(); i++)
{
Node n = items.item(i);
if (n.getNodeType() != Node.ELEMENT_NODE)
continue;
Element e = (Element) n;
System.out.println("\n");
System.out.println("Person:");
NodeList first =e.getElementsByTagName("first");
Element titleElem = (Element) first.item(0);
Node titleNode = titleElem.getChildNodes().item(0);
System.out.println("First Name :"+titleNode.getNodeValue());
NodeList last =e.getElementsByTagName("last");
Element titleElem1 = (Element) last.item(0);
Node titleNode1 = titleElem1.getChildNodes().item(0);
System.out.println("Last name :"+titleNode1.getNodeValue());
NodeList age =e.getElementsByTagName("age");
Element titleElem11 = (Element) age.item(0);
Node titleNode11 = titleElem11.getChildNodes().item(0);
System.out.println("Age :"+titleNode11.getNodeValue());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment