• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# XML Parsing
2
3
4When using XML as a data carrier, it is necessary to parse relevant elements 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 information](#parsing-xml-event-types-and-element-information). 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 text. The input is an ArrayBuffer or a 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 undefined, indicating that XML tags and their 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 undefined, indicating that XML attributes and their 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 undefined, indicating that the XML event type is not parsed.|
18
19
20## Precautions
21
22- The input XML data must be in the standard format.
23
24- Parsing a given node is not supported yet.
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 text.
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.parseXml(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 text.
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.parseXml(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 Information
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 text.
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.parseXml(options);
180     ```
181
182   The output is as follows:
183
184	```
185	 0 0 // First digit 0: <?xml version="1.0" encoding="utf-8"?> corresponds to event type START_DOCUMENT, which value is 0; second digit 0: The depth is 0.
186	 2 1 // First digit 2: <note importance="high" logged="true"> corresponds to event type START_TAG, which value is 2; second digit 1: The depth is 1.
187	 2 2 // First digit 2: <title> corresponds to event type START_TAG, which value is 2; second digit 2: The depth is 2.
188	 4 2 // First digit 4: "Play" corresponds to event type TEXT, which value is 4; second digit 2: The depth is 2.
189	 3 2 // First digit 3: </title> corresponds to event type END_TAG, which value is 3; second digit 2: The depth is 2.
190	 3 1 // First digit 3: </note> corresponds to event type END_TAG, which value is 3; second digit 1: The depth is 1.
191	 1 0 // First digit 1: corresponds to event type END_DOCUMENT, which value is 1; second digit 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.parseXml(options);
242```
243
244The output is as follows:
245
246```
247tag-
248token-0 0
249tag-book
250token-2 1
251attri-category COOKING
252tag-title
253token-2 2
254attri-lang en
255tag-Everyday
256token-4 2
257tag-title
258token-3 2
259tag-author
260token-2 2
261tag-Giana
262token-4 2
263tag-author
264token-3 2
265tag-book
266token-3 1
267tag-
268token-1 0
269```
270