黑马程序员技术交流社区

标题: XML的SAX解析 [打印本页]

作者: liudh1    时间: 2016-4-22 08:37
标题: XML的SAX解析
package liu.dh.dom4j;
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.helpers.DefaultHandler;
public class Dom4j_Test {
public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
    //创建Sax解析工厂
  SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
//创建解析器
  SAXParser newSAXParser = saxParserFactory.newSAXParser();
  //把xml和事件处理机制关联起来
  newSAXParser.parse("src/liu/dh/dom4j/test1.xml",new Myhandler());
  
}
}
//当应用程序尚未提供其自己的处理程序时解析器编写者可通过实例化此类来提供默认的处理程序。
//定义事件处理类:并重写方法
class Myhandler extends DefaultHandler{
//如果只想显示author,title
private boolean isTitle = false;
private boolean isAuthor = false;

@Override
public void startDocument() throws SAXException {
  // TODO Auto-generated method stub
  super.startDocument();
  System.out.println("startDoument");
}
@Override
//接受文档中的元素
public void startElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException {
  // TODO Auto-generated method stub
  //定义条件
  if (qName.equals("author")) {
   isAuthor = true;
  }
  if (qName.equals("title")) {
   isAuthor = true;
  }
   
  //super.startElement(uri, localName, qName, attributes);
  //System.out.println("元素名称="+qName);//代表元素名称
  //如果只想打印出,title,和author
}
@Override
//接收元素中字符数据的通知。
public void characters(char[] ch, int start, int length)
   throws SAXException {
  // TODO Auto-generated method stub
  super.characters(ch, start, length);
  //System.out.println("character");
  String trim = new String(ch,start,length).trim();
  //去除多余的空行
  if (!trim.equals("")&&(isAuthor||isTitle)) {
   System.out.println(new String(ch,start,length).trim());//将数组转换为字符串
  }
  //恢复原值
   isTitle = false;
  isAuthor = false;
  //System.out.println(new String(ch,start,length).trim());//将数组转换为字符串
  //System.out.println(ch);//打印数组
}
@Override
public void endElement(String uri, String localName, String qName)
   throws SAXException {
  // TODO Auto-generated method stub
  super.endElement(uri, localName, qName);
  //System.out.println("endElement"+qName);
  //System.out.println("endElement");
}
@Override
public void endDocument() throws SAXException {
  // TODO Auto-generated method stub
  super.endDocument();
  //System.out.println("endDoument");
}

}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookstore>
<book category="children">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
  </book>
<book category="cooking">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
  </book>
<book category="web" cover="paperback">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
   
  <price>39.95</price>
  </book>
<book category="web">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
  </book>
</bookstore>





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2