黑马程序员_SAX解析XML文档

------- android培训 java培训 、期待与您交流! ---------- 
一、san解析文档主要步骤如下:

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { // 1、创建解析工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); // 2、得到解析器 SAXParser sp = factory.newSAXParser(); // 3、得到读取器 XMLReader reader = sp.getXMLReader(); // 4、设置内容处理器 reader.setContentHandler(new TagValueHandler()); // 5、读取xml文档内容 reader.parse("src/com/fly/sax/book.xml"); }

其中,内容处理器需要自己写。
主要继承ContentHandler。

//得到xml文档所有内容 class ListHandler implements ContentHandler { //解析内容 public void characters(char[] ch, int start, int length) throws SAXException { System.out.println(new String(ch, start, length)); } public void endDocument() throws SAXException { // TODO Auto-generated method stub } //解析结束标签 public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("<" + qName + ">"); } public void endPrefixMapping(String prefix) throws SAXException { // TODO Auto-generated method stub } public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub } public void processingInstruction(String target, String data) throws SAXException { // TODO Auto-generated method stub } public void setDocumentLocator(Locator locator) { // TODO Auto-generated method stub } public void skippedEntity(String name) throws SAXException { // TODO Auto-generated method stub } public void startDocument() throws SAXException { // TODO Auto-generated method stub } /** * 解析开始标签名 * @param atts 带“s”,多个。 */ public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { System.out.print("<" + qName); // for(int i=0; i<atts.getLength(); i++) { //添加atts的空判断 for(int i=0; atts != null && i<atts.getLength(); i++) { //得到属性名称 String name = atts.getQName(i); //得到属性的值 String value = atts.getValue(i); System.out.print(" " + name + "=\"" + value + "\""); } System.out.println(">"); } public void startPrefixMapping(String prefix, String uri) throws SAXException { // TODO Auto-generated method stub } }

二、将xml文档封装成对象的实现。

package com.fly.sax; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class Demo3 { /** * sax解析xml文档 * * @throws SAXException * @throws ParserConfigurationException * @throws IOException */ public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { // 1、创建解析工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); // 2、得到解析器 SAXParser sp = factory.newSAXParser(); // 3、得到读取器 XMLReader reader = sp.getXMLReader(); // 4、设置内容处理器 BeanListHandler handler = new BeanListHandler(); reader.setContentHandler(handler); // 5、读取xml文档内容 reader.parse("src/com/fly/sax/book.xml"); List<Book> list = handler.getBooks(); for(Book b : list) { System.out.println(b.getName()); } } } // 把xml文档中的每一本书封装到book对象中,并把对象放在list中返回 class BeanListHandler extends DefaultHandler { private List list = new ArrayList<Book>(); private String currentTag; private Book book; //解析开始标签 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentTag = qName; if ("书".equals(currentTag)) { book = new Book(); } } //解析结束标签 public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equals("书")) { list.add(book); book = null; } currentTag = null; } //解析内容 public void characters(char[] ch, int start, int length) throws SAXException { if("书名".equals(currentTag)) { book.setName(new String(ch, start, length)); } if("作者".equals(currentTag)) { book.setAuthor(new String(ch, start, length)); } if("价格".equals(currentTag)) { book.setPrice(new String(ch, start, length)); } } public List getBooks() { return list; } }


DefaultHandler是提供的默认处理器,可以根据需要选择需要实现的类。而不需要像 ContentHandler 实现全部类。

----------------------- android培训java培训、java学习型技术博客、期待与您交流! ----------------------

详情请查看:http://edu.csdn.net/heima

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值