Creating an XML document from scratch
You can create an XML document in a script using the appropriate
PBDOM_OBJECT subclasses and methods. The following code uses the
PBDOM_ELEMENT and PBDOM_DOCUMENT classes and some of their methods to
create a simple XML document.
First, the objects are declared and instantiated:
|
1 2 3 4 5 6 7 8 9 |
PBDOM_ELEMENT pbdom_elem_1 PBDOM_ELEMENT pbdom_elem_2 PBDOM_ELEMENT pbdom_elem_3 PBDOM_ELEMENT pbdom_elem_root PBDOM_DOCUMENT pbdom_doc1 pbdom_elem_1 = Create PBDOM_ELEMENT pbdom_elem_2 = Create PBDOM_ELEMENT pbdom_elem_3 = Create PBDOM_ELEMENT |
The instantiated objects are assigned names. Note that the
PBDOM_DOCUMENT object pbdom_doc1 is not named:
|
1 2 3 |
pbdom_elem_1.SetName("pbdom_elem_1") pbdom_elem_2.SetName("pbdom_elem_2") pbdom_elem_3.SetName("pbdom_elem_3") |
The objects are arranged into a node tree using the
AddContent method. The AddContent method adds the referenced object as a
child node under the object from which AddContent is invoked:
|
1 2 |
pbdom_elem_1.AddContent(pbdom_elem_2) pbdom_elem_2.AddContent(pbdom_elem_3) |
Use the NewDocument method to create a new XML document. The
parameter value supplied to the NewDocument method becomes the name of
the root element. This name is then accessed from the PBDOM_DOCUMENT
object pbdom_doc1 and assigned to the PBDOM_ELEMENT object
pbdom_elem_root using the GetRootElement method:
|
1 2 |
pbdom_doc1.NewDocument("Root_Element_From_Doc_1") pbdom_elem_root = pbdom_doc1.GetRootElement() |
The ELEMENT object pbdom_elem_1 and all its child nodes are placed
in the new XML document node tree under the root element using the
AddContent method. Note that as the ancestor node pbdom_elem_1 is placed
in the node tree, all its child nodes move as well:
|
1 |
pbdom_elem_root.AddContent(pbdom_elem_1) |
The XML document created looks like this:
|
1 2 3 4 5 6 7 8 |
<!DOCTYPE Root_Element_From_Doc_1> <Root_Element_From_Doc_1> <pbdom_elem_1> <pbdom_elem_2> <pbdom_elem_3/> </pbdom_elem_2> </pbdom_elem_1> </Root_Element_From_Doc_1> |