中科院云平台架构师,专注于数字化、智能化,技术方向:云、Linux内核、AI、MES/ERP/CRM/OA、物联网、传感器、大数据、ML、微服务。
分类: Java
2013-04-26 15:57:30
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class XercesTest extends DefaultHandler {
// static{
// System.out.print("AAA");
// System.exit(0);
// }
/**
* 一段使用Xerces解析XML的例子。
* @param args
*/
public static void main(String[] args) throws Exception {
System.out.println("hello world!");
//(new XercesTest()).run(args[0]);
(new XercesTest()).run("member.xml");
}
public void run(String file) throws Exception {
XMLReader parser = XMLReaderFactory.createXMLReader();
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!");
}
}
member.xml