- Apache Xerces Tutorial
- Xerces Home
- XML Overview
- Xerces Environment Setup
- XML Parsers
- Xerces DOM Parser
- Xerces DOM Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Xerces SAX Parser
- Xerces SAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Xerces StAX Parser
- Xerces StAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Xerces Useful Resources
- Xerces - Quick Guide
- Xerces - Useful Resources
- Xerces - Discussion
SAX 解析器 - 解析 XML 文档
演示示例
这是我们需要解析的输入 xml 文件 -
<?xml version = "1.0"?> <class> <student rollno = "393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno = "493"> <firstname>Vineet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno = "593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class>
用户处理程序.java
package com.tutorialspoint.xml; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class UserHandler extends DefaultHandler { boolean bFirstName = false; boolean bLastName = false; boolean bNickName = false; boolean bMarks = false; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("student")) { String rollNo = attributes.getValue("rollno"); System.out.println("Roll No : " + rollNo); } else if (qName.equalsIgnoreCase("firstname")) { bFirstName = true; } else if (qName.equalsIgnoreCase("lastname")) { bLastName = true; } else if (qName.equalsIgnoreCase("nickname")) { bNickName = true; } else if (qName.equalsIgnoreCase("marks")) { bMarks = true; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("student")) { System.out.println("End Element :" + qName); } } @Override public void characters(char ch[], int start, int length) throws SAXException { if (bFirstName) { System.out.println("First Name: " + new String(ch, start, length)); bFirstName = false; } else if (bLastName) { System.out.println("Last Name: " + new String(ch, start, length)); bLastName = false; } else if (bNickName) { System.out.println("Nick Name: " + new String(ch, start, length)); bNickName = false; } else if (bMarks) { System.out.println("Marks: " + new String(ch, start, length)); bMarks = false; } } }
SAXParserDemo.java
package com.tutorialspoint.xml; import java.io.File; 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 SAXParserDemo { public static void main(String[] args){ try { File inputFile = new File("input.txt"); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); UserHandler userhandler = new UserHandler(); saxParser.parse(inputFile, userhandler); } catch (Exception e) { e.printStackTrace(); } } } class UserHandler extends DefaultHandler { boolean bFirstName = false; boolean bLastName = false; boolean bNickName = false; boolean bMarks = false; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("student")) { String rollNo = attributes.getValue("rollno"); System.out.println("Roll No : " + rollNo); } else if (qName.equalsIgnoreCase("firstname")) { bFirstName = true; } else if (qName.equalsIgnoreCase("lastname")) { bLastName = true; } else if (qName.equalsIgnoreCase("nickname")) { bNickName = true; } else if (qName.equalsIgnoreCase("marks")) { bMarks = true; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("student")) { System.out.println("End Element :" + qName); } } @Override public void characters(char ch[], int start, int length) throws SAXException { if (bFirstName) { System.out.println("First Name: " + new String(ch, start, length)); bFirstName = false; } else if (bLastName) { System.out.println("Last Name: " + new String(ch, start, length)); bLastName = false; } else if (bNickName) { System.out.println("Nick Name: " + new String(ch, start, length)); bNickName = false; } else if (bMarks) { System.out.println("Marks: " + new String(ch, start, length)); bMarks = false; } } }
上述程序将生成以下结果 -
Roll No : 393 First Name: Dinkar Last Name: Kad Nick Name: Dinkar Marks: 85 End Element :student Roll No : 493 First Name: Vineet Last Name: Gupta Nick Name: Vinni Marks: 95 End Element :student Roll No : 593 First Name: Jasvir Last Name: Singh Nick Name: Jazz Marks: 90 End Element :student