public class Node extends Object
This class provides a tree-like structure for representing XML documents. Each node can have a name, value, attributes, and child nodes. Nodes can be created independently or as part of an XML document tree.
Key features:
| Constructor and Description |
|---|
Node()Creates a Node with empty UserData and no Parent Node. |
Node(Node otherNode)Creates a copy of an existing node. |
Node(String name)Creates a Node with specified name. |
Node(String ns,
String name)Creates a Node with namespace and name. |
| Modifier and Type | Method and Description |
|---|---|
void | addAttribute(Attribute attr)Adds an attribute to this node. |
void | addAttribute(String name,
String value)Adds an attribute with the specified name and value. |
void | addNode(Node node)Adds a child node to this node. |
void | addValue(String value)Adds a value to this node's existing value. |
boolean | equals(Object o) |
Attribute | getAttribute(int index)Gets the attribute at the specified index. |
Attribute | getAttribute(String name)Gets the attribute with the specified name. |
int | getAttributeIntegerValue(String name)Gets the integer value of an attribute with the specified name. |
String | getAttributeValue(String name)Gets the value of an attribute with the specified name. |
String | getIndentLevelString(int nIndentLevel)Inovoke getIndentLevelString(int, String) with " " as String |
String | getIndentLevelString(int nIndentLevel,
String space)Get the indent level string. |
int | getIndex(String name)Gets the index of the first child node with the specified name. |
String | getName()Gets the name of this node. |
int | getNAttributes()Gets the number of attributes this node has. |
int | getNNodes()Gets the number of child nodes this node has. |
Node | getNode(int index)Gets the child node at the specified index. |
Node | getNode(String name)Gets the first child node with the specified name. |
Node | getNodeEndsWith(String name)Gets the first child node whose name ends with the specified string. |
String | getNodeValue(String name)Gets the value of the first child node with the specified name. |
Node | getParentNode()Gets the parent node of this node. |
Node | getRootNode()Gets the root node of the tree this node belongs to. |
Object | getUserData()Gets the user data associated with this node. |
String | getValue()Gets the value of this node. |
boolean | hasAttributes()Checks if this node has any attributes. |
int | hashCode() |
boolean | hasNode(String name)Checks if this node has a child node with the specified name. |
boolean | hasNodes()Checks if this node has any child nodes. |
void | insertAttributeAt(Attribute attr,
int index)Inserts an attribute at the specified index. |
void | insertNode(Node node,
int index)Inserts a child node at the specified index. |
boolean | isName(String value)Checks if this node's name matches the specified value. |
void | output(PrintWriter ps,
int indentLevel,
boolean hasChildNode)Outputs this node as XML to the specified print writer with indentation. |
void | outputAttributes(PrintWriter ps)Outputs this node's attributes to the specified print writer. |
void | print()Prints this node as XML to standard output. |
void | print(boolean hasChildNode)Prints this node as XML to standard output. |
void | removeAllAttributes()Removes all attributes from this node. |
void | removeAllNodes()Removes all child nodes from this node. |
boolean | removeAttribute(Attribute attr)Removes the specified attribute from this node. |
boolean | removeAttribute(String name)Removes the attribute with the specified name from this node. |
boolean | removeNode(Node node)Removes a child node from this node. |
boolean | removeNode(String name)Removes the first child node with the specified name. |
boolean | set(Node otherNode)Copies all properties from another node to this node. |
void | setAttribute(String name,
int value)Sets an attribute value as an integer. |
void | setAttribute(String name,
String value)Sets an attribute value. |
void | setName(String name)Sets the name of this node. |
void | setName(String ns,
String name)Sets the namespace and name of this node. |
void | setNameSpace(String ns,
String value)Sets a namespace attribute for this node. |
void | setNode(String name)Ensures a child node with the specified name exists. |
void | setNode(String name,
String value)Ensures a child node with the specified name exists and sets its value. |
void | setParentNode(Node node)Sets the parent node of this node. |
void | setUserData(Object data)Sets user data for this node. |
void | setValue(int value)Sets the value of this node as an integer. |
void | setValue(String value)Sets the value of this node. |
String | toString()Gets the XML string representation of this node. |
String | toString(String enc,
boolean hasChildNode)Gets the XML string representation of this node with the specified encoding. |
String | toXMLString()Gets the XML string representation with all XML characters escaped. |
String | toXMLString(boolean hasChildNode)Gets the XML string representation with all XML characters escaped. |
public Node()
public Node(Node otherNode)
otherNode - node to copypublic Node(String name)
name - name to set for the nodepublic void addAttribute(Attribute attr)
attr - the attribute to addpublic void addAttribute(String name, String value)
name - the attribute namevalue - the attribute valuepublic void addNode(Node node)
node - child node to addpublic void addValue(String value)
value - value to append to existing valuepublic Attribute getAttribute(int index)
index - the index of the attribute to retrievepublic Attribute getAttribute(String name)
name - the name of the attribute to retrievepublic int getAttributeIntegerValue(String name)
name - the attribute namepublic String getAttributeValue(String name)
name - the attribute namepublic String getIndentLevelString(int nIndentLevel)
getIndentLevelString(int, String) with " " as StringgetIndentLevelString(int, String)public String getIndentLevelString(int nIndentLevel, String space)
nIndentLevel - the level of indentation to producespace - the String to use for the intendationpublic int getIndex(String name)
name - the name of the child node to findpublic String getName()
public int getNAttributes()
public int getNNodes()
public Node getNode(int index)
index - the index of the child node to retrievepublic Node getNode(String name)
name - the name of the child node to retrievepublic Node getNodeEndsWith(String name)
name - the suffix to matchpublic String getNodeValue(String name)
name - the name of the child nodepublic Node getParentNode()
public Node getRootNode()
public Object getUserData()
public String getValue()
public boolean hasAttributes()
public boolean hasNode(String name)
name - the name of the child node to check forpublic boolean hasNodes()
public void insertAttributeAt(Attribute attr, int index)
attr - the attribute to insertindex - the index at which to insert the attributepublic void insertNode(Node node, int index)
node - the child node to insertindex - the index at which to insert the nodepublic boolean isName(String value)
value - the name to compare againstpublic void output(PrintWriter ps, int indentLevel, boolean hasChildNode)
ps - the print writer to output toindentLevel - the indentation level for this nodehasChildNode - whether to treat this node as having child nodespublic void outputAttributes(PrintWriter ps)
ps - the print writer to output topublic void print()
public void print(boolean hasChildNode)
hasChildNode - whether to treat this node as having child nodespublic void removeAllAttributes()
public void removeAllNodes()
public boolean removeAttribute(Attribute attr)
attr - the attribute to removepublic boolean removeAttribute(String name)
name - the name of the attribute to removepublic boolean removeNode(Node node)
node - child node to removepublic boolean removeNode(String name)
name - the name of the child node to removepublic boolean set(Node otherNode)
otherNode - the node to copy frompublic void setAttribute(String name, int value)
name - the attribute namevalue - the integer attribute valuepublic void setAttribute(String name, String value)
name - the attribute namevalue - the attribute valuepublic void setName(String name)
name - name to set for the nodepublic void setName(String ns, String name)
ns - namespace prefixname - name to set for the nodepublic void setNameSpace(String ns, String value)
ns - the namespace prefixvalue - the namespace URIpublic void setNode(String name)
name - the name of the child node to ensure existspublic void setNode(String name, String value)
name - the name of the child nodevalue - the value to set for the child nodepublic void setParentNode(Node node)
node - the parent node, or null if this node has no parentpublic void setUserData(Object data)
data - user data object to associate with this nodepublic void setValue(int value)
value - integer value to set for the nodepublic void setValue(String value)
value - value to set for the nodepublic String toString()
public String toString(String enc, boolean hasChildNode)
enc - the character encoding to use, or null for defaulthasChildNode - whether to treat this node as having child nodespublic String toXMLString()
public String toXMLString(boolean hasChildNode)
hasChildNode - whether to treat this node as having child nodes