Consultor Eletrônico



Kbase P44514: How to use REMOVE-CHILD() method to remove a specific node f
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   2/12/2004
Status: Unverified

GOAL:

How to use REMOVE-CHILD() method to remove a specific node from an XML document?

FIX:

The following code removes the "Comments" node from a the file named "Customer.xml".  Both the original "Customer.xml" and the resulting "NewCustomer.xml" files are listed after the code:

/* The 4 GL code */

DEFINE VARIABLE hDoc AS HANDLE.
DEFINE VARIABLE hRoot AS HANDLE.
DEFINE VARIABLE good AS LOGICAL.

CREATE X-DOCUMENT hDoc.
CREATE X-NODEREF hRoot.
hDoc:LOAD("FILE","Customer.xml",FALSE).
hDoc:GET-DOCUMENT-ELEMENT(hRoot).
RUN PruneDoc(hRoot).
hDoc:SAVE("FILE","NewCustomer.xml").

DELETE OBJECT hDoc.
DELETE OBJECT hRoot.

PROCEDURE PruneDoc:
DEFINE INPUT PARAMETER hParent AS HANDLE.
DEFINE VARIABLE i AS INTEGER.
DEFINE VARIABLE hNoderef AS HANDLE.

CREATE X-NODEREF hNoderef.
REPEAT i = 1 TO hParent:NUM-CHILDREN:
good = hParent:GET-CHILD(hNoderef,i).
IF NOT good THEN LEAVE.
IF hNoderef:SUBTYPE <> "element" THEN NEXT.
IF hNoderef:NAME = "Comments" THEN
hParent:REMOVE-CHILD(hNoderef).
ELSE
RUN PruneDoc(hNoderef).
END.
DELETE OBJECT hNoderef.
END PROCEDURE.

/* The Original Customer.xml file */
<?xml version="1.0" encoding="UTF-8" ?>
<Customers>
<Customer Customer_Name="jack" Customer_Number="1">
<Country>USA</Country>
<Address>276 North Drive</Address>
<Address2 />
<City>Burlington</City>
<State>MA</State>
<PostalCode>01730</PostalCode>
<Contact>Gloria Shepley</Contact>
<Phone>(617) 450-0086</Phone>
<SalesRep>HXM</SalesRep>
<CreditLimit>66,700</CreditLimit>
<Balance>903.64</Balance>
<Terms>Net30</Terms>
<Discount>35%</Discount>
<Comments />
<Fax />
<EmailAddress />
</Customer>
<Customer Customer_Number="2106" Customer_Name="jill">
<Country>USA</Country>
<Address>101 COLLEGE RD</Address>
<Address2 />
<City>Fairbanks</City>
<State>AK</State>
<PostalCode>99701</PostalCode>
<Contact>A Rockwell</Contact>
<Phone>(907) 451-7555</Phone>
<SalesRep>SLS</SalesRep>
<CreditLimit>10,300</CreditLimit>
<Balance>0.00</Balance>
<Terms>Net30</Terms>
<Discount>5%</Discount>
<Comments />
<Fax />
<EmailAddress />
</Customer>
</Customers>

/* The new NewCustomer.xml file */
<?xml version="1.0" encoding="UTF-8" ?>
<Customers>
<Customer Customer_Name="jack" Customer_Number="1">
<Country>USA</Country>
<Address>276 North Drive</Address>
<Address2/>
<City>Burlington</City>
<State>MA</State>
<PostalCode>01730</PostalCode>
<Contact>Gloria Shepley</Contact>
<Phone>(617) 450-0086</Phone>
<SalesRep>HXM</SalesRep>
<CreditLimit>66,700</CreditLimit>
<Balance>903.64&.lt;/Balance>
<Terms>Net30</Terms>
<Discount>35%</Discount>

<Fax/>
<EmailAddress/>
</Customer>
<Customer Customer_Name="jill" Customer_Number="2106">
<Country>USA</Country>
<Address>101 COLLEGE RD</Address>
<Address2/>
<City>Fairbanks</City>
<State>AK</State>
<PostalCode>99701</PostalCode>
<Contact>A Rockwell</Contact>
<Phone>(907) 451-7555</Phone>
<SalesRep>SLS</SalesRep>
<CreditLimit>10,300</CreditLimit>
<Balance>0.00</Balance>
<Terms>Net30</Terms>
<Discount>5%</Discount>

<Fax/>
<EmailAddress/>
</Customer>
</Customers>.