Try OpenEdge Now
skip to main content
Working with XML
Reading and Writing XML with the Document Object Model (DOM) : Reading XML input into ABL : Accessing the child nodes : Using node attributes and values
 
Using node attributes and values
You can get information about the child node by using various attributes and methods. For example, if you do not know how many nodes there are below the node referred to by the node reference, you can use the NUM-CHILDREN attribute. You can obtain or set the value of the node by using the NODE-VALUE attribute. For example:
DEFINE VARIABLE hChNode AS HANDLE NO-UNDO.
CREATE X-NODEREF hChNode.

REPEAT i = 1 TO hParent:NUM-CHILDREN:
  /* logvar is used for checking if the return value is zero. */
  logvar = hParent:GET-CHILD(hChNode, i).
  IF hChNode:NODE-VALUE > 0 THEN
    hChNode:NODE-VALUE = hChNode:NODE-VALUE + i.
. . .
You can obtain a list of an element's attribute names using the ATTRIBUTE-NAMES attribute, get the value of an attribute by using the GET-ATTRIBUTE( ) method or set the value of an attribute by using the SET-ATTRIBUTE( ) method. You can also REMOVE-ATTRIBUTE( ), as shown:
. . .
REPEAT i = 1 TO hNode1:NUM-CHILDREN:
logvar = hNode1:GET-CHILD(hChNode, i).
IF NOT logvar THEN LEAVE.
entries = hNode1:ATTRIBUTE-NAMES.

REPEAT j = 1 TO NUM-ENTRIES(entries):
aname = ENTRY(j, entries).
MESSAGE "attrname is " aname "value is " hNode1:GET-ATTRIBUTE(aname).
END.
END.
. . .
In addition to creating nodes, you can IMPORT-NODE( ), CLONE-NODE( ), and DELETE-NODE( ). In addition to appending and getting a child, you can REMOVE-CHILD( ), REPLACE-CHILD( ), and GET-PARENT( ). The following example demonstrates the CLONE-NODE( ) method:

i-clone.p

/* i-clone.p */
DEFINE VARIABLE hXref  AS HANDLE NO-UNDO.
DEFINE VARIABLE hXref1 AS HANDLE NO-UNDO.
DEFINE VARIABLE hText  AS HANDLE NO-UNDO.
DEFINE VARIABLE hText1 AS HANDLE NO-UNDO.
DEFINE VARIABLE hClone AS HANDLE NO-UNDO.
DEFINE VARIABLE hRoot  AS HANDLE NO-UNDO.
DEFINE VARIABLE hDoc   AS HANDLE NO-UNDO.

CREATE X-NODEREF hXref.
CREATE X-NODEREF hXref1.
CREATE X-NODEREF hText.
CREATE X-NODEREF hText1.
CREATE X-NODEREF hClone.
CREATE X-NODEREF hRoot.
CREATE X-DOCUMENT hDoc.

hDoc:CREATE-NODE(hRoot,"root","ELEMENT").
hDoc:INSERT-BEFORE(hRoot,?).
hDoc:CREATE-NODE(hXref,"customer","ELEMENT").
hDoc:CREATE-NODE(hXref1,"order","ELEMENT").
hDoc:CREATE-NODE(hText,?,"TEXT").
hDoc:CREATE-NODE(hText1,?,"TEXT").

/* Add the two element nodes to the root, each with a text*/
hXref:SET-ATTRIBUTE("id","54").
hXref:SET-ATTRIBUTE("name","Second Skin Scuba").
hRoot:APPEND-CHILD(hXref).
hXref:APPEND-CHILD(hText).
hXref1:SET-ATTRIBUTE("id","55").
hXref1:SET-ATTRIBUTE("name","Off the Wall").
hRoot:APPEND-CHILD(hXref1).
hXref1:APPEND-CHILD(hText1).
hText:NODE-VALUE = "hi from customer".
hText1:NODE-VALUE = "hi from order".
hXref1:CLONE-NODE(hClone,TRUE).
hRoot:APPEND-CHILD(hClone).

/* Save the file */
hDoc:SAVE("file","clone1.xml").
DELETE OBJECT hXref.
DELETE OBJECT hXref1.
DELETE OBJECT hText.
DELETE OBJECT hText1.
DELETE OBJECT hClone.
DELETE OBJECT hRoot.
DELETE OBJECT hDoc.
There are more methods and attributes that apply to the X-document object and the X-noderef objects. For more information on these attributes and methods, see their entries in the OpenEdge Development: ABL Reference.