SAX解析xml学习笔记[1]
2007-07-12 23:29
260 查看
Java类:TestXmlSAXHandler (监听器)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package hello.xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.helpers.DefaultHandler;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.helpers.XMLReaderFactory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.SAXException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.XMLReader;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.InputSource;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.Attributes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.File;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileReader;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 测试SAX
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author l
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class TestXmlSAXHandler extends DefaultHandler...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private HashMap<String,String> tags;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 开始读xml文档的方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void startDocument() throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startDocument..Ready to parser XML document..");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tags=new HashMap<String,String>();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 在解释到一个开始元素时会调用此方法.但是当元素有重复时可以自己写算法来区分
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 这些重复的元素.qName是什么?<name:page ll=""></name:page>这样写就会抛出SAXException错误
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 通常情况下qName等于localName
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void startElement(String uri,String localName,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String qName,Attributes attributes) throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..parsering XML document.the localName is."+localName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..parsering XML document.the qName is."+qName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..attribute localName is."+attributes.getLocalName(0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..attribute qName is."+attributes.getQName(1));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..attribute value is."+attributes.getValue(attributes.getQName(1)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 在遇到结束标签时调用此方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void endElement(String uri, String localName, String qName)throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".endElement..parsering XML document.the localName is."+localName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 读取标签里的值,ch用来存放某行的xml的字符数据,包括标签,初始大小是2048,每解释到新的字符会把它添加到char[]里。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 注意,这个char字符会自己管理存储的字符,并不是每一行就会刷新一次char,start,length是由xml的元素数据确定的,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 暂时找不到规律,以后看源代码.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void characters(char[] ch,int start,int length) throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..get the .ch."+ch.length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..get the .start."+start);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..get the .length."+length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..get the .hashCode."+ch.hashCode());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..ch,0,39.-->"+new String(ch,0,start));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".characters..ch,start,length.-->"+new String(ch,start,length));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 结束解析xml文档
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void endDocument()throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".endDocument..finish to parser XML document..");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[]args)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TestXmlSAXHandler handler=new TestXmlSAXHandler();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
File file=new File("hello/xml/MyXml.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileReader input=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputSource is=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// InputStream input=new InputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XMLReader xmlReader=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
input=new FileReader(file);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
is=new InputSource(input);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
xmlReader=XMLReaderFactory.createXMLReader();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
xmlReader.setContentHandler(handler);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
xmlReader.parse(is);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}catch(Exception e)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xml文件:MyXml.xml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<POEM>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<AUTHOR go="hello" to="my">sai</AUTHOR>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<TITLE oo="11">java</TITLE>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<LINE>tangle</LINE>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</POEM>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
输出:
.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..
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package hello.xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.helpers.DefaultHandler;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.helpers.XMLReaderFactory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.SAXException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.XMLReader;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.InputSource;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.Attributes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.File;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileReader;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 测试SAX
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author l
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class TestXmlSAXHandler extends DefaultHandler...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private HashMap<String,String> tags;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 开始读xml文档的方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void startDocument() throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startDocument..Ready to parser XML document..");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tags=new HashMap<String,String>();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 在解释到一个开始元素时会调用此方法.但是当元素有重复时可以自己写算法来区分
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 这些重复的元素.qName是什么?<name:page ll=""></name:page>这样写就会抛出SAXException错误
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 通常情况下qName等于localName
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void startElement(String uri,String localName,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String qName,Attributes attributes) throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..parsering XML document.the localName is."+localName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..parsering XML document.the qName is."+qName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..attribute localName is."+attributes.getLocalName(0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..attribute qName is."+attributes.getQName(1));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".startElement..attribute value is."+attributes.getValue(attributes.getQName(1)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 在遇到结束标签时调用此方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void endElement(String uri, String localName, String qName)throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".endElement..parsering XML document.the localName is."+localName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 读取标签里的值,ch用来存放某行的xml的字符数据,包括标签,初始大小是2048,每解释到新的字符会把它添加到char[]里。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 注意,这个char字符会自己管理存储的字符,并不是每一行就会刷新一次char,start,length是由xml的元素数据确定的,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 暂时找不到规律,以后看源代码.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void characters(char[] ch,int start,int length) throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..get the .ch."+ch.length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..get the .start."+start);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..get the .length."+length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..get the .hashCode."+ch.hashCode());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// System.out.println(".characters..ch,0,39.-->"+new String(ch,0,start));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".characters..ch,start,length.-->"+new String(ch,start,length));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 结束解析xml文档
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void endDocument()throws SAXException...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(".endDocument..finish to parser XML document..");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[]args)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TestXmlSAXHandler handler=new TestXmlSAXHandler();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
File file=new File("hello/xml/MyXml.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileReader input=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputSource is=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// InputStream input=new InputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XMLReader xmlReader=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
input=new FileReader(file);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
is=new InputSource(input);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
xmlReader=XMLReaderFactory.createXMLReader();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
xmlReader.setContentHandler(handler);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
xmlReader.parse(is);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}catch(Exception e)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xml文件:MyXml.xml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<POEM>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<AUTHOR go="hello" to="my">sai</AUTHOR>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<TITLE oo="11">java</TITLE>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<LINE>tangle</LINE>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</POEM>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
输出:
.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..
相关文章推荐
- java xml解析 学习笔记(2)——SAX
- Java之SAX 方式解析 XML(学习笔记)
- Android 个人学习笔记之---SAX解析XML文件
- Android解析和创建XML之学习笔记------SAX
- android 学习笔记 SAX 解析XML数据
- Java学习笔记--xml构造与解析之Sax的使用
- ANDROID基础学习笔记_4.1_采用pull解析xml文件
- J2EE Struts2 学习笔记003 ---- struts.xml 解析
- java xml解析 学习笔记(4)——DOM4J
- XML 文件解析 -- MarsChen Android 开发教程学习笔记
- IOS学习笔记33—XML解析之KissXML的使用
- 学习笔记之XML的三种l解析方法
- xml学习笔记—简单使用DOM4J解析xml
- Android 个人学习笔记之---SAX解析XML文件(有一个坑爹的问题)
- Andorid中使用Jsoup解析库解析XML、HTML、Dom节点---第三方库学习笔记(三)
- XML学习之SAX解析XML文件
- iOS学习笔记—— xml数据解析
- Android 个人学习笔记之--- Pull解析XML文件
- javaEE学习笔记-利用DOM4J解析xml至数据库
- Javacore学习笔记-XML读取(DOM, SAX, StAx与JAXB)