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:booksLocal Name:booksNamespace Prefix:bksNamespace Uri:http://www.books.org/books---------------Attribute Name:xmlns:bksLocal Name:bksNamespace Prefix:xmlnsNamespace Uri:http://www.w3.org/2000/xmlns/---------------[#text: ]Element Name:bks:bookLocal Name:bookNamespace Prefix:bksNamespace Uri:http://www.books.org/books---------------[#text: ]Element Name:bks:titleLocal Name:titleNamespace Prefix:bksNamespace Uri:http://www.books.org/books---------------[#text: java]Element Name:bks:authorLocal Name:authorNamespace Prefix:bksNamespace Uri:http://www.books.org/books---------------Attribute Name:xmlnsLocal Name:xmlnsNamespace Prefix:nullNamespace Uri:http://www.w3.org/2000/xmlns/---------------[#text: ]Element Name:nameLocal Name:nameNamespace Prefix:nullNamespace Uri:http://www.books.org/people---------------[#text: j2ee]Element Name:titleLocal Name:titleNamespace Prefix:nullNamespace Uri:http://www.books.org/people---------------[#text: teacher]