代码学着视频敲得,怎么运行结果就是不一样呢?
package com.fwh.handle;
import javax.xml.stream.events.StartElement;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Handletest extends DefaultHandler {
int index=0;
//遍历XML文件的开始标签
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
//条用DefaultHandler的startElement方法
super.startElement(uri, localName, qName, attributes);
if(qName.equals("book")) {
index++;
System.out.println("===========第"+index+"本书开始了============");
// String a=attributes.getValue("id");
// System.out.print("属性名ID的值为:"+a);
int a=attributes.getLength();
for(int i=0;i<a;i++) {
System.out.print("属性名:"+attributes.getQName(i));
System.out.println("---值为:"+attributes.getValue(i));
}
}else if(!qName.equals("book")&&!qName.equals("bookstore")) {
System.out.print("节点名是:"+qName);
}
}
//遍历XML文件的结束标签
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if(qName.equals("book")) {
System.out.println("===========第"+index+"本书结束了============");
}
}
//用来标识解析开始
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
System.out.println("现在,解析xml开始了!");
}
//用来标识解析结束
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
System.out.println("现在,解析xml结束了!");
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
String a=new String(ch, start, length);
if(!a.trim().equals(" ")) {
System.out.println("---属性值为:"+a);
}
}
}