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..
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..
相关文章推荐
- 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)