File: xml-doc.2gg

package info (click to toggle)
golf 601.4.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,824 kB
  • sloc: ansic: 20,020; sh: 1,171; makefile: 292
file content (156 lines) | stat: -rw-r--r-- 5,424 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
.TH GOLF 2gg $VERSION $DATE Development Tools
.SH NAME
xml-doc \-  (XML-parsing)
.SH PURPOSE
Parse XML text.

.SH SYNTAX

.RS 4
.EX

xml-doc <text> to <xml> \\
    [ status <status> ] [ length <length> ] \\
    [ error-text <error text> ] \\
        [ error-line <line> ] \\
        [ error-char <char> ] 

xml-doc delete <xml>

.EE
.RE

.SH DESCRIPTION
xml-doc will parse XML <text> into <xml> variable, which can be used with \fBread-xml\fP to get the data.

The length of <text> may be specified with "length" clause in <length> variable, or if not, it will be the string length of <text>.

The "status" clause specifies the return <status> number, which is GG_OKAY if successful or GG_ERR_XML if there is an error in parsing, in which case <error text> in "error-text" clause is the error message. You can also obtain the error's line number (with number <line> in clause "error-line") and the character position within that line (with number <char> in clause "error-char").

The maximum depth of nested elements in XML document is 32, and the maximum length of a normalized element name is 500 (see \fBread-xml\fP for more on normalized names). 

An XML element that is comprised of multiple anonymous sub-elements is presented as a concatenated single element; this is likely how most applications expect to see such data. All data is trimmed for whitespace on left and right.

Golf uses libxml2 library for XML parsing.

.LP
.B DELETING
.LP

To delete an XML variable, use "delete" clause with the <xml>. This will delete all memory associated with it, and the variable <xml> itself.

.SH EXAMPLES
Parse the following XML document and display all element names and values from it. You can use them as they come along, or store them into \fBnew-hash\fP or \fBnew-tree\fP for instance for searching of large documents. This also demonstrates usage of UTF characters:

.RS 4
.EX

%% /xml public
    set-string xmld unquoted =<?xml version="1.0" encoding="UTF-8"?>\\
<countries>\\
    <country id='1'>\\
        <name>USA&amp;America</name>\\
        <state id='1'>Arizona\\
            <city id="1">\\
                <name>Phoenix</name>\\
                <population>5000000</population>\\
            </city>\\
            <city id="2">\\
                <name>Tuscon</name>\\
                <population>1000000</population>\\
            </city>\\
        </state>\\
        <state id='2'>California\\
            <city id="1">\\
                <name>Los Angeles</name>\\
                <population>19000000</population>\\
            </city>\\
            <city id="2">\\
                <name>Irvine</name>\\
            </city>\\
        </state>\\
    </country>\\
    <country id='2'>\\
        <name>Mexico</name>\\
        <state id='1'>Veracruz\\
            <city id="1">\\
                <name>Xalapa-Enriquez</name>\\
                <population>8000000</population>\\
            </city>\\
            <city id="2">\\
                <name>C\eu00F3rdoba</name>\\
                <population>220000</population>\\
            </city>\\
        </state>\\
        <state id='2'>Sinaloa\\
            <city id="1">\\
                <name>Culiac\eu00E1n Rosales</name>\\
                <population>3000000</population>\\
            </city>\\
        </state>\\
    </country>\\
</countries>

    xml-doc xmld to x
    start-loop
        read-xml x key k value v status s next
        if-true s equal GG_ERR_EXIST
            break-loop
        end-if
        text-utf v 
        @Key [<<print-out k>>] Value [<<print-out v>>] 
    end-loop
    xml-doc delete x 
%%

.EE
.RE

The output would be:

.RS 4
.EX

Key [countries/country/id/@] Value [1]
Key [countries/country/name/] Value [USA&America]
Key [countries/country/state/id/@] Value [1]
Key [countries/country/state/city/id/@] Value [1]
Key [countries/country/state/city/name/] Value [Phoenix]
Key [countries/country/state/city/population/] Value [5000000]
Key [countries/country/state/city/id/@] Value [2]
Key [countries/country/state/city/name/] Value [Tuscon]
Key [countries/country/state/city/population/] Value [1000000]
Key [countries/country/state/] Value [Arizona]
Key [countries/country/state/id/@] Value [2]
Key [countries/country/state/city/id/@] Value [1]
Key [countries/country/state/city/name/] Value [Los Angeles]
Key [countries/country/state/city/population/] Value [19000000]
Key [countries/country/state/city/id/@] Value [2]
Key [countries/country/state/city/name/] Value [Irvine]
Key [countries/country/state/] Value [California]
Key [countries/country/id/@] Value [2]
Key [countries/country/name/] Value [Mexico]
Key [countries/country/state/id/@] Value [1]
Key [countries/country/state/city/id/@] Value [1]
Key [countries/country/state/city/name/] Value [Xalapa-Enriquez]
Key [countries/country/state/city/population/] Value [8000000]
Key [countries/country/state/city/id/@] Value [2]
Key [countries/country/state/city/name/] Value [Córdoba]
Key [countries/country/state/city/population/] Value [220000]
Key [countries/country/state/] Value [Veracruz]
Key [countries/country/state/id/@] Value [2]
Key [countries/country/state/city/id/@] Value [1]
Key [countries/country/state/city/name/] Value [Culiacán Rosales]
Key [countries/country/state/city/population/] Value [3000000]
Key [countries/country/state/] Value [Sinaloa]

.EE
.RE

.SH SEE ALSO
 XML parsing

\fBread-xml\fP  
\fBxml-doc\fP   
See all 
\fBdocumentation\fP