• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# XML Parsing
2
3
4When using XML as a data carrier, it is necessary to parse relevant nodes in practice. This typically includes three types of operations: [parsing XML tags and their values](#parsing-xml-tags-and-values), [parsing XML attributes and their values](#parsing-xml-attributes-and-values), and [parsing XML event types and element depths](#parsing-xml-event-types-and-element-depths). For example, in web services, XML is the foundation of the Simple Object Access Protocol (SOAP). SOAP messages, which are usually encapsulated in XML format and contain request and response parameters, are parsed by web services to process client requests and generate corresponding responses.
5
6
7The XML module provides the **XmlPullParser** class to parse XML documents. The input is an ArrayBuffer or DataView containing XML text, and the output is the parsed information.
8
9**Table 1** XML parsing options (see [ParseOptions](../reference/apis-arkts/js-apis-xml.md#parseoptions) for detailed descriptions)
10
11| Name| Type| Mandatory| Description|
12| -------- | -------- | -------- | -------- |
13| supportDoctype | boolean | No| Whether to parse the document type. The value **true** means to parse the document type, and **false** means the opposite. The default value is **false**.|
14| ignoreNameSpace | boolean | No| Whether to ignore the namespace. If the namespace is ignored, it will not be parsed. The value **true** means to ignore the namespace, and **false** means the opposite. The default value is **false**.|
15| tagValueCallbackFunction | (name: string, value: string) => boolean | No| Callback used to return **tagValue**, which consists of a tag and its value. The default value is **null**, indicating that XML tags and values are not parsed.|
16| attributeValueCallbackFunction | (name: string, value: string) => boolean | No| Callback used to return **attributeValue**, which consists of an attribute and its value. The default value is **null**, indicating that XML attributes and values are not parsed.|
17| tokenValueCallbackFunction | (eventType: EventType, value: ParseInfo) => boolean | No| Callback used to return **tokenValue**, which consists of the event type and the attributes of **parseInfo**. The default value is **null**, indicating that the event type and the attributes of **parseInfo** are not parsed.|
18
19
20## Precautions
21
22- To ensure successful XML parsing and conversion, the input XML data must comply with the standard format.
23
24- Currently, parsing a given node is not supported.
25
26
27## Parsing XML Tags and Values
28
291. Import the module.
30
31    ```ts
32    import { xml, util } from '@kit.ArkTS'; // Use the API provided by the util module to encode the file.
33    ```
34
352. Create an XmlPullParser object.
36
37   You can construct an XmlPullParser object based on ArrayBuffer or DataView. Both methods yield the same results.
38
39    ```ts
40    let strXml: string =
41    '<?xml version="1.0" encoding="utf-8"?>' +
42      '<note importance="high" logged="true">' +
43      '<title>Play</title>' +
44      '<lens>Work</lens>' +
45      '</note>';
46    let textEncoder: util.TextEncoder = new util.TextEncoder();
47    let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters.
48    // Method 1: Create an XmlPullParser object based on ArrayBuffer.
49    let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer as object as ArrayBuffer, 'UTF-8');
50
51    // Method 2: Create an XmlPullParser object based on DataView.
52    // let dataView: DataView = new DataView(arrBuffer.buffer as object as ArrayBuffer);
53    // let that: xml.XmlPullParser = new xml.XmlPullParser(dataView, 'UTF-8');
54    ```
55
563. Customize a callback function. In this example, the callback function directly prints the tags and their values.
57
58    ```ts
59    function func(name: string, value: string): boolean {
60      if (name == 'note') {
61        console.info(name);
62      }
63      if (value == 'Play' || value == 'Work') {
64        console.info('    ' + value);
65      }
66      if (name == 'title' || name == 'lens') {
67        console.info('  ' + name);
68      }
69      return true; // The value true means to continue parsing, and false means to stop parsing.
70    }
71    ```
72
734. Set parsing options and call the **parse()** function.
74
75    ```ts
76    let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func};
77    that.parse(options);
78    ```
79
80	The output is as follows:
81
82	```
83	note
84	  title
85	    Play
86	  title
87	  lens
88	    Work
89	  lens
90	note
91	```
92
93
94
95
96## Parsing XML Attributes and Values
97
981. Import the module.
99
100    ```ts
101    import { xml, util } from '@kit.ArkTS'; // Use the API provided by the util module to encode the file.
102    ```
103
1042. Create an XmlPullParser object.
105
106    ```ts
107    let strXml: string =
108      '<?xml version="1.0" encoding="utf-8"?>' +
109        '<note importance="high" logged="true">' +
110        '    <title>Play</title>' +
111        '    <title>Happy</title>' +
112        '    <lens>Work</lens>' +
113        '</note>';
114    let textEncoder: util.TextEncoder = new util.TextEncoder();
115    let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters.
116    let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer as object as ArrayBuffer, 'UTF-8');
117    ```
118
1193. Customize a callback function. In this example, the callback function directly prints the attributes and their values.
120
121    ```ts
122    let str: string = '';
123    function func(name: string, value: string): boolean {
124      str += name + ' ' + value + ' ';
125      return true; // The value true means to continue parsing, and false means to stop parsing.
126    }
127    ```
128
1294. Set parsing options and call the **parse()** function.
130
131    ```ts
132    let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, attributeValueCallbackFunction:func};
133    that.parse(options);
134    console.info(str); // Print all attributes and their values at a time.
135    ```
136
137   The output is as follows:
138   ```
139   importance high logged true // Attributes and values of the note node
140   ```
141
142
143## Parsing XML Event Types and Element Depths
144
1451. Import the module.
146
147    ```ts
148    import { xml, util } from '@kit.ArkTS'; // Use the API provided by the util module to encode the file.
149    ```
150
1512. Create an XmlPullParser object.
152
153    ```ts
154    let strXml: string =
155      '<?xml version="1.0" encoding="utf-8"?>' +
156      '<note importance="high" logged="true">' +
157      '<title>Play</title>' +
158      '</note>';
159    let textEncoder: util.TextEncoder = new util.TextEncoder();
160    let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters.
161    let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer as object as ArrayBuffer, 'UTF-8');
162    ```
163
1643. Customize a callback function. In this example, the callback function directly prints the event types and element depths.
165
166    ```ts
167    let str: string  = '';
168    function func(name: xml.EventType, value: xml.ParseInfo): boolean {
169      str = name +' ' + value.getDepth(); // getDepth is called to obtain the element depth.
170      console.info(str)
171      return true; // The value true means to continue parsing, and false means to stop parsing.
172    }
173    ```
174
1754. Set parsing options and call the **parse()** function.
176
177     ```ts
178     let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func};
179     that.parse(options);
180     ```
181
182   The output is as follows:
183
184	```
185	 0 0 // 0: <?xml version="1.0" encoding="utf-8"?>. The event type value of START_DOCUMENT is 0. 0: The depth is 0.
186	 2 1 // 2: <note importance="high" logged="true">. The event type value of START_TAG is 2. 1: The depth is 1.
187	 2 2 // 2: <title>. The event type value of START_TAG is 2. 2: The depth is 2.
188	 4 2 // 4: Play. The event type value of TEXT is 4. 2: The depth is 2.
189	 3 2 // 3: </title>. The event type value of END_TAG is 3. 2: The depth is 2.
190	 3 1 // 3: </note>. The event type value of END_TAG is 3. 1: The depth is 1 (corresponding to <note>).
191	 1 0 // 1: The event type value of END_DOCUMENT is 1. 0: The depth is 0.
192	```
193
194
195
196
197## Example Scenario
198
199This example demonstrates how to use all parsing options to parse XML tags, attributes, and event types.
200
201
202```ts
203import { xml, util } from '@kit.ArkTS';
204
205let strXml: string =
206  '<?xml version="1.0" encoding="UTF-8"?>' +
207    '<book category="COOKING">' +
208    '<title lang="en">Everyday</title>' +
209    '<author>Giana</author>' +
210    '</book>';
211let textEncoder: util.TextEncoder = new util.TextEncoder();
212let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml);
213let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer as object as ArrayBuffer, 'UTF-8');
214let str: string = '';
215
216function tagFunc(name: string, value: string): boolean {
217  str = name + value;
218  console.info('tag-' + str);
219  return true;
220}
221
222function attFunc(name: string, value: string): boolean {
223  str = name + ' ' + value;
224  console.info('attri-' + str);
225  return true;
226}
227
228function tokenFunc(name: xml.EventType, value: xml.ParseInfo): boolean {
229  str = name + ' ' + value.getDepth();
230  console.info('token-' + str);
231  return true;
232}
233
234let options: xml.ParseOptions = {
235  supportDoctype: true,
236  ignoreNameSpace: true,
237  tagValueCallbackFunction: tagFunc,
238  attributeValueCallbackFunction: attFunc,
239  tokenValueCallbackFunction: tokenFunc
240};
241that.parse(options);
242```
243
244The output is as follows:
245
246   ```
247   tag-
248   token-0 0
249   tag-book
250   attri-category COOKING
251   token-2 1
252   tag-title
253   attri-lang en
254   token-2 2
255   tag-Everyday
256   token-4 2
257   tag-title
258   token-3 2
259   tag-author
260   token-2 2
261   tag-Giana
262   token-4 2
263   tag-author
264   token-3 2
265   tag-book
266   token-3 1
267   tag-
268   token-1 0
269   ```
270