Release 10.1A: OpenEdge Development:
Programming Interfaces

> ## Examples of reading an input XML file

> The following sample program shows reading in a file called “personal.xml”, processing through all the child nodes and displaying information if the node name is “person”:

> i-attnam.p
>     /* i-attnam.p */
>     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","personal.xml",TRUE).
>     hDoc:GET-DOCUMENT-ELEMENT(hRoot).

>     RUN GetChildren(hRoot, 1).
>     DELETE OBJECT hDoc.
>     DELETE OBJECT hRoot.

>     PROCEDURE GetChildren:
>     DEFINE INPUT PARAMETER hParent AS HANDLE.
>     DEFINE INPUT PARAMETER level AS INTEGER.
>     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 = "person" THEN
>             MESSAGE "getattr id gives" hNoderef:GET-ATTRIBUTE("id")
>               hNoderef:ATTRIBUTE-NAMES.
>         RUN GetChildren(hNoderef, (level + 1)).
>     END.

>     DELETE OBJECT hNoderef.
>     END PROCEDURE.

> The following program reads in the output file created by the previous program, i-outcus.p and creates temp-table entries:

> i-incus.p
>     /* i-incus.p - Import the Customer table from an xml file*/
>     DEFINE VARIABLE hDoc AS HANDLE.
>     DEFINE VARIABLE hRoot AS HANDLE.
>     DEFINE VARIABLE hTable AS HANDLE.
>     DEFINE VARIABLE hField AS HANDLE.
>     DEFINE VARIABLE hText AS HANDLE.
>     DEFINE VARIABLE hBuf AS HANDLE.
>     DEFINE VARIABLE hDBFld AS HANDLE.
>     DEFINE VARIABLE i AS INTEGER.
>     DEFINE VARIABLE j AS INTEGER.

>     /*so we can create new recs*/
>     DEFINE TEMP-TABLE Custt LIKE Customer.

>     CREATE X-DOCUMENT hDoc.
>     CREATE X-NODEREF hRoot.
>     CREATE X-NODEREF hTable.
>     CREATE X-NODEREF hField.
>     CREATE X-NODEREF hText.
>     hBuf = BUFFER Custt:HANDLE.

>     /*read in the file created in i-outcus.p*/
>     hDoc:LOAD("file", "cust.xml", FALSE).
>     hDoc:GET-DOCUMENT-ELEMENT(hRoot).

>     /*read each Customer from the root*/
>     REPEAT i = 1 TO hRoot:NUM-CHILDREN:
>         hRoot:GET-CHILD(hTable,i).
>         CREATE Custt.
>         /*get the fields given as attributes*/
>         cust-num = integer(hTable:GET-ATTRIBUTE("Cust-num")).
>         NAME = hTable:GET-ATTRIBUTE("Name").
>         /*get the remaining fields given as elements with text*/
>         REPEAT j = 1 TO hTable:NUM-CHILDREN:
>            hTable:GET-CHILD(hField,j).
>            IF hField:NUM-CHILDREN < 1 THEN NEXT.
>         /*skip any null value*/
>            hDBFld = hBuf:BUFFER-FIELD(hField:NAME).
>            hField:GET-CHILD(hText,1).
>      /*get the text value of the field*/
>            hDBFld:BUFFER-VALUE = hTEXT:NODE-VALUE.
>         END.
>     END.
>     DELETE OBJECT hDoc.
>     DELETE OBJECT hRoot.
>     DELETE OBJECT hTable.
>     DELETE OBJECT hField.
>     DELETE OBJECT hText.

>     /* show data made it by displaying temp-table */
>     FOR EACH Custt:
>         DISPLAY custt.
>     END.

Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095