import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class SAXTest {
public static void main(String[] args) {
// String filexml = SAXTest.class.getResource("books.xml").toString();
String filexml = SAXTest.class.getResource("BookXMLFile.xml").toString();
//sax解析器工厂实例
SAXParserFactory factory = SAXParserFactory.newInstance();
//sax解析器实例对xml文档解析
try {
factory.newSAXParser().parse(filexml, new SaxHandlerExample1());
factory.newSAXParser().parse(filexml, new SaxHandlerExample2());
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解析方法1:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* date:9月14日am
*
*/
public class SaxHandlerExample1 extends DefaultHandler{
@Override
public void startDocument() throws SAXException {
// 开始解析文档
System.out.println("-------开始解析文档--------");
super.startDocument();
}
/* uri - 名称空间 URI,如果元素没有任何名称空间 URI,或者没有正在执行名称空间处理,则为空字符串。
* localName - 本地名称(不带前缀),如果没有正在执行名称空间处理,则为空字符串。
* qName - 限定的名称(带有前缀),如果限定的名称不可用,则为空字符串。
* attributes - 附加到元素的属性。如果没有属性,则它将是空的 Attributes 对象。
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// 开始解析元素
System.out.println("开始解析元素:\r"+qName);
// System.out.println(uri);//名称空间为空字符串,打印换行
// System.out.println(localName);//本地名称(不带前缀)为空字符串
// System.out.println(attributes.getIndex("id"));//通过 XML 限定(前缀)名查找属性的索引。 存在指定名称的属性则返回其索引位置,否则返回-1
//属性attributes是一个列表,包含了解析的xml中元素的属性
for(int i = 0 ;i<attributes.getLength();i++){
uri = attributes.getURI(i);
localName = attributes.getLocalName(i);//通过索引查找属性的本地名称。 属性有 id,color,face
String localNameValue = attributes.getValue(i);
System.out.println("属性:"+localName+"="+localNameValue);
}
super.startElement(uri, localName, qName, attributes);
}
/*
* ch - 字符。
* start - 字符数组中的开始位置。
* length - 从字符数组中使用的字符数。
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// 接收元素中字符数据的通知。
//ch字符数组中可以读取到的xml中所有内容
System.out.println(ch);//打印读取到的所有xml内容
//. 要取出xml中的标签的数据“标题”,如:<title>标题</title>
// 则需要根据ch数组重新建立一个字符串
String content = new String(ch,start,length);
System.out.println(content);
super.characters(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// 接收元素结束的通知。
super.endElement(uri, localName, qName);
}
@Override
public void endDocument() throws SAXException {
// 接收文档结束的通知。
System.out.println("-------解析文档结束-------");
super.endDocument();
}
}
运行结果:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book isbn="6527564315823">
<name>xml入门</name>
<authors>
<name>张三</name>
<name>李四</name>
</authors>
<pulisher>达内出版社</pulisher>
<pages>23</pages>
<price>389.99</price>
</book>
<book isbn="452345">
<name>xml进阶</name>
<authors>
<name>李四</name>
<name>王五</name>
</authors>
<pulisher>达内出版社</pulisher>
<pages>33</pages>
<price>399.99</price>
</book>
<book isbn="95634235">
<name>xml高级</name>
<authors>
<name>王五</name>
<name>赵六</name>
</authors>
<pulisher>达内出版社</pulisher>
<pages>43</pages>
<price>409.99</price>
</book>
</books>