24.03.2013 Views

OpenEdge Development: Working with XML - Product ...

OpenEdge Development: Working with XML - Product ...

OpenEdge Development: Working with XML - Product ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Reading and Writing <strong>XML</strong> <strong>with</strong> the Document Object Model (DOM)<br />

2–16<br />

You can obtain a list of an element’s attribute names using the ATTRIBUTE-NAMES attribute, get<br />

the value of an attribute by using the GET-ATTRIBUTE( ) method or set the value of an attribute<br />

by using the SET-ATTRIBUTE( ) method. You can also REMOVE-ATTRIBUTE( ), as shown:<br />

. . .<br />

REPEAT i = 1 TO hNode1:NUM-CHILDREN:<br />

logvar = hNode1:GET-CHILD(hChNode, i).<br />

IF NOT logvar THEN LEAVE.<br />

entries = hNode1:ATTRIBUTE-NAMES.<br />

REPEAT j = 1 TO NUM-ENTRIES(entries):<br />

aname = ENTRY(j, entries).<br />

MESSAGE "attrname is " aname "value is " hNode1:GET-ATTRIBUTE(aname).<br />

END.<br />

END.<br />

. . .<br />

In addition to creating nodes, you can IMPORT-NODE( ), CLONE-NODE( ), and DELETE-NODE( ).<br />

In addition to appending and getting a child, you can REMOVE-CHILD( ), REPLACE-CHILD( ),<br />

and GET-PARENT( ). The following example demonstrates the CLONE-NODE( ) method:<br />

i-clone.p (1 of 2)<br />

/* i-clone.p */<br />

DEFINE VARIABLE hXref AS HANDLE NO-UNDO.<br />

DEFINE VARIABLE hXref1 AS HANDLE NO-UNDO.<br />

DEFINE VARIABLE hText AS HANDLE NO-UNDO.<br />

DEFINE VARIABLE hText1 AS HANDLE NO-UNDO.<br />

DEFINE VARIABLE hClone AS HANDLE NO-UNDO.<br />

DEFINE VARIABLE hRoot AS HANDLE NO-UNDO.<br />

DEFINE VARIABLE hDoc AS HANDLE NO-UNDO.<br />

CREATE X-NODEREF hXref.<br />

CREATE X-NODEREF hXref1.<br />

CREATE X-NODEREF hText.<br />

CREATE X-NODEREF hText1.<br />

CREATE X-NODEREF hClone.<br />

CREATE X-NODEREF hRoot.<br />

CREATE X-DOCUMENT hDoc.<br />

hDoc:CREATE-NODE(hRoot,"root","ELEMENT").<br />

hDoc:INSERT-BEFORE(hRoot,?).<br />

hDoc:CREATE-NODE(hXref,"customer","ELEMENT").<br />

hDoc:CREATE-NODE(hXref1,"order","ELEMENT").<br />

hDoc:CREATE-NODE(hText,?,"TEXT").<br />

hDoc:CREATE-NODE(hText1,?,"TEXT").<br />

/* Add the two element nodes to the root, each <strong>with</strong> a text*/<br />

hXref:SET-ATTRIBUTE("id","54").<br />

hXref:SET-ATTRIBUTE("name","Second Skin Scuba").<br />

hRoot:APPEND-CHILD(hXref).<br />

hXref:APPEND-CHILD(hText).<br />

hXref1:SET-ATTRIBUTE("id","55").<br />

hXref1:SET-ATTRIBUTE("name","Off the Wall").<br />

hRoot:APPEND-CHILD(hXref1).<br />

hXref1:APPEND-CHILD(hText1).<br />

hText:NODE-VALUE = "hi from customer".<br />

hText1:NODE-VALUE = "hi from order".

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!