• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# XML Parsing
2
3
4Data transferred in XML format must be parsed in actual use. Generally, three types of elements need to be parsed, as described in [Parsing XML Tags and Tag Values](#parsing-xml-tags-and-tag-values), [Parsing XML Attributes and Attribute Values](#parsing-xml-attributes-and-attribute-values), and [Parsing XML Event Types and Element Depths](#parsing-xml-event-types-and-element-depths).
5
6
7The **xml** module provides the **XmlPullParser** class to parse XML files. The input is an object of the ArrayBuffer or DataView type containing XML text, and the output is the parsed information.
8
9
10**Table 1** XML parsing options
11
12| Name| Type| Mandatory| Description|
13| -------- | -------- | -------- | -------- |
14| supportDoctype | boolean | No| Whether to ignore the document type. The default value is **false**, indicating that the document type is parsed.|
15| ignoreNameSpace | boolean | No| Whether to ignore the namespace. The default value is **false**, indicating that the namespace is parsed.|
16| 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 tag values are not parsed.|
17| 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 attribute values are not parsed.|
18| 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.|
19
20
21## Precautions
22
23- To ensure successful XML parsing and conversion, the input XML data must comply with the standard format.
24
25- Currently, parsing a given node is not supported.
26
27
28## Parsing XML Tags and Tag Values
29
301. Import the modules.
31
32   ```js
33   import xml from '@ohos.xml';
34   import util from '@ohos.util'; // Use the API provided by the util module to encode the file.
35   ```
36
372. Create an **XmlPullParser** object.
38
39   The **XmlPullParser** object can be created based on an object of the ArrayBuffer or DataView type.
40
41
42   ```js
43   let strXml =
44     '<?xml version="1.0" encoding="utf-8"?>' +
45       '<note importance="high" logged="true">' +
46       '<title>Play</title>' +
47       '<lens>Work</lens>' +
48       '</note>';
49   let textEncoder = new util.TextEncoder();
50   let arrBuffer = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters.
51   // 1. Create an XmlPullParser object based on an object of the ArrayBuffer type.
52   let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
53
54   // 2. Create an XmlPullParser object based on an object of the DataView type.
55   let dataView = new DataView(arrBuffer.buffer);
56   let that = new xml.XmlPullParser(dataView, 'UTF-8');
57   ```
58
593. Customize a callback function. In this example, the tag and tag value are directly printed.
60
61   ```js
62   let str = '';
63   function func(name, value){
64     str = name + value;
65     console.info(str);
66     return true; // The value true means to continue parsing, and false means to stop parsing.
67   }
68   ```
69
704. Set parsing options and call the **parse()** function.
71
72   ```js
73   let options = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func};
74   that.parse(options);
75   ```
76
77   The output is as follows:
78
79
80   ```js
81   note
82   title
83   Play
84   title
85   lens
86   Work
87   lens
88   note
89   ```
90
91
92## Parsing XML Attributes and Attribute Values
93
941. Import the modules.
95
96   ```js
97   import xml from '@ohos.xml';
98   import util from '@ohos.util'; // Use the API provided by the util module to encode the file.
99   ```
100
1012. Create an **XmlPullParser** object.
102
103   ```js
104   let strXml =
105     '<?xml version="1.0" encoding="utf-8"?>' +
106       '<note importance="high" logged="true">' +
107       '    <title>Play</title>' +
108       '    <title>Happy</title>' +
109       '    <lens>Work</lens>' +
110       '</note>';
111   let textEncoder = new util.TextEncoder();
112   let arrBuffer = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters.
113   let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
114   ```
115
1163. Customize a callback function. In this example, the attribute and attribute value are directly printed.
117
118   ```js
119   let str = '';
120   function func(name, value){
121     str += name + ' ' + value + ' ';
122     return true; // The value true means to continue parsing, and false means to stop parsing.
123   }
124   ```
125
1264. Set parsing options and call the **parse()** function.
127
128   ```js
129   let options = {supportDoctype:true, ignoreNameSpace:true, attributeValueCallbackFunction:func};
130   that.parse(options);
131   console.info(str); // Print all attributes and their values at a time.
132   ```
133
134   The output is as follows:
135
136
137   ```js
138   importance high logged true // Attributes and attribute values of the note node
139   ```
140
141
142## Parsing XML Event Types and Element Depths
143
1441. Import the modules.
145
146   ```js
147   import xml from '@ohos.xml';
148   import util from '@ohos.util'; // Use the API provided by the util module to encode the file.
149   ```
150
1512. Create an **XmlPullParser** object.
152
153   ```js
154   let strXml =
155     '<?xml version="1.0" encoding="utf-8"?>' +
156     '<note importance="high" logged="true">' +
157     '<title>Play</title>' +
158     '</note>';
159   let textEncoder = new util.TextEncoder();
160   let arrBuffer = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters.
161   let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
162   ```
163
1643. Customize a callback function. In this example, the event type and element depth are directly printed.
165
166   ```js
167   let str = '';
168   function func(name, value){
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   ```js
178   let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func};
179   that.parse(options);
180   ```
181
182   The output is as follows:
183
184
185   ```js
186   0 0 // 0: <?xml version="1.0" encoding="utf-8"?>. The event type value of START_DOCUMENT is 0. 0: The depth is 0.
187   2 1 // 2: <note importance="high" logged="true">. The event type value of START_TAG is 2. 1: The depth is 1.
188   2 2 // 2: <title>. The event type value of START_TAG is 2. 2: The depth is 2.
189   4 2 // 4: Play. The event type value of TEXT is 4. 2: The depth is 2.
190   3 2 // 3: </title>. The event type value of END_TAG is 3. 2: The depth is 2.
191   3 1 // 3: </note>. The event type value of END_TAG is 3. 1: The depth is 1 (corresponding to <note>).
192   1 0 // 1: The event type value of END_DOCUMENT is 1. 0: The depth is 0.
193   ```
194
195
196## Example Scenario
197
198The following uses invoking all parsing options as an example to describe how to parse XML tags, attributes, and event types.
199
200
201```js
202import xml from '@ohos.xml';
203import util from '@ohos.util';
204
205let strXml =
206  '<?xml version="1.0" encoding="UTF-8"?>' +
207    '<book category="COOKING">' +
208    '<title lang="en">Everyday</title>' +
209    '<author>Giada</author>' +
210    '</book>';
211let textEncoder = new util.TextEncoder();
212let arrBuffer = textEncoder.encodeInto(strXml);
213let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
214let str = '';
215
216function tagFunc(name, value) {
217  str = name + value;
218  console.info('tag-' + str);
219  return true;
220}
221
222function attFunc(name, value) {
223  str = name + ' ' + value;
224  console.info('attri-' + str);
225  return true;
226}
227
228function tokenFunc(name, value) {
229  str = name + ' ' + value.getDepth();
230  console.info('token-' + str);
231  return true;
232}
233
234let options = {
235  supportDocType: true,
236  ignoreNameSpace: true,
237  tagValueCallbackFunction: tagFunc,
238  attributeValueCallbackFunction: attFunc,
239  tokenValueCallbackFunction: tokenFunc
240};
241that.parse(options);
242
243```
244
245The output is as follows:
246
247
248```js
249tag-
250token-0 0
251tag-book
252attri-category COOKING
253token-2 1
254tag-title
255attri-lang en
256token-2 2
257tag-Everyday
258token-4 2
259tag-title
260token-3 2
261tag-author
262token-2 2
263tag-Giada
264token-4 2
265tag-author
266token-3 2
267tag-book
268token-3 1
269tag-
270token-1 0
271```
272