使用DOM处理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  DomNS;

import  java.io.File;

import  javax.xml.parsers.DocumentBuilder;
import  javax.xml.parsers.DocumentBuilderFactory;

import  org.w3c.dom.Document;
import  org.w3c.dom.NamedNodeMap;
import  org.w3c.dom.Node;

public   class  DomNSTest  {

    
public static void printNSInfo(Node node){
         
short type=node.getNodeType();
         
switch(type){
         
case Node.ELEMENT_NODE:
             
if(node.getNamespaceURI()!=null){
                 System.out.println(
"Element Name:"+node.getNodeName());
                 System.out.println(
"Local Name:"+node.getLocalName());
                 System.out.println(
"Namespace Prefix:"+node.getPrefix());
                 System.out.println(
"Namespace Uri:"+node.getNamespaceURI());
                 System.out.println(
"---------------");
             }

             
if(node.hasAttributes()){
                 NamedNodeMap map
=node.getAttributes();
                 
int len=map.getLength();
                 
for (int i = 0; i < len; i++{
                    Node attr
=map.item(i);
                    
if(attr.getNamespaceURI()!=null){
                        printNSInfo(attr);
                    }

                }

             }

             Node child
=node.getFirstChild();
             System.out.println(child);
             
while(child!=null)
                
{
                    printNSInfo(child);
                    child
=child.getNextSibling();
                }

            
             
break;
         
case Node.ATTRIBUTE_NODE:
             System.out.println(
"Attribute Name:"+node.getNodeName());
             System.out.println(
"Local Name:"+node.getLocalName());
             System.out.println(
"Namespace Prefix:"+node.getPrefix());
             System.out.println(
"Namespace Uri:"+node.getNamespaceURI());
             System.out.println(
"---------------");
             
break;
         
default:
             
break;
         }

    }

    
public static void main(String[] args) {
         String realpath
=System.getProperty("user.dir")+File.separator+"src"+File.separator+"DomNS"+File.separator+"book.xml";
        DocumentBuilderFactory factory
=DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(
true);//名称空间支持
        try {
            DocumentBuilder builder
=factory.newDocumentBuilder();
            Document doc
=builder.parse(new File(realpath));
            printNSInfo(doc.getDocumentElement());
        }
 catch (Exception e) {
            e.printStackTrace();
        }


    }


}

 运行结果:

Element Name:bks:books
Local Name:books
Namespace Prefix:bks
Namespace Uri:http://www.books.org/books
---------------
Attribute Name:xmlns:bks
Local Name:bks
Namespace Prefix:xmlns
Namespace Uri:http://www.w3.org/2000/xmlns/
---------------
[#text:
 ]
Element Name:bks:book
Local Name:book
Namespace Prefix:bks
Namespace Uri:http://www.books.org/books
---------------
[#text:
  ]
Element Name:bks:title
Local Name:title
Namespace Prefix:bks
Namespace Uri:http://www.books.org/books
---------------
[#text: java]
Element Name:bks:author
Local Name:author
Namespace Prefix:bks
Namespace Uri:http://www.books.org/books
---------------
Attribute Name:xmlns
Local Name:xmlns
Namespace Prefix:null
Namespace Uri:http://www.w3.org/2000/xmlns/
---------------
[#text:
   ]
Element Name:name
Local Name:name
Namespace Prefix:null
Namespace Uri:http://www.books.org/people
---------------
[#text: j2ee]
Element Name:title
Local Name:title
Namespace Prefix:null
Namespace Uri:http://www.books.org/people
---------------
[#text: teacher]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值