当需要处理大型XML文件时,SAX解析器是一种比DOM解析器更有效的解析器。DOM解析器在将整个文档加载到内存中并在系统中构建树结构后解析XML文档,而SAX解析器则在处理XML文档时逐个元素进行解析,它遍历该文档,并在需要时发出事件以指示XML文档的不同部分。
SAX解析器不需要在内存中保存整个XML文档,因此可以处理很大的XML文件而无需担心内存不足或性能问题。
下面是使用SAX解析器在Java中处理XML的步骤:
- 创建一个SAXParserFactory对象并从中获取SAXParser对象。
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
- 实现自己的处理类(也称为处理器),继承DefaultHandler类并覆盖需要的方法。
示例代码:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyHandler extends DefaultHandler {
boolean bFirstName = false;
boolean bLastName = 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;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("student")) {
System.out.println("End Element : student");
}
}
@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;
}
}
}
- 创建处理器对象并向SAX解析器注册处理器对象。
MyHandler handler = new MyHandler();
saxParser.parse("input.xml", handler);
完成以上三个步骤后,就可以使用SAX解析器在Java中解析XML文件了。
以下是两个示例说明:
- 解析input.xml文件
input.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<class>
<student rollno="393">
<firstname>dinkar</firstname>
<lastname>kad</lastname>
<nickname>dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>jasvir</firstname>
<lastname>singn</lastname>
<nickname>jazz</nickname>
<marks>90</marks>
</student>
</class>
解析该文件的示例代码:
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class SAXParserDemo {
public static void main(String[] args) {
try {
File inputFile = new File("input.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
MyHandler handler = new MyHandler();
saxParser.parse(inputFile, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 解析具有复合元素的XML文件
input2.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<departments>
<accounting>
<employee>
<firstname>John</firstname>
<lastname>Doe</lastname>
<salary>40000</salary>
</employee>
<employee>
<firstname>Bob</firstname>
<lastname>Smith</lastname>
<salary>55000</salary>
</employee>
</accounting>
<sales>
<employee>
<firstname>Jane</firstname>
<lastname>Doe</lastname>
<salary>50000</salary>
</employee>
<employee>
<firstname>Tom</firstname>
<lastname>Jones</lastname>
<salary>65000</salary>
</employee>
</sales>
</departments>
解析该文件的示例代码:
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class SAXParserDemo {
public static void main(String[] args) {
try {
File inputFile = new File("input2.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
MyHandler2 handler = new MyHandler2();
saxParser.parse(inputFile, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyHandler2 extends DefaultHandler {
private boolean employee = false;
private boolean firstname = false;
private boolean lastname = false;
private boolean salary = false;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("employee")) {
employee = true;
} else if (qName.equalsIgnoreCase("firstname")) {
firstname = true;
} else if (qName.equalsIgnoreCase("lastname")) {
lastname = true;
} else if (qName.equalsIgnoreCase("salary")) {
salary = true;
}
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.equalsIgnoreCase("employee")) {
System.out.println("------------------------");
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (employee) {
System.out.println("Employee:");
employee = false;
} else if (firstname) {
System.out.println("First Name: " + new String(ch, start, length));
firstname = false;
} else if (lastname) {
System.out.println("Last Name : " + new String(ch, start, length));
lastname = false;
} else if (salary) {
System.out.println("Salary : " + new String(ch, start, length));
salary = false;
}
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java中使用sax解析xml的解决方法 - Python技术站