java类库详细讲解

源代码在线查看: getroot.html

软件大小: 5593 K
上传用户: add505
关键词: java
下载地址: 免注册下载 普通下载 VIP

相关代码

				
				
				
				Getting the Root Element in a DOM Document
				(Java Developers Almanac Example)
				
				
				
				
				
				
								    BODY CODE  {font-family: Courier, Monospace;				           font-size: 11pt}				    TABLE, BODY				          {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 10pt}				    PRE   {font-family: Courier, Monospace;				           font-size: 10pt}				    H3    {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 11pt}				    A.eglink {text-decoration: none}				    A:hover.eglink {text-decoration: underline}				    -->
				
				
				
				
				
				The Java Developers Almanac 1.4
				
				        Order this book from Amazon.
				    
				
				
				
				
				
				
				
				
				
				
				
				Home
				    >
				    List of Packages
				    >
				    org.w3c.dom
				         [30 examples]
				        
				        >
				        Getting Nodes
				             [5 examples]
				            
				
				  
				    e1071.  
				    Getting the Root Element in a DOM Document
				
				The root element of an XML file is not the same as the root element of
				a DOM document.  In particular, the XML root element is a child of the
				DOM's root element. The reason is that the XML root element can have
				siblings, such as a DocumentType or a Comment node.  The XML
				root node can be found by looking for an element node among the
				children of the DOM's root node.  However, there is a convenient
				method, Document.getDocumentElement(), that does the same thing.
				This example demonstrates both methods:
				
				
				
				    // Create a document; this method is implemented in
				    // e510 The Quintessential Program to Create a DOM Document from an XML File
				    Document doc = parseXmlFile("infilename.xml", false);
				    
				    Element root = null;
				    
				    // Get the XML root node by examining the children nodes
				    NodeList list = doc.getChildNodes();
				    for (int i=0; i<list.getLength(); i++) {
				        if (list.item(i) instanceof Element) {
				            root = (Element)list.item(i);
				            break;
				        }
				    }
				    
				    // Get the XML root node the easy way
				    root = doc.getDocumentElement();
				
				
				
				
				             Related Examples
				        
				
				
				
				
				e527. 
				    Getting a Node Relative to Another Node in a DOM Document
				
				
				
				e528. 
				    Getting the Notations in a DOM Document
				
				
				
				e529. 
				    Getting the Declared Entities in a DOM Document
				
				
				
				e530. 
				    Getting the Value of an Entity Reference in a DOM Document
				
				
				
				
				
				
				
				
				        See also: 
				
				    Adding and Removing Nodes
				  
				
				
				    Element Attributes
				  
				
				
				    Elements
				  
				
				
				    Text Nodes
				  
				
				
				    XPath
				  
				
				
				
				
				
				
				
				
				
				© 2002 Addison-Wesley.
				
				
				
							

相关资源