forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxml-parser.java
54 lines (43 loc) · 1.6 KB
/
xml-parser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlParse
{
public static void main(String args[])
{
try
{
File file = new File("C:\\Users\\user\\Desktop\\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("course");
// nodeList is not iterable, so we are using for loop
System.out.println("Course Details");
System.out.println("Course Code\tCourse Name\t\tCourse Fee");
for (int itr = 0; itr < nodeList.getLength(); itr++)
{
Node node = nodeList.item(itr);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
System.out.print( eElement.getElementsByTagName("coursecode").item(0).getTextContent()+"\t\t");
System.out.print( eElement.getElementsByTagName("coursename").item(0).getTextContent()+"\t\t");
System.out.print( eElement.getElementsByTagName("fees").item(0).getTextContent()+"\t\t");
}
System.out.println();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}}