Consultor Eletrônico



Kbase 20724: XML. How to Use GET-CHILD and node:SUBTYPE to Get XML Values
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   10/30/2008
Status: Verified

GOAL:

How to Use GET-CHILD and node:SUBTYPE to Get XML Values

FACT(s) (Environment):

Progress 9.1C

FIX:

In an XML document with a default format, there is a new line and blank spaces between elements definitions.

The following is an example of two records in the Department table of sports2000 database:

<?xml version="1.0" ?>
<Department>
<Record>
<DeptCode>100</DeptCode>
<DeptName>Consulting</DeptName>
</Record>
<Record>
<DeptCode>200</DeptCode>
<DeptName>Administration</DeptName>
</Record>
</Department>

According to this example, the Root node is the 'Department' element and it has two child nodes ('Record' elements). Using the NUM-CHILDREN property for Root node to get the number of children, the property shows five children (and not two children) as 'Record' elements in the document.

This issue is because the new lines and blank spaces between record elements and department elements are nodes also.

Solution:
One way to resolve this issue is to use the SUBTYPE property. The SUBTYPE node property for an element is "ELEMENT" and the SUBTYPE node property for the new line and blank spaces is "TEXT".

Between the Field names elements is the field value:

<DeptCode>100</DeptCode>

The field value is a "TEXT" property too. The NODE-VALUE property gets the "TEXT" node value.

The following is an example of how to get the database record values into a XML Document:

DEFINE VARIABLE hXML AS HANDLE NO-UNDO.
DEFINE VARIABLE hRoot AS HANDLE NO-UNDO.
DEFINE VARIABLE hRecord AS HANDLE NO-UNDO.
DEFINE VARIABLE hXMLField AS HANDLE NO-UNDO.
DEFINE VARIABLE hFieldValue AS HANDLE NO-UNDO.

DEFINE VARIABLE iNumRecords AS INTEGER NO-UNDO.
DEFINE VARIABLE iNumFields AS INTEGER NO-UNDO.
DEFINE VARIABLE iNumValues AS INTEGER NO-UNDO.

CREATE X-DOCUMENT hXML.
CREATE X-NODEREF hRoot.
CREATE X-NODEREF hRecord.
CREATE X-NODEREF hXMLField.
CREATE X-NODEREF hFieldValue .

hXML:LOAD('FILE':U, "department.xml":U , FALSE).

hXML:GET-DOCUMENT-ELEMENT(hRoot).

REPEAT iNumRecords = 1 TO hRoot:NUM-CHILDREN:

hRoot:GET-CHILD(hRecord, iNumRecords).

IF hRecord:SUBTYPE NE 'ELEMENT':U THEN NEXT.

REPEAT iNumFields = 1 TO hRecord:NUM-CHILDREN:

hRecord:GET-CHILD(hXMLField, iNumFields).

IF hXMLField:SUBTYPE NE 'ELEMENT':U THEN NEXT.

REPEAT iNumValues = 1 TO hXMLField:NUM-CHILDREN:
hXMLField:GET-CHILD(hFieldValue, iNumValues).
IF hFieldValue:SUBTYPE NE 'TEXT':U THEN NEXT.
MESSAGE hFieldValue:NODE-VALUE.
END. /*REPEAT iNumV.alues = 1 TO hNumFields:NUM-CHILDREN:*/
END. /*REPEAT iNumFields = 1 TO hRecord:NUM-CHILDREN:*/

END. /*REPEAT iNumRecords = 1 TO hRoot:NUM-CHILDREN:*/.