Uploaded by Kapil

CBS 2

advertisement
FREEMIND
Date
Page
Comor 9oyond 3llabus-2
Am ato a paogam paxsing XMLdocumant usina
DOM forsoN
Longuag
U O USod
Thoo
XML
oNa
Iho Documont dbiat modo (Dam is om
Icla scommandotian o Auolà uidi
(WZC) Consostium. A4 dalln93 am
Antoxlate hot
doCumed
onabbs poasama da Occess amd
m
pCOD Anot 9ubbodiS
Dom implomonHoHon_ K tKdJos intDDAaCo_
USduhom
naod
Aa ho
obatho gBxuCUN _o
daumem
MOKP Aham OnCO-
Whom ua poRCo_gm ym
pOzso_u go
dOCUmnt ujltna ANOM.
bacRa Aafe_91dUCHDe thot
Conaingl AhosonordE hDocumont
Vaais
o
com USO O
2AAuCHUA
oRul
OXGmingh
rctiong
Contonds @nd
aho douont
ThoxpOximont høs hgosn 2tudid and
imblornontad 9kcASSL
Content Beyond Syllabus-2
Aim:- Write a program parsing XML document using DOM Parser.
Source Code:XMLFile.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<class>
<student>
<id>101</id>
<firstname>Naman</firstname>
<lastname>Kumar</lastname>
<subject>Math</subject>
<marks>83</marks>
</student>
<student>
<id>102</id>
<firstname>Kapil</firstname>
<lastname>Kumar</lastname>
<subject>Chemistry</subject>
<marks>60</marks>
</student>
<student>
<id>103</id>
<firstname>Harsh</firstname>
<lastname>Singh</lastname>
<subject>English</subject>
<marks>70</marks>
</student>
<student>
<id>104</id>
<firstname>Jitesh</firstname>
<lastname>Singh</lastname>
<subject>Physics</subject>
<marks>76</marks>
</student>
</class>
• ReadXMLFileExample1.java
package readxmlfileexample1;
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 ReadXMLFileExample1
{
public static void main(String[] args)
{
try
{
//creating a constructor of file class and parsing an XML file
File file = new
File("C:\\Users\\DELL\\Documents\\NetBeansProjects\\ReadXMLFileExample1\\src\\rea
dxmlfileexa
mple1\\XMLFile.xml");
//an instance of factory that gives a document builder
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//an instance of builder to parse the specified xml file
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
NodeList nodeList = doc.getElementsByTagName("student");
// nodeList is not iterable, so we are using for loop
for (int itr = 0; itr < nodeList.getLength(); itr++)
{
Node node = nodeList.item(itr);
System.out.println("\nNode Name :" + node.getNodeName());
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
System.out.println("Student id: "+
eElement.getElementsByTagName("id").item(0).getTextContent());
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("Subject: "+
eElement.getElementsByTagName("subject").item(0).getTextContent());
System.out.println("Marks: "+
eElement.getElementsByTagName("marks").item(0).getTextContent());
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
OUTPUT:-
Download