使用SAX处理XML Namespace

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...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值