xml命名空间

来源:互联网 发布:自定义域名 编辑:程序博客网 时间:2024/05/13 17:18

. xml中的localName和QName

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <websites  
  3.     xmlns:sina="http://www.sina.com"  
  4.     xmlns:baidu="http://www.baidu.com">  
  5.       
  6.     <sina:website sina:blog="blog.sina.com">新浪</sina:website>  
  7.     <baidu:website baidu:blog="hi.baidu.com">百度</baidu:website>  
  8. </websites>  

Namespace(空间名称,命名空间)

引入的原因是为了避免混淆。例如上面的这个XML文档,sina和baidu都有blog属性,定义了两个namespace,就像sax官网说的,用namespace是为了实现更多的扩展功能,作为基本应用,很多时候都用不到它:

sina的namespace: http://www.sina.com

baidu的namespace:http://www.baidu.com

[html] view plain copy
  1. xmlns:sina="http://www.sina.com"  
  2. xmlns:baidu="http://www.baidu.com">  

namespace的值可以任意,但是注意不要重复。一般默认的格式都是以url来作为namespace,比如

xmlns:Android="http://schemas.android.com/apk/res/android。


Prefix(前缀)

sina:blog中 sina就是前缀。


LocalName(本地名称)

sina:blog 中blog就是localName。


QName(Qualified Name 限定?指定?名称)

sina:blog就是QName,相当于前缀+":"+LocalName。


uri(不是url哈)

例如sina:blog的uri就是前缀sina的namespace,即"http://www.sina.com"。

2.sax解析localName与qName

sax  simple API for XML,现在有两个版本,sax和sax2。

sax不支持LocalName、QName和uri。对于属性sina:blog="blog.sina.com",sax解析的结果是LocalName=QName="sina:blog",uri="",value="blog.sina.com"。

sax2支持LocalName、QName、uri。对于属性sina:blog="blog.sina.com",sax2解析的结果是LocalName="blog",QName="sina:blog",uri="",value="blog.sina.com"。

测试:

TestSax.Java

[java] view plain copy
  1. package com.siqi.xml;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import javax.xml.parsers.SAXParser;  
  6. import javax.xml.parsers.SAXParserFactory;  
  7.   
  8. import org.xml.sax.InputSource;  
  9. import org.xml.sax.XMLReader;  
  10. import org.xml.sax.helpers.XMLReaderFactory;  
  11.   
  12. public class TestSax {  
  13.     public static void main(String... args) {  
  14.           
  15.         try {  
  16.             //Sax2解析XML文档  
  17.             System.out.println("parse xml file use sax2");  
  18.             SaxParseHandler sax2Handler = new SaxParseHandler();  
  19.             XMLReader xmlReader = XMLReaderFactory.createXMLReader();  
  20.             xmlReader.setContentHandler(sax2Handler);  
  21.             xmlReader.setErrorHandler(sax2Handler);  
  22.   
  23.             FileReader fileReader = new FileReader("./src/sample.xml");  
  24.             xmlReader.parse(new InputSource(fileReader));  
  25.               
  26.             //sax1解析XML文档  
  27.             System.out.println("parse xml file use sax");  
  28.             SaxParseHandler saxHandler = new SaxParseHandler();  
  29.             SAXParser parser = SAXParserFactory.newInstance().newSAXParser();  
  30.               
  31.             File file = new File("./src/sample.xml");  
  32.             parser.parse(file, saxHandler);  
  33.               
  34.         } catch (Exception e) {  
  35.             // TODO Auto-generated catch block  
  36.             e.printStackTrace();  
  37.         }   
  38.     }  
  39. }  

SaxParseHandler.java

[java] view plain copy
  1. package com.siqi.xml;  
  2.   
  3. import org.xml.sax.Attributes;  
  4. import org.xml.sax.SAXException;  
  5. import org.xml.sax.helpers.DefaultHandler;  
  6.   
  7. /** 
  8.  * 要用sax解析,需要实现一个ParseHandler 
  9.  * @author siqi 
  10.  * 
  11.  */  
  12. public class SaxParseHandler extends DefaultHandler{  
  13.   
  14.     /** 
  15.      * 重写了DefaultHandler中的startElement函数,每解析到 
  16.      * 一个元素(element)的时候都会触发这个函数,并且将这个element 
  17.      * 的属性attributes和值value当作参数传进来。除了startElement, 
  18.      * 还有startDocument,endDOucment,endElement,要根据需要 
  19.      * 重写这些函数。 
  20.      */  
  21.     @Override  
  22.     public void startElement(String uri, String localName, String qName,  
  23.             Attributes attributes) throws SAXException {  
  24.           
  25.         //打印element的基本信息,qName  
  26.         System.out.println("Element qName    : "+qName);  
  27.         System.out.println("Element localName: "+localName);  
  28.         System.out.println("Element uri      : "+uri);  
  29.         //打印element的所有属性attributes  
  30.         for(int i=0; i<attributes.getLength(); i++) {  
  31.             System.out.println("");  
  32.             System.out.println("  attribute qName    : "+attributes.getQName(i));  
  33.             System.out.println("  attribute localName: "+attributes.getLocalName(i));  
  34.             System.out.println("  attribute value    : "+attributes.getValue(i));  
  35.             System.out.println("  attribute uri      : "+attributes.getURI(i));  
  36.         }  
  37.         System.out.println("");  
  38.         super.startElement(uri, localName, qName, attributes);  
  39.     }  
  40.       
  41. }  


sample.xml的内容最上面那个xml
执行结果:
[html] view plain copy
  1. parse xml file use sax2  
  2. Element qName    : websites  
  3. Element localName: websites  
  4. Element uri      :   
  5.   
  6. Element qName    : sina:website  
  7. Element localName: website  
  8. Element uri      : http://www.sina.com  
  9.   
  10.   attribute qName    : sina:blog  
  11.   attribute localName: blog  
  12.   attribute value    : blog.sina.com  
  13.   attribute uri      : http://www.sina.com  
  14.   
  15. Element qName    : baidu:website  
  16. Element localName: website  
  17. Element uri      : http://www.baidu.com  
  18.   
  19.   attribute qName    : baidu:blog  
  20.   attribute localName: blog  
  21.   attribute value    : hi.baidu.com  
  22.   attribute uri      : http://www.baidu.com  
  23.   
  24. parse xml file use sax  
  25. Element qName    : websites  
  26. Element localName:   
  27. Element uri      :   
  28.   
  29.   attribute qName    : xmlns:sina  
  30.   attribute localName: xmlns:sina  
  31.   attribute value    : http://www.sina.com  
  32.   attribute uri      :   
  33.   
  34.   attribute qName    : xmlns:baidu  
  35.   attribute localName: xmlns:baidu  
  36.   attribute value    : http://www.baidu.com  
  37.   attribute uri      :   
  38.   
  39. Element qName    : sina:website  
  40. Element localName:   
  41. Element uri      :   
  42.   
  43.   attribute qName    : sina:blog  
  44.   attribute localName: sina:blog  
  45.   attribute value    : blog.sina.com  
  46.   attribute uri      :   
  47.   
  48. Element qName    : baidu:website  
  49. Element localName:   
  50. Element uri      :   
  51.   
  52.   attribute qName    : baidu:blog  
  53.   attribute localName: baidu:blog  
  54.   attribute value    : hi.baidu.com  
  55.   attribute uri      :   

对于sax2,正确的解析出了qName,LocalName和uri。sax不能识别出前缀和uri,qName=localName。


0 0
原创粉丝点击