New in version 6.1.0: See the new features.

Read XML

read_xml_values()

Read the XML content into a !record


SYNTAX

read_xml_values(filename|channel|string, recordId [, options])

That method will open the requested file or channel and read all the XML key/value pairs setting the values on the fields with name equal to the key.

filename

XML file to read

channel

Since version 6.2.0 a channel can be specified to read a JSON (open sequential file, pipe, …).

string

XML in memory string when the option JSONXML_MEMORY is specified.

recordId

Record where to read the XML values into

options

Since Sculptor version 6.2.0 JSONXML_MEMORY specifies that the first parameter is a XML string in memory (probably an alpha packed field)

It returns the number of keys read from the XML content (0 if none)

Note

New in version 6.2.0: See the new features.

  • Nested objects are supported since the nested record feature.

  • Array of objects are supported since the array record feature.

  • The field arrays can be autoredim’ed if the “a” flag is set.

  • The !record arrays can be autoredim’ed if the RECFL_AUTOREDIM flag is set.

  • Sequential files can be specified in place of a file name.

  • To address the problem of reading/writting a json/xml tag which happens to be a Sculptor reserved word (creating a field on a record with that name is not possible). It’s possible to define the reserved word with an underscore “_” in front of it. Not clashing with the reserved word any more. As a rule, when writting/reading the first “_” will be removed (if an underscore is needed just add one more). E.g:

    !record Rec {
        _image,,a64
    }
    
  • Now the function treats an ASCII or binary array with a “p” (packed flag) flag as one big field, so that large XML fields can be read. E.g.:

    !record Rec {
        large8kDataString,,a128[64],,p
    }
    

EXAMPLE

A XML file (e.g “file.xml”) with this content:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <arrIntsVal>
        <element>1</element>
        <element>2</element>
        <element>3</element>
        <element>4</element>
        <element>5</element>
        <element>6</element>
        <element>7</element>
        <element>8</element>
        <element>9</element>
        <element>0</element>
    </arrIntsVal>
    <boolVal>true</boolVal>
    <intVal>123</intVal>
    <realVal>123.123</realVal>
    <strVal>XML String</strVal>
</root>

Can be read with this Sculptor code:

!record infoXml {
    boolVal,,u1
    intVal,,i4
    realVal,,r8
    strVal,,a128
    arrIntsVal,,i4[10]
}

num = read_xml_values("file.xml", infoxml)
prompt "Read " + tostr(num) + " Xml attributes"

NOTE: this array XML syntax is also supported:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <boolVal>1</boolVal>
    <intVal>123</intVal>
    <realVal>123.123</realVal>
    <strVal>XML String</strVal>
    <arrIntsVal>1</arrIntsVal>
    <arrIntsVal>2</arrIntsVal>
    <arrIntsVal>3</arrIntsVal>
    <arrIntsVal>4</arrIntsVal>
    <arrIntsVal>5</arrIntsVal>
    <arrIntsVal>6</arrIntsVal>
    <arrIntsVal>7</arrIntsVal>
    <arrIntsVal>8</arrIntsVal>
    <arrIntsVal>9</arrIntsVal>
    <arrIntsVal>0</arrIntsVal>
</root>

Since Sculptor version 6.2.0 this XML content:

<?xml version="1.0" encoding="UTF-8"?>
<SrcRec>
    <element>
    <pre>1234</pre>
    <txt>Top     </txt>
    <SubRec>
        <num>
            <element>1111</element>
            <element>1112</element>
        </num>
        <txt>SubRec1 </txt>
    </SubRec>
    <num>
        <element>1123</element>
        <element>2123</element>
    </num>
    <SubRec2>
        <num>
            <element>1211</element>
            <element>1212</element>
        </num>
        <txt>SubRec21</txt>
    </SubRec2>
    <post>225</post>
    </element>
    <element>
    <pre>2234</pre>
    <txt>Top2    </txt>
    <SubRec>
        <num>
            <element>2111</element>
            <element>2112</element>
        </num>
        <txt>2ubRec1 </txt>
    </SubRec>
    <num>
        <element>1122</element>
        <element>2122</element>
    </num>
    <SubRec2>
        <num>
            <element>2211</element>
            <element>2212</element>
        </num>
        <txt>2ubRec21</txt>
    </SubRec2>
    <post>226</post>
    </element>
</SrcRec>

Can be read with this Sculptor code:

!record SrcRec[1] {
    pre,,i2
    txt,,a8
    !record SubRec {
        num,,i4[2]
        txt,,a8
    }
    num,,i4[2]
    !record SubRec2 = SrcRec.SubRec
    post,,i2
}

SrcRec->flags = RECFL_AUTOREDIM

/* Open channel 1 as stdin */
open #1, "stdin" read

read_xml_values(1, SrcRec)
/* The SrcRec first dimension is automatically redim'ed from 1 to 2 and the XML is read */

RELATED TOPICS

write_xml_values

!record

Sequential files

JSONXML_ options