0% found this document useful (0 votes)
33 views1 page

Public Class Public Static Void

Uploaded by

madhan13092001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views1 page

Public Class Public Static Void

Uploaded by

madhan13092001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1 import java.io.

File;
2 import javax.xml.parsers.DocumentBuilder;
3 import javax.xml.parsers.DocumentBuilderFactory;
4 import org.w3c.dom.Document;
5 import org.w3c.dom.Element;
6 import org.w3c.dom.Node;
7 import org.w3c.dom.NodeList;
8
9 public class DOM {
10 public static void main(String[] args) {
11 try {
12 File inputFile = new File("input.txt");
13 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
14 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
15 Document doc = dBuilder.parse(inputFile);
16
17 doc.getDocumentElement().normalize();
18 System.out.println("Root Element: " + doc.getDocumentElement().getNodeName
());
19
20 NodeList nList = doc.getElementsByTagName("student");
21 System.out.println("------------------------");
22
23 for (int temp = 0; temp < nList.getLength(); temp++) {
24 Node nNode = nList.item(temp);
25 System.out.println("\nCurrent Element: " + nNode.getNodeName());
26
27 if (nNode.getNodeType() == Node.ELEMENT_NODE) {
28 Element eElement = (Element) nNode;
29 System.out.println("Student roll no: " + eElement.getAttribute(
"rollno"));
30 System.out.println("First name: " + eElement.getElementsByTagName(
"firstname").item(0).getTextContent());
31 System.out.println("Last name: " + eElement.getElementsByTagName(
"lastname").item(0).getTextContent());
32 System.out.println("Nick name: " + eElement.getElementsByTagName(
"nickname").item(0).getTextContent());
33 System.out.println("Marks: " + eElement.getElementsByTagName(
"marks").item(0).getTextContent());
34 }
35 }
36 } catch (Exception e) {
37 e.printStackTrace();
38 }
39 }
40 }
41 <?xml version="1.0" encoding="UTF-8"?>
42 <students>
43 <student rollno="1">
44 <firstname>Lohith</firstname>
45 <lastname>Kumar</lastname>
46 <nickname>A</nickname>
47 <marks>95</marks>
48 </student>
49 <student rollno="2">
50 <firstname>Computer</firstname>
51 <lastname>Science</lastname>
52 <nickname>CS</nickname>
53 <marks>90</marks>
54 </student>
55 </students>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy