java.lang.Object
org.cybergarage.xml.Node
Represents an XML node with name, value, attributes, and child nodes.
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:
- Node name and value management
- Attribute storage and retrieval
- Parent-child node relationships
- XML string output with proper escaping
- User data attachment for application-specific information
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AttributeList/////////////////////////////////////////////private String/////////////////////////////////////////////private NodeList/////////////////////////////////////////////private Node/////////////////////////////////////////////private Object/////////////////////////////////////////////private String/////////////////////////////////////////////Constructor Summary
Method Summary
All MethodsModifier and TypeMethodDescriptionvoidaddAttribute(String name, String value) Adds an attribute with the specified name and value.voidaddAttribute(Attribute attr) Adds an attribute to this node.voidAdds a child node to this node.voidAdds a value to this node's existing value.boolean/////////////////////////////////////////////getAttribute(int index) Gets the attribute at the specified index.getAttribute(String name) Gets the attribute with the specified name.intGets the integer value of an attribute with the specified name.getAttributeValue(String name) Gets the value of an attribute with the specified name.getIndentLevelString(int nIndentLevel) InvokegetIndentLevelString(int, String)with" "as StringgetIndentLevelString(int nIndentLevel, String space) Get the indent level string.intGets the index of the first child node with the specified name.getName()Gets the name of this node.intGets the number of attributes this node has.intGets the number of child nodes this node has.getNode(int index) Gets the child node at the specified index.Gets the first child node with the specified name.getNodeEndsWith(String name) Gets the first child node whose name ends with the specified string.getNodeValue(String name) Gets the value of the first child node with the specified name.Gets the parent node of this node.Gets the root node of the tree this node belongs to.Gets the user data associated with this node.getValue()Gets the value of this node.booleanChecks if this node has any attributes.inthashCode()booleanChecks if this node has a child node with the specified name.booleanhasNodes()Checks if this node has any child nodes.voidinsertAttributeAt(Attribute attr, int index) Inserts an attribute at the specified index.voidinsertNode(Node node, int index) Inserts a child node at the specified index.booleanChecks if this node's name matches the specified value.voidoutput(PrintWriter ps, int indentLevel, boolean hasChildNode) Outputs this node as XML to the specified print writer with indentation.voidOutputs this node's attributes to the specified print writer.voidprint()Prints this node as XML to standard output.voidprint(boolean hasChildNode) Prints this node as XML to standard output.voidRemoves all attributes from this node.voidRemoves all child nodes from this node.booleanremoveAttribute(String name) Removes the attribute with the specified name from this node.booleanremoveAttribute(Attribute attr) Removes the specified attribute from this node.booleanremoveNode(String name) Removes the first child node with the specified name.booleanremoveNode(Node node) Removes a child node from this node.booleanCopies all properties from another node to this node.voidsetAttribute(String name, int value) Sets an attribute value as an integer.voidsetAttribute(String name, String value) Sets an attribute value.voidSets the name of this node.voidSets the namespace and name of this node.voidsetNameSpace(String ns, String value) Sets a namespace attribute for this node.voidEnsures a child node with the specified name exists.voidEnsures a child node with the specified name exists and sets its value.voidsetParentNode(Node node) Sets the parent node of this node.voidsetUserData(Object data) Sets user data for this node.voidsetValue(int value) Sets the value of this node as an integer.voidSets the value of this node.toString()Gets the XML string representation of this node.Gets the XML string representation of this node with the specified encoding.Gets the XML string representation with all XML characters escaped.toXMLString(boolean hasChildNode) Gets the XML string representation with all XML characters escaped.
Field Details
parentNode
/////////////////////////////////////////////name
/////////////////////////////////////////////value
/////////////////////////////////////////////attrList
/////////////////////////////////////////////nodeList
/////////////////////////////////////////////userData
/////////////////////////////////////////////
Constructor Details
Node
public Node()Creates a Node with empty UserData and no Parent Node.Node
Creates a Node with specified name.- Parameters:
name- name to set for the node
Node
Node
Method Details
setParentNode
Sets the parent node of this node.- Parameters:
node- the parent node, or null if this node has no parent
getParentNode
Gets the parent node of this node.- Returns:
- the parent node, or null if this node has no parent
getRootNode
Gets the root node of the tree this node belongs to.- Returns:
- the root node, or this node if it has no parent
setName
Sets the name of this node.- Parameters:
name- name to set for the node
setName
getName
isName
Checks if this node's name matches the specified value.- Parameters:
value- the name to compare against- Returns:
- true if the names match, false otherwise
setValue
Sets the value of this node.- Parameters:
value- value to set for the node
setValue
public void setValue(int value) Sets the value of this node as an integer.- Parameters:
value- integer value to set for the node
addValue
Adds a value to this node's existing value.- Parameters:
value- value to append to existing value
getValue
getNAttributes
public int getNAttributes()Gets the number of attributes this node has.- Returns:
- the number of attributes
getAttribute
Gets the attribute at the specified index.- Parameters:
index- the index of the attribute to retrieve- Returns:
- the attribute at the specified index
getAttribute
addAttribute
Adds an attribute to this node.- Parameters:
attr- the attribute to add
insertAttributeAt
Inserts an attribute at the specified index.- Parameters:
attr- the attribute to insertindex- the index at which to insert the attribute
addAttribute
removeAttribute
Removes the specified attribute from this node.- Parameters:
attr- the attribute to remove- Returns:
- true if the attribute was removed, false if not found
removeAttribute
Removes the attribute with the specified name from this node.- Parameters:
name- the name of the attribute to remove- Returns:
- true if the attribute was removed, false if not found
removeAllAttributes
public void removeAllAttributes()Removes all attributes from this node.hasAttributes
public boolean hasAttributes()Checks if this node has any attributes.- Returns:
- true if this node has attributes, false otherwise
setAttribute
setAttribute
Sets an attribute value as an integer. If the attribute already exists, its value is updated. If it doesn't exist, a new attribute is created.- Parameters:
name- the attribute namevalue- the integer attribute value
getAttributeValue
getAttributeIntegerValue
Gets the integer value of an attribute with the specified name.- Parameters:
name- the attribute name- Returns:
- the attribute value as integer, or 0 if not found or invalid
setNameSpace
set
Copies all properties from another node to this node. This includes name, value, attributes, and child nodes.- Parameters:
otherNode- the node to copy from- Returns:
- true if the copy was successful, false if otherNode is null
equals
hashCode
getNNodes
public int getNNodes()Gets the number of child nodes this node has.- Returns:
- the number of child nodes
getNode
Gets the child node at the specified index.- Parameters:
index- the index of the child node to retrieve- Returns:
- the child node at the specified index
getNode
getNodeEndsWith
addNode
insertNode
Inserts a child node at the specified index.- Parameters:
node- the child node to insertindex- the index at which to insert the node
getIndex
Gets the index of the first child node with the specified name.- Parameters:
name- the name of the child node to find- Returns:
- the index of the child node, or -1 if not found
removeNode
Removes a child node from this node.- Parameters:
node- child node to remove- Returns:
- true if node was removed, false if not found
removeNode
Removes the first child node with the specified name.- Parameters:
name- the name of the child node to remove- Returns:
- true if the node was removed, false if not found
removeAllNodes
public void removeAllNodes()Removes all child nodes from this node.hasNodes
public boolean hasNodes()Checks if this node has any child nodes.- Returns:
- true if this node has child nodes, false otherwise
hasNode
Checks if this node has a child node with the specified name.- Parameters:
name- the name of the child node to check for- Returns:
- true if a child node with the specified name exists, false otherwise
setNode
Ensures a child node with the specified name exists. If a child node with the name already exists, this method does nothing. If not, a new child node with the specified name is created and added.- Parameters:
name- the name of the child node to ensure exists
setNode
Ensures a child node with the specified name exists and sets its value. If a child node with the name already exists, its value is updated. If not, a new child node with the specified name and value is created and added.- Parameters:
name- the name of the child nodevalue- the value to set for the child node
getNodeValue
setUserData
Sets user data for this node.- Parameters:
data- user data object to associate with this node
getUserData
Gets the user data associated with this node.- Returns:
- user data object, or null if not set
getIndentLevelString
InvokegetIndentLevelString(int, String)with" "as String- Parameters:
nIndentLevel- how many times the three space indent is repeated- Returns:
- the indent String, built from three spaces repeated nIndentLevel times
- See Also:
getIndentLevelString
outputAttributes
Outputs this node's attributes to the specified print writer.- Parameters:
ps- the print writer to output to
output
Outputs this node as XML to the specified print writer with indentation.- Parameters:
ps- the print writer to output toindentLevel- the indentation level for this nodehasChildNode- whether to treat this node as having child nodes
toString
toString
toXMLString
Gets the XML string representation with all XML characters escaped. This is useful for embedding XML within XML.- Parameters:
hasChildNode- whether to treat this node as having child nodes- Returns:
- the escaped XML string
toXMLString
Gets the XML string representation with all XML characters escaped. This is useful for embedding XML within XML.- Returns:
- the escaped XML string
print
public void print(boolean hasChildNode) Prints this node as XML to standard output.- Parameters:
hasChildNode- whether to treat this node as having child nodes
print
public void print()Prints this node as XML to standard output.