使用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]

你可能感兴趣的:(使用DOM处理XML NameSpace)