两种解析方式:
dom4j 和 sax
解析思想:
book2.xml
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名>JAVA</书名> <作者>XXXXXX</作者> <售价>23333</售价> </书> <书> <书名>ANDROID</书名> <作者>XXXXXX</作者> <售价>23333</售价> </书> </书架>
利用Sax解析book2.xml文件
public class SaxDemo { public static void main(String[] args) throws Exception { // 获取解析器工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); // 通过工厂来获取sax解析器 SAXParser parser = factory.newSAXParser(); // 获取读取器 XMLReader reader = parser.getXMLReader(); // 注册事件处理器 reader.setContentHandler(new MyContentHandler1()); // 解析xml reader.parse("book2.xml"); } } class MyContentHandler1 implements ContentHandler { @Override public void startDocument() throws SAXException { System.out.println("文档解析开始"); } @Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { System.out.println("发现开始标签..." + qName); } @Override public void characters(char[] ch, int start, int length) throws SAXException { System.out.println(new String(ch, start, length)); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("发现结束标签..." + qName); } @Override public void endDocument() throws SAXException { System.out.println("文档解析结束"); } /********************************************************************/ @Override public void setDocumentLocator(Locator locator) { // TODO Auto-generated method stub } @Override public void startPrefixMapping(String prefix, String uri) throws SAXException { // TODO Auto-generated method stub } @Override public void endPrefixMapping(String prefix) throws SAXException { // TODO Auto-generated method stub } @Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub } @Override public void processingInstruction(String target, String data) throws SAXException { // TODO Auto-generated method stub } @Override public void skippedEntity(String name) throws SAXException { // TODO Auto-generated method stub } }
解析结果:
这样的结果是将整个xml文档都解析出来了,如何获得文档中的某一个标签体呢?
获得第二本书的书名 ,也就是ANDROID
public class SaxDemo { public static void main(String[] args) throws Exception { // 获取解析器工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); // 通过工厂来获取sax解析器 SAXParser parser = factory.newSAXParser(); // 获取读取器 XMLReader reader = parser.getXMLReader(); // 注册事件处理器 reader.setContentHandler(new MyContentHandler2()); // 解析xml reader.parse("book2.xml"); } } class MyContentHandler2 extends DefaultHandler { private String name = null; int count = 0; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { this.name = qName; } @Override public void characters(char[] ch, int start, int length) throws SAXException { if ("书名".equals(name) && count++ == 1) { System.out.println(new String(ch, start, length)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { name = null; } }
结果:
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 17:33:58