java代码:
public class SaxTest {
public static void main(String[] args) throws Exception {
SAXParser parser=SAXParserFactory.newInstance().newSAXParser();
parser.parse("src/books.xml", new DefaultHandler(){
@Override
public void startDocument() throws SAXException {
System.out.println("文档开始了");
}
@Override
public void endDocument() throws SAXException {
System.out.println("文档结束了");
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
System.out.println("元素开始了");
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("元素结束了");
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println(new String(ch,start,length));
}
});
}
XML代码:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><books>
<book>
<name>DOM测试</name>
<author>陈冠希</author>
<price>10</price>
</book>
<book>
<name>DOM解析</name>
<author>范冰冰</author>
<price>100</price>
</book>
<
}