XFILE Support


9.0D

 

Since data in an XML document is represented in a parent-child relationship, it cannot be easily handled using the standard FILE operations.

 

An XFILE is an abstract representation of an XML document that loads, examines, modifies, and saves XML data. A XFILE will create a memory resident representation of the elements inside of an XML document. The elements are broken into records and data fields.

 

A record is:

 

  1. An XML element that has attributes

  2. An XML element that has child elements

 

A data field is:

 

  1. An XML element that has no attributes or child elements

  2. An XML attribute of an XML element considered to be a record

 

If an XML element is considered to be a data field but repeats, it is a data field array.

 

If an XML element has both attributes and text, a data field named "Record_Text" is created to reference the text.

 

Each record can contain other records and data fields.

 

Records are then grouped into recordsets where the record name (element tag name) is used for the grouping. Records within records are also grouped into recordsets.

 

A XFILE corresponds to one record set. When a record set contains other recordsets, multiple XFILEs must be used to access the contained recordsets.

Example 1:

 

In this first example, the <PEOPLE> element tag is the XML root element. Also, the <PEOPLE> tag identifies a record set that contains a single embedded record set named <PERSON>. The <PERSON> tag identifies a record set that has two records. Each record in the <PERSON> record set has four data fields that are reference by the XML element names of PERSONID, NAME, ADDRESS, and PHONE. In this case, the first data field name PERSONID is an attribute for each record in the <PERSON> record set. The other data fields named NAME, ADDRESS, and PHONE are contained elements for each record in the <PERSON> record set.

 

<?xml version="1.0"?>

<PEOPLE>

     <PERSON PERSONID="5622">

           <NAME>John Smith</NAME>

           <ADDRESS>512 Every Road</ADDRESS>

           <PHONE>(111) 123-6645</PHONE>      

     </PERSON>

     <PERSON PERSONID="6109">

           <NAME>James Jones</NAME>

           <ADDRESS>513 Every Road</ADDRESS>

           <PHONE>(111) 123-4567</PHONE>

     </PERSON>

</PEOPLE>

 

Record Set 1:

PEOPLE

Contained Recordsets:

PERSON

Data Fields:

None

 

Record Set 2:

PERSON with 2 records

Contained Recordsets:

None

Data Fields:

PERSONID, NAME, ADDRESS, PHONE

 

The following PL/B code is a sample set of instructions for the example 1 XML structure that shows how XFILE data variables are used to read and update the data in the XML file.

Note the following about this example:

  1. Because the XML file contains two recordsets, two XFILE data variables must be used to access and manipulate these recordsets.

  2. The 'People' XFILE is opened to reference all elements in the XML data file.

  3. A READ operation is executed for the ‘People’ XFILE to access the next sequential record set and that record set is associated with the ‘Person’ XFILE. This operation acts as if an implied open operation was performed for the embedded <PERSON> record set.

  4. The READ operation for the ‘Person’ XFILE is performed to retrieve the data fields identified by ‘PERSONID=id’ and ‘NAME=name’. Notice, that the ADDRESS and PHONE data fields are not being retrieved.

  5. The UPDATE operation for the ‘Person’ XFILE is updating a single data field for the last ‘Person’ record that was previously read successfully.

  6. When the CLOSE for the ‘Person’ XFILE is executed, the resources are released.

  7. When the CLOSE for the ‘People’ XFILE is executed, all of the current XML file data is written back to the physical disk data file that is associated with the ‘People’ XFILE.

 

 

People

XFILE

 

Person

XFILE

 

SEQ

FORM

"-1"

.

 

 

 

OPEN

People,"people.xml"

 

READ

People,SEQ;PERSON=Person

 

READ

Person,SEQ;PERSONID=id,NAME=name

 

UPDATE

Person;ADDRESS=newAddress

 

CLOSE

Person

 

CLOSE

People

 

Example 2:

 

<?xml version="1.0"?>

<LIBRARY>

     <BOOK>

           <NAME>Programming with PL/B</NAME>

           <TYPE>Computer</TYPE>      

     </BOOK>

     <MAGAZINE>

           <NAME>Visual PL/B World</NAME>

           <ISSUE>45</ISSUE>      

     </MAGAZINE>

     <BOOK>

           <NAME>My Life As A PL/B Programmer</NAME>

           <TYPE>Adventure</TYPE>

     </BOOK>

</LIBRARY>

 

 

Record Set 1:

LIBRARY

Contained Recordsets:

BOOK, MAGAZINE

Data Fields:

None

 

Recordset 1:

BOOK with 2 records

Contained Recordsets:

None

Data Fields:

NAME, TYPE

 

Record Set 1:

MAGAZINE with 1 record

Contained Recordsets:

None

Data Fields:

NAME, ISSUE

 

 

Library

XFILE

 

Book

XFILE

 

Mag

XFILE

 

SEQ

FORM

"-1"

EOF

FORM

"-3"

 

 

 

 

OPEN

Library,"library.xml"

 

READ

Library,SEQ;BOOK=Book,MAGAZINE=Mag

 

WRITE

Book,EOF;NAME="PL/B Samples",TYPE="Programming"

 

WRITE

Mag,EOF;Name="Visual PL/B World",ISSUE="46"

 

CLOSE

Book

 

CLOSE

Mag

 

CLOSE

Library

 

 

See Also: XML Support



PL/B Language Reference XML Document Structure XML Schema Support