Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 1118
نمايش فراداده

Nodeorg.w3c.dom

Java 1.4

All objects in a DOM document tree (including the Document object itself) implement the Node interface, which provides basic methods for traversing and manipulating the tree.

getParentNode( ) and getChildNodes( ) allow you to traverse up and down the document tree. You can enumerate the children of a given node by looping through the elements of the NodeList returned by getChildNodes( ), or by using getFirstChild( ) and getNextSibling( ) (or getLastChild( ) and getPreviousSibling( ) to loop backwards). It is sometimes useful to call hasChildNodes( ) to determine whether a node has children or not. getOwnerDocument( ) returns the Document node of which the node is a descendant or with which it is associated. It provides a quick way to jump to the root of the document tree.

Several methods allow you to add children to a tree or alter the list of children. appendChild( ) adds a new child node at the end of this nodes list of children. insertChild( ) inserts a node into this nodes list of children, placing it immediately before a specified child node. removeChild( ) removes the specified node from this node's list of children. replaceChild( ) replaces one child node of this node with another node. For all of these methods, if the node to be appended or inserted is already part of the document tree, it is first removed from its current parent. Use cloneNode( ) to produce a copy of this node. Pass true if you want all descendants of this node to be cloned as well.

Every object in a document tree implements the Node interface, but also implements a more specialized subinterface, such as Element or Text. The getNodeType( ) method provides an easy way to determine which subinterface a node implements: the return value is one of the _NODE constants defined by this class. You might use the return value of getNodeType( ) in a switch statement, for exmaple, to determine how to process a node of unknown type.

getNodeName( ) and getNodeValue( ) provide additional information about a node, but the interpretation of the strings they return depends on the node type as shown in the table below. Note that subinterfaces typically define specialized methods (such as the getTagName( ) method of Element and the getdata( ) method of Text) for obtaining this same information. Note also that unless a node is read-only, you can use setNodeValue( ) to alter the value associated with the node.

Node type

Node name

Node value

ELEMENT_NODE

The element s tag name

null

ATTRIBUTE_NODE

The attribute name

The attribute value

TEXT_NODE

#text

The text of the node

CDATA_SECTION_NODE

#cdata-section

The text of the node

ENTITY_REFERENCE_NODE

The name of the referenced entity

null

ENTITY_NODE

The entity name

null

PROCESSING_INSTRUCTION_NODE

The target of the PI

The remainder of the PI

COMMENT_NODE

#comment

The text of the comment

DOCUMENT_NODE

#document

null

DOCUMENT_TYPE_NODE

The document type name

null

DOCUMENT_FRAGMENT_NODE

#document-fragment

null

NOTATION_NODE

The notation name

null

In documents that use namespaces, the getNodeName( ) method of a Element or Attr node returns the qualified node name, which may include a namespace prefix. In documents that use namespaces, you may prefer to use the namespace-aware methods getNamespaceURI( ), getLocalName( ) and getPrefix( ).

Element nodes may have a list of attributes, and the Element interface defines a number of methods for working with these attributes. In addition, however, Node defines the hasAttributes( ) method to determine if a node has any attributes. If it does, they can be retrieved with getAttributes( ).

Text content in an XML document is represented by Text nodes, which have methods for manipulating that textual content. The Node interface defines a normalize( ) method which has the specialized purpose of normalizing all descendants of a node by deleting empty Text nodes and coalescing adjacent Text nodes into a single combined node. Document trees usually start off in this normalized form, but modifications to the tree may result in non-normalized documents.

Most of the other interfaces in this package extend Node. Document, Element and Text are the most commonly used.

public interface

Node { // Public Constants public static final short

ATTRIBUTE_NODE ; =2 public static final short

CDATA_SECTION_NODE ; =4 public static final short

COMMENT_NODE ; =8 public static final short

DOCUMENT_FRAGMENT_NODE ; =11 public static final short

DOCUMENT_NODE ; =9

5.0 public static final short

DOCUMENT_POSITION_CONTAINED_BY ; =16

5.0 public static final short

DOCUMENT_POSITION_CONTAINS ; =8

5.0 public static final short

DOCUMENT_POSITION_DISCONNECTED ; =1

5.0 public static final short

DOCUMENT_POSITION_FOLLOWING ; =4

5.0 public static final short

DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC ; =32

5.0 public static final short

DOCUMENT_POSITION_PRECEDING ; =2 public static final short

DOCUMENT_TYPE_NODE ; =10 public static final short

ELEMENT_NODE ; =1 public static final short

ENTITY_NODE ; =6 public static final short

ENTITY_REFERENCE_NODE ; =5 public static final short

NOTATION_NODE ; =12 public static final short

PROCESSING_INSTRUCTION_NODE ; =7 public static final short

TEXT_NODE ; =3 // Public Instance Methods Node

appendChild (Node

newChild ) throws DOMException; Node

cloneNode (boolean

deep );

5.0 short

compareDocumentPosition (Node

other ) throws DOMException; NamedNodeMap

getAttributes ( );

5.0 String

getBaseURI ( ); NodeList

getChildNodes ( );

5.0 Object

getFeature (String

feature , String

version ); Node

getFirstChild ( ); Node

getLastChild ( ); String

getLocalName ( ); String

getNamespaceURI ( ); Node

getNextSibling ( ); String

getNodeName ( ); short

getNodeType ( ); String

getNodeValue ( ) throws DOMException; Document

getOwnerDocument ( ); Node

getParentNode ( ); String

getPrefix ( ); Node

getPreviousSibling ( );

5.0 String

getTextContent ( ) throws DOMException;

5.0 Object

getUserData (String

key ); boolean

hasAttributes ( ); boolean

hasChildNodes ( ); Node

insertBefore (Node

newChild , Node

refChild ) throws DOMException;

5.0 boolean

isDefaultNamespace (String

namespaceURI );

5.0 boolean

isEqualNode (Node

arg );

5.0 boolean

isSameNode (Node

other ); boolean

isSupported (String

feature , String

version );

5.0 String

lookupNamespaceURI (String

prefix );

5.0 String

lookupPrefix (String

namespaceURI ); void

normalize ( ); Node

removeChild (Node

oldChild ) throws DOMException; Node

replaceChild (Node

newChild , Node

oldChild ) throws DOMException; void

setNodeValue (String

nodeValue ) throws DOMException; void

setPrefix (String

prefix ) throws DOMException;

5.0 void

setTextContent (String

textContent ) throws DOMException;

5.0 Object

setUserData (String

key , Object

data , UserDataHandler

handler ); }

Implementations

Attr, CharacterData, Document, DocumentFragment, DocumentType, Element, Entity, EntityReference, Notation, ProcessingInstruction

Passed To

Too many methods to list.

Returned By

Too many methods to list.