Posted in

To Read Xml File Using SAX Parser In Java

Java program To Parse XML File In SAX Parser


SAX Parser 


SAX Stands For Simple API for XML.SAX parser makes no parse tree.

SAX get occasion warnings about the XML report. In this parser need to change the order of item when you will track the date.

SAX Peruses an XML record start to finish to make up the well-formed XML document.


AIM:

To write a java program to parse an XML file using SAX parser.

PROCEDURE:

STEP 1: Create a XML document.

STEP 2: Create a java program with the DOM API.

STEP 3: Parse the XML document using the java program.

STEP 4: Display the contents of the XML document with appropriate levels.

Source Code Of This Program


1.XML File [SaxParser.xml]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?xml version="1.0"?>
<university>
	<Student_Info>
		<Firstname>karthic</Firstname>
		<Lastname>keyen</Lastname>
		<RegNo>4005</RegNo>
		<Department>Information Technology</Department>
	</Student_Info>
	<Student_Info>
		<Firstname>Vijay</Firstname>
		<Lastname>kumar</Lastname>
		<RegNo>4050</RegNo>
		<Department>Information Technology</Department>
	</Student_Info>
</university>

2. JAVA File[SaxParser.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxParser {
public static void main(String argv[]) {
 try {

	SAXParserFactory factory = SAXParserFactory.newInstance();
	 SAXParser saxParser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {
            boolean bfname = false;
	boolean blname = false;
	boolean bregno = false;
	boolean bdept = false;

	public void startElement(String uri, String localName,String qName,
                Attributes attributes) throws SAXException {

		System.out.println("Start Element :" + qName);
                       if (qName.equalsIgnoreCase("FIRSTNAME")) {
			bfname = true;
		}
                       if (qName.equalsIgnoreCase("LASTNAME")) {
			blname = true;
		}

		if (qName.equalsIgnoreCase("REGNO")) {
			bregno = true;
		}

		if (qName.equalsIgnoreCase("DEPARTMENT")) {
			bdept = true;
		}
}
public void endElement(String uri, String localName,
		String qName) throws SAXException {
                        System.out.println("End Element :" + qName);
}
public void characters(char ch[], int start, int length) throws SAXException {

if (bfname) {
System.out.println("First Name : " + new String(ch, start, length));
bfname = false;
}

if (blname) {
System.out.println("Last Name : " + new String(ch, start, length));
blname = false;
}
if (bregno) {
System.out.println("RegNo : " + new String(ch, start, length));
bregno = false;
}
if (bdept) {
System.out.println("Department : " + new String(ch, start, length));
bdept = false;
}
}
};
saxParser.parse("SaxParser.xml", handler);

 } catch (Exception e) {
       e.printStackTrace();
     }
}
}

NOTE: ALL THE TWO FILES ARE  IN THE SAME FOLDER THAT’S ONLY YOU CAN RUN THE CORRECT OUTPUT OF THE RESULT.

 Click Here

I am a Software Engineer with a strong interest in writing technology-related articles, managing two websites. I am dedicated to learning new technologies and producing engaging content. Additionally, I possess expertise in digital marketing and SEO strategies.

Leave a Reply

Your email address will not be published. Required fields are marked *