SAX解析XML
来源:互联网 发布:python与opencv3.0 编辑:程序博客网 时间:2024/05/16 06:56
SAX解析XML
package simpleTest;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.Locator;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
class TestSAX extends DefaultHandler {
private StringBuffer buf;
public TestSAX() {
super();
}
public void setDocumentLocator(Locator locator) {
}
public void startDocument() throws SAXException {
buf = new StringBuffer();
System.out.println("*******开始解析文档*******");
}
public void endDocument() throws SAXException {
System.out.println("*******文档解析结束*******");
}
public void startPrefixMapping(String prefix, String uri) {
System.out.println("/n前缀映射: " + prefix + " 开始!" + " 它的URI是:" + uri);
}
public void endPrefixMapping(String prefix) {
System.out.println("/n前缀映射: " + prefix + " 结束!");
}
public void processingInstruction(String target, String instruction)
throws SAXException {
}
public void ignorableWhitespace(char[] chars, int start, int length)
throws SAXException {
}
public void skippedEntity(String name) throws SAXException {
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) {
System.out.println("*******开始解析元素*******");
System.out.println("元素名" + qName);
for (int i = 0; i < atts.getLength(); i++) {
System.out.println("元素名" + atts.getLocalName(i) + "属性值"
+ atts.getValue(i));
}
}
public void endElement(String namespaceURI, String localName,
String fullName) throws SAXException {
System.out.println("******元素解析结束********");
}
public void characters(char[] chars, int start, int length)
throws SAXException {
// 将元素内容累加到StringBuffer中
buf.append(chars, start, length);
}
public static void main(String args[]) {
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
TestSAX testsax = new TestSAX();
sp.parse(new InputSource(
"D://test//simpleTest//classes//simpleTest//test.xml"),
testsax);
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
XML文件如下:
<?xml version="1.0" encoding="gb2312"?>
<row>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
</row>
- [XML] SAX解析XML
- 【XML】SAX解析XML
- XML解析(SAX解析)
- XML解析:SAX解析
- Study SAX《SAX解析XML》
- 使用SAX解析XML
- 解析xml之--SAX
- SAX解析XML
- SAX解析XML
- xml文件解析-SAX
- 解析xml之--SAX
- 解析xml之--SAX
- 使用SAX解析XML
- XML学习《SAX解析》
- SAX解析XML例子
- java sax 解析 xml
- 使用SAX解析XML
- sax解析xml实例
- hibernate的各种保存方式的区别
- c#画笔Pen保存和恢复图形对象的设置
- A case for coredump under HP-UX B.11.31 U ia64 + oracle 10.2
- Hibernate数据加载——Session.get/load
- c#画笔Pen使用路径绘制图形
- SAX解析XML
- rex 和 racc (二) 去掉注释的内容
- IT路要如何走?——老程序员的10个忠告
- 勤劳的 coder 们注意保重身体...
- 将视频文件转为flv文件
- hibernate分页,点击最后一页时查询速度很慢的解决方法
- Asp.net页面跳转方式比较
- Vc键盘消息大全
- 错误检查思路