xmlrpc::construct (private)

 xmlrpc::construct context arglist

Defined in packages/xml-rpc/tcl/xml-rpc-procs.tcl

Construct an XML-RPC element. arglist is a 2-element list which is converted to XML. The first element of arglist is the datatype and the second element is the value.

Example:
    set arglist {-int 33}
    set result [xmlrpc::construct {} $arglist]
    set result ==> <i4>33</i4>
    

This proc works recursively, so if your top level list has a list within it, then that list will be processed first. The two examples of this are arrays and structs. In addition, structs and arrays can contain each other.

Array example:
    set arglist {-array {
        {-int 6682}
        {-boolean 0}
        {-text Iowa}
        {-double 8931.33333333}
        {-date {Fri Jan 01 05:41:30 EST 1904}}}}

    set result [xmlrpc::construct {} $arglist]
    set result ==>  <array>
                    <data>
                        <value>
                            <i4>6682</i4>
                        </value>
                        <value>
                            <boolean>0</boolean>
                        </value>
                        <value>
                            <string>Iowa</string>
                        </value>
                        <value>
                            <double>8931.33333333</double>
                        </value>
                        <value>
                            <dateTime.iso8601>19040101T05:41:30</dateTime.iso8601>
                        </value>
                    </data>
                </array>
    

struct's have the special format: -struct {name1 {-datatype1 value1} name2 {-datatype2 value2}}

Struct Example:
    set arglist {-struct {
        ctLeftAngleBrackets {-int 5}
        ctRightAngleBrackets {-int 6}
        ctAmpersands {-int 7}
        ctApostrophes {-int 0}
        ctQuotes {-int 3}}}

    set result [xmlrpc::construct {} $arglist]
    set result ==>  <struct>
                    <member>
                        <name>ctLeftAngleBrackets</name>
                        <value>
                            <i4>5</i4>
                        </value>
                    </member>
                    <member>
                        <name>ctRightAngleBrackets</name>
                        <value>
                            <i4>6</i4>
                        </value>
                    </member>
                    <member>
                        <name>ctAmpersands</name>
                        <value>
                            <i4>7</i4>
                        </value>
                    </member>
                    <member>
                        <name>ctApostrophes</name>
                        <value>
                            <i4>0</i4>
                        </value>
                    </member>
                    <member>
                        <name>ctQuotes</name>
                        <value>
                            <i4>3</i4>
                        </value>
                    </member>
                </struct>
    

The context parameter is used internally to create tags within tags.

Example:
    set arglist {-int 33}
    set result [xmlrpc::construct {foo bar} $arglist]
    set result ==> <foo><bar><i4>33</i4></bar></foo>
    

Parameters:
context - extra tags to wrap around the data
arglist - datatype-value list (or more complex types as described above)
Returns:
XML formatted result

Partial Call Graph (max 5 caller/called nodes):
%3 test_xml_rpc_construct xml_rpc_construct (test xml-rpc) xmlrpc::construct xmlrpc::construct test_xml_rpc_construct->xmlrpc::construct xmlrpc::create_context xmlrpc::create_context (private) xmlrpc::construct->xmlrpc::create_context xmlrpc::remote_call xmlrpc::remote_call (public) xmlrpc::remote_call->xmlrpc::construct xmlrpc::respond xmlrpc::respond (private) xmlrpc::respond->xmlrpc::construct

Testcases:
xml_rpc_construct
Source code:
    set result ""
    # list of valid options
    set options_list [list "-string" "-text" "-i4" "-int" "-integer"  "-boolean" "-double" "-date" "-binary" "-base64"  "-variable" "-structvariable" "-struct"  "-array" "-keyvalue"]

    # if no valid option is specified, treat it as string
    if {[lsearch $options_list [lindex $arglist 0]] == -1} {
        set value "<string>[ns_quotehtml $arglist]</string>"
        return [xmlrpc::create_context $context $arglist]
    }

    if { [llength $arglist] % 2} {
        # datatype required for each value
        return -code error  "no value for option \"[lindex $arglist end]\""
    }

    foreach {option value} $arglist {
        switch -- $option {
            -string -
            -text {
                set value "<string>[ns_quotehtml $value]</string>"
                append result [xmlrpc::create_context $context $value]
            }

            -i4 -
            -int -
            -integer {
                if {![string is integer $value]} {
                    return -code error  "value \"$value\" for option \"$option\" is not an integer:"
                }
                set value "<i4>$value</i4>"
                append result [xmlrpc::create_context $context $value]
            }

            -boolean {
                set value "<boolean>[string is true $value]</boolean>"
                append result [xmlrpc::create_context $context $value]
            }

            -double {
                if {![string is double $value]} {
                    return -code error  "value \"$value\" for option \"$option\" is not a floating point value"
                }
                set value "<double>$value</double>"
                append result [xmlrpc::create_context $context $value]
            }

            -date {
                if {[catch {clock format [clock scan $value]  -format {%Y%m%dT%T} } datevalue]} {
                    return -code error  "value \"$value\" for option \"$option\" is not a valid date ($datevalue)"
                }

                set value "<dateTime.iso8601>$datevalue</dateTime.iso8601>"
                append result [xmlrpc::create_context $context $value]
            }

            -binary -
            -base64 {
                # it is up to the application to do the encoding
                # before the data gets here
                set value "<base64>$value</base64>"
                append result [xmlrpc::create_context $context $value]
            }

            -array {
                set data "<array><data>"
                foreach datum $value {
                    append data [xmlrpc::construct value $datum]
                }
                append data "</data></array>"
                append result [xmlrpc::create_context $context $data]
            }

            -struct -
            -keyvalue {
                set data "<struct>"
                foreach {name mvalue} $value {
                    append data "<member><name>[ns_quotehtml $name]</name>"
                    append data [xmlrpc::construct value $mvalue]
                    append data "</member>"
                }
                append data "</struct>"
                append result [xmlrpc::create_context $context $data]
            }

            default {
                # anything else will be ignored
                ns_log notice xmlrpc::construct ignored option: $option  with value: $value
            }
        }
    }

    return $result
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: