The XML is very simple language that allows the developers to store the structured data into XML files.XML is designed to transport and store data.
XML Document Example
In this tutorial, we will show you how to read an XML file via DOM XML parser. DOM parser parses the entire XML document and loads it into memory; then models it in a “TREE” structure for easy traversal or manipulation.
In short, it turns a XML file into DOM or Tree structure, and you have to traverse a node by node to get what you want.
e.g of your personalinfo.xml file
when you create xml file you need to specify the version of xml file. i created <company>---</company> this is called as root element of xml file.other its called node.eg. staff.
here we used the classes
DocumentBuilderFactory:Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.
DocumentBuilder:Defines the API to obtain DOM Document instances from an XML document. Using this class, an application programmer can obtain a Document from XML.
An instance of this class can be obtained from the DocumentBuilderFactory.newDocumentBuilder() method. Once an instance of this class is obtained, XML can be parsed from a variety of input sources. These input sources are InputStreams, Files, URLs, and SAX InputSources.
----------------------------
Current Element :emp
Staff id : 1
First Name : ishwar
Last Name : Panjari
Nick Name : ish
Salary : 1000
Current Element :emp
Staff id : 2
First Name : jaydip
Last Name : Raol
Nick Name : DK
Salary : 2000
XML Document Example
In this tutorial, we will show you how to read an XML file via DOM XML parser. DOM parser parses the entire XML document and loads it into memory; then models it in a “TREE” structure for easy traversal or manipulation.
In short, it turns a XML file into DOM or Tree structure, and you have to traverse a node by node to get what you want.
e.g of your personalinfo.xml file
<?xml version="1.0"?>
<company>
<staff id="1">
<firstname>ishwar</firstname>
<lastname>Panjari</lastname>
<nickname>ish</nickname>
<salary>1000</salary>
</staff>
<staff id="2">
<firstname>jaydip</firstname>
<lastname>Raol</lastname>
<nickname>DK</nickname>
<salary>2000</salary>
</staff>
</company>
when you create xml file you need to specify the version of xml file. i created <company>---</company> this is called as root element of xml file.other its called node.eg. staff.
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;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("personalInfo.xml");//personalInfo.xml is xml file name
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("emp");//here emp is the tag in xml file
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
here we used the classes
DocumentBuilderFactory:Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.
DocumentBuilder:Defines the API to obtain DOM Document instances from an XML document. Using this class, an application programmer can obtain a Document from XML.
An instance of this class can be obtained from the DocumentBuilderFactory.newDocumentBuilder() method. Once an instance of this class is obtained, XML can be parsed from a variety of input sources. These input sources are InputStreams, Files, URLs, and SAX InputSources.
output:
Root element :company----------------------------
Current Element :emp
Staff id : 1
First Name : ishwar
Last Name : Panjari
Nick Name : ish
Salary : 1000
Current Element :emp
Staff id : 2
First Name : jaydip
Last Name : Raol
Nick Name : DK
Salary : 2000
No comments:
Post a Comment