New in version 6.1.0: See the new features.

Write XML

write_xml_values()

Write the XML content from a !record


SYNTAX

write_xml_values(filename|channel|field, recordId [, options])

That method will create the requested content and write all the XML key/value pairs from the values of the record fields where the key is the field name and the value it’s content.

filename

XML file to write

channel

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

field

Field to receive the XML string in memory when the option JSONXML_MEMORY is specified.

recordId

Record where to write the XML values into

options

JSONXML_MINIFY removes all unnecessary spaces and characters to reduce the transfered size.

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

It returns the number of keys written into the XML file or channel (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.

  • 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 written. E.g.:

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

New in version 6.3.0: See the new features.

The character conversion honors the value set in sys.CharSetEncodeFrom and sys.CharSetEncodeTo, so that the string received is supposed to be sys.CharSetEncodeFrom encoded while the result is converted into the sys.CharSetEncodeTo encoding.


EXAMPLE

This code writes the !record content into a file as XML:

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

for (num=1; num <= 10; num++) {
    infoXml.arrIntsVal[num] = num
}
infoXml.boolVal = TRUE
infoXml.intVal = 543
infoXml.realVal = 543.43
infoXml.strVal = "Writting XML"

num = write_xml_values("file.xml", infoXml)
prompt "Written " + tostr(num) + " Xml attributes"

An XML file (e.g “file.xml”) with this content is created:

<?xml version="1.0" encoding="UTF-8"?>
<infoXml>
    <boolVal>1</boolVal>
    <intVal>543</intVal>
    <realVal>543.43</realVal>
    <strVal>Writting XML</strVal>
    <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>10</element>
    </arrIntsVal>
</infoXml>

Since Sculptor version 6.2.0 this XML:

<?xml version="1.0" encoding="UTF-8"?>
<SrcRec>
    <pre>1234</pre>
    <txt>Top     </txt>
    <SubRec>
        <num>
            <element>1111</element>
            <element>2121</element>
        </num>
        <txt>SubRec  </txt>
    </SubRec>
    <num>
        <element>1123</element>
        <element>2123</element>
    </num>
    <SubRec2>
        <num>
            <element>1010</element>
            <element>2020</element>
        </num>
        <txt>SubRec2 </txt>
    </SubRec2>
    <post>225</post>
</SrcRec>

Can be written into a pipe with this Sculptor code:

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

/* Fill SrcRec with information */
...

/* Execute a background "cmd" program using channel 1 as their stdin (used here as stdout) */
execute(NULL, cmd, NULL, 0, AA_SHOWNORMAL, 1, 0, 0)

/* Send the SrcRec in JSON format to the "cmd" program */
write_xml_values(1, SrcRec, JSONXML_MINIFY)

RELATED TOPICS

read_xml_values

!record

Sequential files

JSONXML_ options

sys.CharSetEncodeFrom and sys.CharSetEncodeTo