public class XMLNode
extends java.lang.Object
XMLRecord
. Child and parent relationships in the XML
element hierarchy are mirrored by the XMLNode
structure.
The path strings created and maintained by this class reflect the path
that would be traversed through the JDOM tree hierarchy to reach the
wrapped element. Path strings are extended as nodes are added, by
appending the element's name and its number of occurrence. The path
strings then serve as keys to the map maintained by XMLRecord
.
Here is an example path:
record:0.technical:0.requirements:1.type:0.langstring:0
This path identifies the type langstring element belonging to the 2nd technical requirement of the root element record. Here is the corresponding XML:
<record> ... <technical> ... <requirements> <type> <langstring /> </type> ... </requirements> <requirements> <type> <langstring>THIS IS THE ONE</langstring> </type> ... </requirements> ... <technical> ... <record>
By knowing the path structure, a calling method may ask XMLRecord
for any node anywhere in the hierarchy. The node then provides the caller
with convenient access methods to the element and its attributes.
Modifier and Type | Field and Description |
---|---|
protected java.util.ArrayList |
children
List of child nodes.
|
protected org.jdom.Element |
element
The JDOM XML element this node wraps.
|
protected int |
occurs
Identifies the nth occurence of this node in the parent's children
array.
|
protected XMLNode |
parent
The parent node of this one.
|
protected java.lang.String |
path
Identifies the location in the JDOM hierarchy of the wrapped XML element.
|
Constructor and Description |
---|
XMLNode(org.jdom.Element element)
Constructor wraps a JDOM Element with a new node.
|
XMLNode(org.jdom.Element element,
boolean setNormalize)
Constructor wraps a JDOM Element with a new node.
|
Modifier and Type | Method and Description |
---|---|
void |
addChild(XMLNode node)
Adds a child node to this one at the correct location, and sets
the path.
|
void |
addComment(java.lang.String value)
Adds a comment to the element of this node.
|
void |
clear() |
protected int |
findPlace(XMLNode node)
Finds the place where a new node should be inserted.
|
java.lang.String |
getAttribute(java.lang.String attName)
Retrieves a named attribute from the element of this node, if the
attribute exists.
|
XMLNode |
getChild(int index)
Gets the child node that lives at a specific index of this node's
children array.
|
java.util.List |
getChildren()
Gets the list of children belonging to this node.
|
java.lang.String |
getComment(java.lang.String identifier)
Gets an identified comment from the element of this node, if it exists.
|
java.util.List |
getComments()
Gets all the comments belonging to the element of this node.
|
org.jdom.Element |
getElement()
Retrieves the element of this node.
|
java.util.List |
getLikeChildren(java.lang.String name) |
java.lang.String |
getName()
Retrieves the name of the element of this node.
|
XMLNode |
getParent()
Retrieve the node's parent node, which is null if
this node wraps the root element.
|
java.lang.String |
getPath()
Returns the path which maps this node's element location in the
JDOM tree hierarchy.
|
java.lang.String |
getValue()
Retrieves the textual content of the element of this node.
|
boolean |
isLeaf()
Tests whether this node wraps a leaf element, which is useful for
knowing how to add content.
|
boolean |
isRoot()
Tests whether this node wraps the JDOM document root element.
|
int |
likeChildren(java.lang.String nodeName)
Searches the children nodes for an element name and returns
the number found.
|
boolean |
removeComment(java.lang.String identifier)
Removes the first identified comment from the element of this node,
if one exists.
|
void |
setAttribute(java.lang.String attName,
java.lang.String value)
Sets the value of an attribute for the element of this node.
|
void |
setPath(java.lang.String parentPath)
Appends this node's name and number of occurrence to a path.
|
void |
setValue(java.lang.String value)
Sets the textual content of the element of this node, if it is a
leaf element.
|
java.lang.String |
toString()
Retrieves the name of the element of this node.
|
protected java.lang.String path
protected java.util.ArrayList children
protected int occurs
protected org.jdom.Element element
protected XMLNode parent
public XMLNode(org.jdom.Element element)
element
- JDOM element to be wrapped.public XMLNode(org.jdom.Element element, boolean setNormalize)
element
- JDOM element to be wrapped.public XMLNode getParent()
public void clear()
public void addChild(XMLNode node)
node
- The node to add.public XMLNode getChild(int index)
NOTE: no bounds checking is done in this implementation.
index
- An integer identifying the index of the node to return.public java.util.List getChildren()
List
containing all the children of this node.public java.util.List getLikeChildren(java.lang.String name)
public int likeChildren(java.lang.String nodeName)
nodeName
- The name to search for.protected int findPlace(XMLNode node)
node
- The XMLNode to find the placement for.public java.lang.String getPath()
public void setPath(java.lang.String parentPath)
parentPath
- The path of the node which is or will be this node's
parent.public void addComment(java.lang.String value)
value
- String comment to be added.public java.lang.String getComment(java.lang.String identifier)
identifier
- String to match as an identifier.public java.util.List getComments()
public boolean removeComment(java.lang.String identifier)
identifier
- String to match as an identifier.public boolean isRoot()
public boolean isLeaf()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String getName()
public org.jdom.Element getElement()
public java.lang.String getValue()
NOTE: This implementation is built on JDOM 4, which provides weak access for reading and writing element content. JDOM 5 has deprecated this approach, and JDOM 6 is even more flexible and robust. Future implementations of this class should reflect the updates in JDOM.
public void setValue(java.lang.String value)
NOTE: This implementation is built on JDOM 4, which provides weak access for reading and writing element content. JDOM 5 has deprecated this approach, and JDOM 6 is even more flexible and robust. Future implementations of this class should reflect the updates in JDOM.
value
- The text to set as content for the element of this node.public java.lang.String getAttribute(java.lang.String attName)
attName
- The name of the attribute to retrieve.public void setAttribute(java.lang.String attName, java.lang.String value)
attName
- The name of the attribute to set.value
- The value to assign to the attribute.