0% found this document useful (0 votes)
24 views2 pages

JAXP SAX Lab1

The document describes using JAXP to parse an XML file containing student data with SAX in Java. It defines an XML file with 3 student entries, a SAXProcessing class that extends DefaultHandler to count student elements, and a TestSAXProcessing class that runs the parser on the XML file and prints the student count. The SAXProcessing class overrides startElement to increment a count for each <Student> element, and endDocument prints the final count.

Uploaded by

hmp_of_you
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

JAXP SAX Lab1

The document describes using JAXP to parse an XML file containing student data with SAX in Java. It defines an XML file with 3 student entries, a SAXProcessing class that extends DefaultHandler to count student elements, and a TestSAXProcessing class that runs the parser on the XML file and prints the student count. The SAXProcessing class overrides startElement to increment a count for each <Student> element, and endDocument prints the final count.

Uploaded by

hmp_of_you
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

IXJ – JAXP

Create Project : DemoSAXinJAXP


Create file XML : Student.xml
<?xml version="1.0" encoding="utf-8" ?>
<StudentList>
<Student StudentID="S01" Gender="Male" >
<Name>Nguyen Van Tuyen</Name>
<Age>23</Age>
</Student>
<Student StudentID="S02" Gender="FeMale" >
<Name>Kieu Thi Nga </Name>
<Age>20</Age>
</Student>
<Student StudentID="S03" Gender="FeMale" >
<Name>Nguyen Thi Hoa </Name>
<Age>21</Age>
</Student>
</StudentList>

Create class : SAXProcessing


package app;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXProcessing extends DefaultHandler {

int count;

public SAXProcessing() {
count = 0;
}

@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
if (qName.equals("Student")) {
count++;
}

Design by Nguyen Hong Son


IXJ – JAXP
}

@Override
public void endDocument() throws SAXException {
System.out.println("Co " + count + " trong danh
sach");
}
}
Create class : TestSAXProcessing
package app;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

public class TestSAXProcessing {

public static void main(String[] args) {


SAXProcessing saxObject = new SAXProcessing();
String path = "src\\app\\Student.xml";
SAXParserFactory spf =
SAXParserFactory.newInstance();
try {
SAXParser parser = spf.newSAXParser();
File f = new File(path);
parser.parse(f, saxObject);
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

Design by Nguyen Hong Son

You might also like