80,362
社区成员
发帖
与我相关
我的任务
分享
public class sax {
/**
* SAX方式解析XML
*
* @param args
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
*/
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
// 创建工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 利用工长创建SAX解析器
SAXParser sp = factory.newSAXParser();
// 用解析器创建Reader(SAX与Dom的区别之一,不能直接用解析器读取XML文件,而是要利用Reader去读取XML)
XMLReader reader = sp.getXMLReader();
// 设置事件处理
reader.setContentHandler(new MyContentHandler());
// 读取XML,逐行解析会触发事件处理器
reader.parse("http://php.weather.sina.com.cn/xml.phppassword=DJOYnieT8234jlsK&day=0");
}
}
// 事件处理器1
class MyContentHandler implements ContentHandler {
String Weather = null;// 当前标签名
String city = null;
String status = null;
String drection = null;
String Weathererature = null;
// 解析到标签的内容会触发该事件,将其内容传过来
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String content = new String(ch, start, length);
if (Weather != null && Weather.equals("city")) {
city = content;
// System.out.println("city="+city);
}
if (Weather != null && Weather.equals("Weathererature")) {
// String content=new String(ch);
Weathererature = content;
// System.out.println("Weathererature="+Weathererature);
}
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
/*System.out.println("SAX解析XML-->startDocument");*/
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
/* System.out.println("SAX解析XML-->endDocument");
*/ }
// 解析到开始标签触发该方法,将标签名和属性传过来
@Override
public void startElement(String uri, String localcity, String qcity,
Attributes atts) throws SAXException {
// TODO Auto-generated method stub
System.out.println("开始:" + qcity);
Weather = qcity;
if (Weather.equals("Profiles")) {// 获取Profiles属性
status = atts.getValue("status");// 其实将Attributes当做一个集合,里面的元素封装属性名和属性的值更合理
drection = atts.getValue("drection");
// System.out.println("status="+status+",drection="+drection);
}
}
@Override
public void endElement(String uri, String localcity, String qcity)
throws SAXException {
// TODO Auto-generated method stub
System.out.println("结束:" + qcity);
if (Weather != null && Weather.equals("Profiles")) {
System.out.println("city=" + city);
System.out.println("status=" + status);
System.out.println("drection=" + drection);
System.out.println("Weathererature=" + Weathererature);
// 重置
city = null;
status = null;
drection = null;
Weathererature = null;
}
/*
* if(Weather!=null&&Weather.equals("grade")){
* Weather="Profiles";//很重要,Profiles的最后一个子标签遍历完后应将Weather置为Profiles }else{
* Weather=null; }
*/
}
@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 city) throws SAXException {
// TODO Auto-generated method stub
}
}
// DefaultHandler实现了ContentHandler接口,这里利用继承机制只重写感兴趣的方法
class MyDefaultHandler extends DefaultHandler {
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void startElement(String uri, String localcity, String qcity,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localcity, qcity, attributes);
}
@Override
public void endElement(String uri, String localcity, String qcity)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localcity, qcity);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
}
}