您的位置:首页 > 其它

SAX解析xml学习笔记[1]

2007-07-12 23:29 225 查看
Java类:TestXmlSAXHandler (监听器)


package hello.xml;




import org.xml.sax.helpers.DefaultHandler;


import org.xml.sax.helpers.XMLReaderFactory;


import org.xml.sax.SAXException;


import org.xml.sax.XMLReader;


import org.xml.sax.InputSource;


import org.xml.sax.Attributes;


import java.io.InputStream;


import java.io.File;


import java.io.FileInputStream;


import java.io.FileReader;


import java.util.HashMap;










/** *//**


* 测试SAX


* @author l


*


*/




public class TestXmlSAXHandler extends DefaultHandler...{




private HashMap<String,String> tags;






/** *//**


* 开始读xml文档的方法


*/




public void startDocument() throws SAXException...{


System.out.println(".startDocument..Ready to parser XML document..");


tags=new HashMap<String,String>();


}






/** *//**


* 在解释到一个开始元素时会调用此方法.但是当元素有重复时可以自己写算法来区分


* 这些重复的元素.qName是什么?<name:page ll=""></name:page>这样写就会抛出SAXException错误


* 通常情况下qName等于localName


*/


public void startElement(String uri,String localName,




String qName,Attributes attributes) throws SAXException...{




System.out.println(".startElement..parsering XML document.the localName is."+localName);


System.out.println(".startElement..parsering XML document.the qName is."+qName);


System.out.println(".startElement..attribute localName is."+attributes.getLocalName(0));


System.out.println(".startElement..attribute qName is."+attributes.getQName(1));


System.out.println(".startElement..attribute value is."+attributes.getValue(attributes.getQName(1)));




}






/** *//**


* 在遇到结束标签时调用此方法


*/




public void endElement(String uri, String localName, String qName)throws SAXException...{


System.out.println(".endElement..parsering XML document.the localName is."+localName);


}






/** *//**


* 读取标签里的值,ch用来存放某行的xml的字符数据,包括标签,初始大小是2048,每解释到新的字符会把它添加到char[]里。


* 注意,这个char字符会自己管理存储的字符,并不是每一行就会刷新一次char,start,length是由xml的元素数据确定的,


* 暂时找不到规律,以后看源代码.


*/




public void characters(char[] ch,int start,int length) throws SAXException...{


// System.out.println(".characters..get the .ch."+ch.length);


// System.out.println(".characters..get the .start."+start);


// System.out.println(".characters..get the .length."+length);


// System.out.println(".characters..get the .hashCode."+ch.hashCode());


// System.out.println(".characters..ch,0,39.-->"+new String(ch,0,start));


System.out.println(".characters..ch,start,length.-->"+new String(ch,start,length));


}






/** *//**


* 结束解析xml文档


*/




public void endDocument()throws SAXException...{


System.out.println(".endDocument..finish to parser XML document..");


}






public static void main(String[]args)...{


TestXmlSAXHandler handler=new TestXmlSAXHandler();


File file=new File("hello/xml/MyXml.xml");


FileReader input=null;


InputSource is=null;


// InputStream input=new InputStream();






XMLReader xmlReader=null;




try...{


input=new FileReader(file);


is=new InputSource(input);


xmlReader=XMLReaderFactory.createXMLReader();


xmlReader.setContentHandler(handler);


xmlReader.parse(is);






}catch(Exception e)...{


e.printStackTrace();


}


}




}



xml文件:MyXml.xml


<?xml version="1.0" encoding="UTF-8"?>


<POEM>


<AUTHOR go="hello" to="my">sai</AUTHOR>


<TITLE oo="11">java</TITLE>


<LINE>tangle</LINE>


</POEM>



输出:

.startDocument..Ready to parser XML document..
.startElement..parsering XML document.the localName is.POEM
.startElement..parsering XML document.the qName is.POEM
.startElement..attribute localName is.null
.startElement..attribute qName is.null
.startElement..attribute value is.null
.characters..ch,start,length.-->

.startElement..parsering XML document.the localName is.AUTHOR
.startElement..parsering XML document.the qName is.AUTHOR
.startElement..attribute localName is.go
.startElement..attribute qName is.to
.startElement..attribute value is.my
.characters..ch,start,length.-->sai
.endElement..parsering XML document.the localName is.AUTHOR
.characters..ch,start,length.-->

.startElement..parsering XML document.the localName is.TITLE
.startElement..parsering XML document.the qName is.TITLE
.startElement..attribute localName is.oo
.startElement..attribute qName is.null
.startElement..attribute value is.null
.characters..ch,start,length.-->java
.endElement..parsering XML document.the localName is.TITLE
.characters..ch,start,length.-->

.startElement..parsering XML document.the localName is.LINE
.startElement..parsering XML document.the qName is.LINE
.startElement..attribute localName is.null
.startElement..attribute qName is.null
.startElement..attribute value is.null
.characters..ch,start,length.-->tangle
.endElement..parsering XML document.the localName is.LINE
.characters..ch,start,length.-->

.endElement..parsering XML document.the localName is.POEM
.endDocument..finish to parser XML document..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: