Try OpenEdge Now
skip to main content
Working with XML
Reading and Writing XML Data from Temp-Tables and ProDataSets : Using XML Schema : Examples of additional XML support : XML schema with root element mapping to a temp-table
 
XML schema with root element mapping to a temp-table
The following example illustrates the functionality of creating a DATASET definition from an XML schema where the root element is mapped to a temp-table.
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<element name="person">
   <complexType>
    <sequence>
     <element name="name" type="xsd:string" />
     <element name="children">
      <complexType>
       <sequence>
        <element name="child" type="xsd:string" maxOccurs="unbounded"/>
       </sequence>
      </complexType>
    </element>
   </sequence>
  </complexType>
 </element>
</schema>
In the above XML Schema:
*The root element, person, is mapped to a temp-table with a single field, name.
*The nested child element, children, has no fields defined.
*The children element has a nested element, child, which maps to a temp-table with a single TEXT field.
The following is the static ProDataSet definition for the XML Schema:
DEFINE TEMP-TABLE person NO-UNDO
    FIELD name AS CHARACTER.

DEFINE TEMP-TABLE children NO-UNDO
    FIELD person_id as RECID XML-NODE-TYPE "Hidden".

DEFINE TEMP-TABLE child NO-UNDO
    FIELD child_Text AS CHARACTER XML-NODE-TYPE "Text"
    FIELD children_id AS RECID XML-NODE-TYPE "Hidden".

DEFINE DATASET personDset XML-NODE-TYPE "Hidden"
    FOR person, children, child
    PARENT-ID-RELATION Relation1 FOR person, children PARENT-ID-FIELD
   person_id
    PARENT-ID-RELATION Relation2 FOR children, child PARENT-ID-FIELD
    children_id.
The bproxsdto4gl utility defines ProDataSet named personDset, with temp-tables person and children. READ-XML() and READ-XMLSCHEMA() creates the dynamic equivalent of this static definition.
Since personDset is XML-NODE-TYPE "Hidden", the <personDset> element is not written during WRITE-XML() call. The root element of the XML document is <person>.
The ABL code required to create the data for personDset ProDataSet and generate the person.xml document is as follows:
CREATE person.
ASSIGN name = "Ken".

CREATE children.
ASSIGN person_id = RECID(person).

CREATE child.
ASSIGN child_Text = "Adam"
       Children_id = RECID(children).

CREATE child.
ASSIGN child_Text = "Elana"
Children_id = RECID(children).

DATASET personDset:WRITE-XML("file", "person.xml", TRUE).
The person.xml XML file generated is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<person>
   <name>Ken</name>
   <children>
      <child>Adam</child>
      <child>Elana</child>
   </children>
<person>