帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > JAVA编程
SAX解析XML
作者:未知 发布时间:2005-03-12 来源:JSP天空网
package simpleTest;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.Locator;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

class TestSAX extends DefaultHandler
{
private StringBuffer buf;

public TestSAX()
{
super();
}
public void setDocumentLocator(Locator locator)
{
}
public void startDocument() throws SAXException
{
buf=new StringBuffer();
System.out.println("*******开始解析文档*******");
}
public void endDocument() throws SAXException
{
System.out.println("*******文档解析结束*******");
}

public void startPrefixMapping( String prefix, String uri )
{
System.out.println(" 前缀映射: " + prefix +" 开始!"+ " 它的URI是:" + uri);
}

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
{
}

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));
}
}

public void endElement(String namespaceURI,String localName,String fullName )throws SAXException
{
System.out.println("******元素解析结束********");
}
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();
TestSAX testsax=new TestSAX();
sp.parse(new InputSource("D:testsimpleTestclassessimpleTesttest.xml"),testsax);

}catch(IOException e)
{
e.printStackTrace();
}catch(SAXException e)
{
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}



}
}
XML文件如下:
<?xml version="1.0" encoding="gb2312"?>
<row>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
</row> 
  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·谈谈JDBC
·JDBC专题介绍
·JDBC接口技术
·利用weblogic的POOL(连接池)连接
·Jsp中调用Oracle存储过程的小例子
·JSP数据库操作例程
·JSP数据库连接大全
·用连接池提高Servlet访问数据库的效
·一种简单JDBC连接池的实现
·数据库连接池Java实现小结
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统