介绍Xerces是一个开放源代码的XML语法分析器。
Xerces-C++ 的前身是 IBM 的 XML4C 项目。XML4C 和 XML4J 是两个并列的项目,而 XML4J 是 Xerces-J——Java 实现——的前身。IBM 将这两个项目的源代码让与 Apache 软件基金会(Apache Software Foundation),他们将其分别改名为 Xerces-C++ 和 Xerces-J。注:“Xerces-C”和“Xerces-C++”是同一个东西。
Xerces_J的一个例子package net.java2000.xerces;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/**
* 一段使用Xerces解析XML的例子。
*
* @author 赵学庆,Java世纪网(java2000.net)
*
*/
public class XercesTest extends DefaultHandler {
public static void main(String args[]) throws Exception {
(new XercesTest()).run(args[0]);
}
public void run(String file) throws Exception {
XMLReader parser = new SAXParser();
parser.setContentHandler(this);
parser.parse(file);
}
public void startDocument() throws SAXException {
System.out.println("starting parse XML file....");
}
public void startElement(String uri, String localName, String rawName, Attributes attlist)
throws SAXException {
System.out.println(localName);
}
public void endElement(String uri, String localName, String rawName) throws SAXException {
}
public void characters(char[] ch, int start, int length) throws SAXException {
System.out.println(new String(ch, start, length));
}
public void endDocument() throws SAXException {
System.out.println("end parse XML file!");
}
}