不积跬步无以至千里,不积小流无以成江海----SAX读取xml
2014-05-28 20:59
387 查看
sax 读取xml
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.sql.rowset.spi.XmlWriter;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
class Test extends DefaultHandler{
private StringBuffer buf;
private String str;
public Test(){
super();
}
// public void setDocumentLocator(Locator locator){}
@Override
public void startDocument() throws SAXException{
buf=new StringBuffer();
System.out.println("*******开始解析文档*******");
}
@Override
public void endDocument() throws SAXException{
System.out.println("*******文档解析结束*******");
}
@Override
public void startPrefixMapping( String prefix, String uri ){
System.out.println(" 前缀映射: " + prefix +" 开始!"+ " 它的URI是:" + uri);
}
@Override
public void endPrefixMapping( String prefix ){
System.out.println(" 前缀映射: "+prefix+" 结束!");
}
// public void processingInstruction( String target, String instruction )throws SAXException{}
// public void ignorableWhitespace( char[] chars, int start, int length ) throws SAXException {}
// public void skippedEntity( String name ) throws SAXException {}
@Override
public void startElement(String namespaceURI,String localName,String qName,Attributes atts){
System.out.println("*******开始解析元素*******");
System.out.println("元素名"+qName);
for(int i=0;i<atts.getLength();i++){
System.out.println("元素名"+atts.getLocalName(i)+"属性值"+atts.getValue(i));
}
}
@Override
public void endElement(String namespaceURI,String localName,String fullName )throws SAXException{
// buf.trimToSize();
str = buf.toString();
System.out.println("buf = "+buf+" || length = "+buf.length());
System.out.println("str = "+str.trim()+" || length = "+str.trim().length());
buf.delete(0,buf.length());
System.out.println("******"+namespaceURI+"元素解析结束"+localName+"********"+fullName);
}
@Override
public void characters( char[] chars, int start, int length )throws SAXException{
//将元素内容累加到StringBuffer中
buf.append(chars,start,length);
}
public static void main(String args[]){
try{
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
Test Test=new Test();
sp.parse(new File("F:/text.xml"),Test);
}catch(IOException e){
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
xml:
<?xml version="1.0" encoding="gb2312"?>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.sql.rowset.spi.XmlWriter;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
class Test extends DefaultHandler{
private StringBuffer buf;
private String str;
public Test(){
super();
}
// public void setDocumentLocator(Locator locator){}
@Override
public void startDocument() throws SAXException{
buf=new StringBuffer();
System.out.println("*******开始解析文档*******");
}
@Override
public void endDocument() throws SAXException{
System.out.println("*******文档解析结束*******");
}
@Override
public void startPrefixMapping( String prefix, String uri ){
System.out.println(" 前缀映射: " + prefix +" 开始!"+ " 它的URI是:" + uri);
}
@Override
public void endPrefixMapping( String prefix ){
System.out.println(" 前缀映射: "+prefix+" 结束!");
}
// public void processingInstruction( String target, String instruction )throws SAXException{}
// public void ignorableWhitespace( char[] chars, int start, int length ) throws SAXException {}
// public void skippedEntity( String name ) throws SAXException {}
@Override
public void startElement(String namespaceURI,String localName,String qName,Attributes atts){
System.out.println("*******开始解析元素*******");
System.out.println("元素名"+qName);
for(int i=0;i<atts.getLength();i++){
System.out.println("元素名"+atts.getLocalName(i)+"属性值"+atts.getValue(i));
}
}
@Override
public void endElement(String namespaceURI,String localName,String fullName )throws SAXException{
// buf.trimToSize();
str = buf.toString();
System.out.println("buf = "+buf+" || length = "+buf.length());
System.out.println("str = "+str.trim()+" || length = "+str.trim().length());
buf.delete(0,buf.length());
System.out.println("******"+namespaceURI+"元素解析结束"+localName+"********"+fullName);
}
@Override
public void characters( char[] chars, int start, int length )throws SAXException{
//将元素内容累加到StringBuffer中
buf.append(chars,start,length);
}
public static void main(String args[]){
try{
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
Test Test=new Test();
sp.parse(new File("F:/text.xml"),Test);
}catch(IOException e){
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
xml:
<?xml version="1.0" encoding="gb2312"?>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
相关文章推荐
- qingshow “不积跬步无以至千里,不积小流无以成江海”。--荀子《劝学篇》 用tomcat+花生壳搭建自己的web服务器+域名(参考)
- 未来两周目标计划---C++ and Disassembly(不积跬步无以至千里,不积小流无以成江海)
- 未来两周目标计划---C++ and Disassembly(不积跬步无以至千里,不积小流无以成江海)
- 不积跬步 无以至千里 不积小流 无以成江海
- 不积小流,无以成江海;不积跬步,无以至千里
- 不积跬步无以至千里 不积小流无以成江海
- 不积跬步无以至千里,不积小流无以成江海
- 劝学:不积跬步,无以至千里,不积小流,无以成江海.
- 不积跬步无以至千里 不积小流无以成江海
- 不积跬步无以至千里,不积小流无以成江海
- java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】
- SAX 读取xml文件
- java读取xml配置文件(sax)
- 关于oracle数据库里的date项插入(不积跬步,无以至千里)
- Oracle 删除表中完全相同的重复数据 (不积跬步,无以至千里)
- 不积跬步,无以至千里
- 不积跬步无以至千里----高度自适应的textarea
- Oracle 数据库查询条件null和空字符串''的区别 (不积跬步,无以至千里)
- java读取xml(SAX)示例
- 不积跬步,无以至千里