经过2天的调试终于写出了第一个自己的用SAX获取XML信息的Bean.
之前找了好多网上的资料 都调试不同。都快气爆炸了。
终于放弃了直接找代码来学,重编Oreilly的 JAVA与XML上面的例子,终于有了一点点收获后写出了一个简单的使用SAX的代码。(原来比DOM要烦那么多。)
是实现 用JAVA获取XML信息 第一步 一样的功能。
准备:
去 http://xml.apache.org 下载 Xerces 解析器
第一步:写一个JAVA程序 ShairdTest_1.java
/*
* Created on 2004-11-11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;
import shaird.XMLHandler;
/**
* @author Shaird
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ShairdTest_1{
//获取文件
public void initXML(String XMLURI) throws IOException, SAXException{
//通过指定解析器的名称来动态加载解析器
XMLReader reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
//处理内容前要注册内容管理器
reader.setContentHandler(new XMLHandler());
//解析
InputSource inputSource= new InputSource(new FileInputStream(new File(XMLURI)));
inputSource.setSystemId(XMLURI);
reader.parse(inputSource);
}
public static void main(String[] args){
try{
if (args.length != 1)
{
System.out.println("Need a XML's URL");
System.exit(0);
}
ShairdTest_1 testxml = new ShairdTest_1();
testxml.initXML(args[0]);
}catch(Exception e){
e.printStackTrace();
}
}
}
第二步:实现ContentHandler接口
XMLHandler.java
/*
* Created on 2004-11-11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.util.Map;
import org.xml.sax.Locator;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
/**
* @author Shaird
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ShairdTest2 implements ContentHandler{
private static String Empid =null;
private static String EmpName =null;
private static String EmpDept =null;
private static String EmpAge = null;
private String NowData = "";
/**位置信息定义器*/
private Locator locator;
/**将名字空间 URI保存到前缀映射*/
private Map namespaceMappings;
//设置和取得信息
public void setEmpid (String Emp_id ){
Empid =Emp_id;
}
public String getEmpid(){
return Empid;
}
public void setEmpName(String Emp_name){
EmpName=Emp_name;
}
public String getEmpName(){
return EmpName;
}
public void setEmpDept(String Emp_dept){
EmpDept=Emp_dept;
}
public String getEmpDept(){
return EmpDept;
}
public void setEmpAge(String Emp_age){
EmpAge=Emp_age;
}
public String getEmpAge(){
return EmpAge;
}
public void setDocumentLocator(Locator locator)
{
}
public void startDocument() throws SAXException
{
}
public void endDocument() throws SAXException
{
}
public void startPrefixMapping(String prefix, String uri) throws SAXException
{
}
public void endPrefixMapping(String prefix) throws SAXException
{
}
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException{
for(int i=0; i<atts.getLength();i++){
String attributename=new String(atts.getLocalName(i));
if(attributename.equals("id")){
setEmpid(atts.getValue(i));
System.out.println("id :"+getEmpid());
}
}
}
public void endElement(String namespaceURI, String localName, String qName) throws SAXException
{
if (localName.equals("EMPNAME"))
{
setEmpName(NowData);
System.out.println("EMPNAME:"+getEmpName());
}
if (localName.equals("EMPDEPT"))
{
setEmpDept(NowData);
System.out.println("EMPDEPT:"+getEmpDept());
}
if (localName.equals("EMPAGE"))
{
setEmpAge(NowData);
System.out.println("EMPAGE :"+getEmpAge());
}
}
public void characters(char[] ch, int start, int length) throws SAXException
{
NowData = new String(ch, start, length).trim();
}
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
{
}
public void processingInstruction(String target, String data) throws SAXException
{
}
public void skippedEntity(String name) throws SAXException
{
}
}
好了。用控制台去试试。java ShairdTest_1 Try.xml