Accessing node data
An XML document can be read by accessing the elements of its node
tree using the appropriate PBDOM_OBJECT subclasses and methods. The
following code uses an array, the PBDOM_OBJECT, and its descendant class
PBDOM_DOCUMENT, and the GetContent and GetRootElement methods of the
PBDOM_DOCUMENT class to access node data on an XML document.
A PBDOM_DOCUMENT object named pbdom_doc contains the following XML
document:
|
1 2 3 4 5 6 7 8 9 |
<Root> <Element_1> <Element_1_1/> <Element_1_2/> <Element_1_3/> </Element_1> <Element_2/> <Element_3/> </Root> |
The following code declares an array to hold the elements returned
from the GetContent method, which reads the PBDOM_DOCUMENT object named
pbdom_doc:
|
1 2 3 |
PBDOM_OBJECT pbdom_obj_array[] ... pbdom_doc.GetContent(ref pbdom_obj_array) |
The pbdom_obj_array array now contains one value representing the
root element of pbdom_doc: <Root>.
To access the other nodes in pbdom_doc, the GetRootElement method
is used with the GetContent method.
|
1 2 |
pbdom_doc.GetRootElement().GetContent & (ref pbdom_obj_array) |
The pbdom_obj_array array now contains three values corresponding
to the three child nodes of the root element of pbdom_doc:
<Element_1>, <Element_2>, and <Element_3>.
PBDOM provides other methods for accessing data, including
InsertContent, AddContent, RemoveContent, and SetContent.
Changing node content with
arrays
You can use the AddContent method to change node content:
|
1 |
pbdom_obj_array[3].AddContent("This is Element 3.") |
This line of code changes the node tree as follows:
|
1 2 3 4 5 6 7 8 9 |
<Root> <Element_1> <Element_1_1/> <Element_1_2/> <Element_1_3/> </Element_1> <Element_2/> <Element_3>This is Element 3.</Element_3> </Root> |
Arrays and object references
When you use a method such as the GetContent method of the
PBDOM_DOCUMENT class to return an array of PBDOM_OBJECT references,
the references are to instantiated PBDOM objects. If you modify any of
these objects through its array item, the changes are permanent and
are reflected in any other arrays that hold the same object
reference.