您的位置:首页 > 其它

使用SAX处理XML Namespace

2007-11-12 14:08 501 查看
book.xml




<?xml version="1.0" encoding="gb2312"?>




<bks:books xmlns:bks="http://www.books.org/books">


<bks:book>


<bks:title>java</bks:title>


<bks:author xmlns="http://www.books.org/people">


<name>j2ee</name>


<title>teacher</title>


</bks:author>


</bks:book>


</bks:books>

测试代码:




package saxNS;




import java.io.File;


import java.io.FileInputStream;


import java.io.FileNotFoundException;


import java.io.IOException;




import org.xml.sax.Attributes;


import org.xml.sax.InputSource;


import org.xml.sax.SAXException;


import org.xml.sax.XMLReader;


import org.xml.sax.helpers.DefaultHandler;


import org.xml.sax.helpers.XMLReaderFactory;






public class SaxNSTest extends DefaultHandler ...{









public void endPrefixMapping(String prefix) throws SAXException ...{


System.out.println("Namespace prefix: "+prefix);


System.out.println("Namespace ending...");







}







public void startElement(String uri, String localName, String name,




Attributes attributes) throws SAXException ...{




if(!uri.equals(""))...{


System.out.println("Element name:"+name);


System.out.println("Local name:"+localName);


//查看名称空间前缀


int index=name.indexOf(":");




if(index>0)...{


System.out.println("Namespace prefix:"+name.substring(0,index));





}


System.out.println("NameSpace uri:"+uri);


System.out.println("---------------------");


}





}







public void startPrefixMapping(String prefix, String uri)




throws SAXException ...{


System.out.println("Namespace starting...");


System.out.println("Namespace prefix: "+prefix);


System.out.println("Namespace uri: "+uri);





}








public static void main(String[] args) ...{


String realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"saxNS"+File.separator+"book.xml";




try ...{


//XMLReader缺省对名称空间进行解析


XMLReader reader=XMLReaderFactory.createXMLReader();


reader.setContentHandler(new SaxNSTest());


reader.parse(new InputSource(new FileInputStream(realpath)));




} catch (SAXException e) ...{


// TODO Auto-generated catch block


e.printStackTrace();




} catch (FileNotFoundException e) ...{


// TODO Auto-generated catch block


e.printStackTrace();




} catch (IOException e) ...{


// TODO Auto-generated catch block


e.printStackTrace();


}


}








}





结果:

Namespace starting...
Namespace prefix: bks
Namespace uri: http://www.books.org/books
Element name:bks:books
Local name:books
Namespace prefix:bks
NameSpace uri:http://www.books.org/books
---------------------
Element name:bks:book
Local name:book
Namespace prefix:bks
NameSpace uri:http://www.books.org/books
---------------------
Element name:bks:title
Local name:title
Namespace prefix:bks
NameSpace uri:http://www.books.org/books
---------------------
Namespace starting...
Namespace prefix:
Namespace uri: http://www.books.org/people
Element name:bks:author
Local name:author
Namespace prefix:bks
NameSpace uri:http://www.books.org/books
---------------------
Element name:name
Local name:name
NameSpace uri:http://www.books.org/people
---------------------
Element name:title
Local name:title
NameSpace uri:http://www.books.org/people
---------------------
Namespace prefix:
Namespace ending...
Namespace prefix: bks
Namespace ending...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: