• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# XML解析
2
3
4对于以XML作为载体传递的数据,实际使用中需要对相关的节点进行解析,一般包括[解析XML标签和标签值](#解析xml标签和标签值)、[解析XML属性和属性值](#解析xml属性和属性值)、[解析XML事件类型和元素深度](#解析xml事件类型和元素深度)三类场景。
5
6
7XML模块提供XmlPullParser类对XML文件解析,输入为含有XML文本的ArrayBuffer或DataView,输出为解析得到的信息。
8
9
10  **表1** XML解析选项
11
12| 名称 | 类型 | 必填 | 说明 |
13| -------- | -------- | -------- | -------- |
14| supportDoctype | boolean | 否 | 是否忽略文档类型。默认为false,表示对文档类型进行解析。 |
15| ignoreNameSpace | boolean | 否 | 是否忽略命名空间。默认为false,表示对命名空间进行解析。 |
16| tagValueCallbackFunction | (name: string, value: string) => boolean | 否 | 获取tagValue回调函数,打印标签及标签值。默认为null,表示不进行XML标签和标签值的解析。 |
17| attributeValueCallbackFunction | (name: string, value: string) => boolean | 否 | 获取attributeValue回调函数, 打印属性及属性值。默认为null,表示不进行XML属性和属性值的解析。 |
18| tokenValueCallbackFunction | (eventType: EventType, value: ParseInfo) => boolean | 否 | 获取tokenValue回调函数,打印标签事件类型及parseInfo对应属性。默认为null,表示不进行XML事件类型解析。 |
19
20
21## 注意事项
22
23- XML解析及转换需要确保传入的XML数据符合标准格式。
24
25- XML解析目前不支持按指定节点解析对应的节点值。
26
27
28## 解析XML标签和标签值
29
301. 引入模块。
31
32   ```js
33   import xml from '@ohos.xml';
34   import util from '@ohos.util'; // 需要使用util模块函数对文件编码
35   ```
36
372. 对XML文件编码后调用XmlPullParser。
38   可以基于ArrayBuffer构造XmlPullParser对象, 也可以基于DataView构造XmlPullParser对象。
39
40
41   ```js
42   let strXml =
43     '<?xml version="1.0" encoding="utf-8"?>' +
44       '<note importance="high" logged="true">' +
45       '<title>Play</title>' +
46       '<lens>Work</lens>' +
47       '</note>';
48   let textEncoder = new util.TextEncoder();
49   let arrBuffer = textEncoder.encodeInto(strXml); // 对数据编码,防止包含中文字符乱码
50   // 1.基于ArrayBuffer构造XmlPullParser对象
51   let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
52
53   // 2.基于DataView构造XmlPullParser对象
54   let dataView = new DataView(arrBuffer.buffer);
55   let that = new xml.XmlPullParser(dataView, 'UTF-8');
56   ```
57
583. 自定义回调函数,本例直接打印出标签及标签值。
59
60   ```js
61   let str = '';
62   function func(name, value){
63     str = name + value;
64     console.info(str);
65     return true; //true:继续解析 false:停止解析
66   }
67   ```
68
694. 设置解析选项,调用parse函数。
70
71   ```js
72   let options = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func};
73   that.parse(options);
74   ```
75
76   输出结果如下所示:
77
78
79   ```js
80   note
81   title
82   Play
83   title
84   lens
85   Work
86   lens
87   note
88   ```
89
90
91## 解析XML属性和属性值
92
931. 引入模块。
94
95   ```js
96   import xml from '@ohos.xml';
97   import util from '@ohos.util'; // 需要使用util模块函数对文件编码
98   ```
99
1002. 对XML文件编码后调用XmlPullParser。
101
102   ```js
103   let strXml =
104     '<?xml version="1.0" encoding="utf-8"?>' +
105       '<note importance="high" logged="true">' +
106       '    <title>Play</title>' +
107       '    <title>Happy</title>' +
108       '    <lens>Work</lens>' +
109       '</note>';
110   let textEncoder = new util.TextEncoder();
111   let arrBuffer = textEncoder.encodeInto(strXml); // 对数据编码,防止包含中文字符乱码
112   let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
113   ```
114
1153. 自定义回调函数,本例直接打印出属性及属性值。
116
117   ```js
118   let str = '';
119   function func(name, value){
120     str += name + ' ' + value + ' ';
121     return true; // true:继续解析 false:停止解析
122   }
123   ```
124
1254. 设置解析选项,调用parse函数。
126
127   ```js
128   let options = {supportDoctype:true, ignoreNameSpace:true, attributeValueCallbackFunction:func};
129   that.parse(options);
130   console.info(str); // 一次打印出所有的属性及其值
131   ```
132
133   输出结果如下所示:
134
135
136   ```js
137   importance high logged true // note节点的属性及属性值
138   ```
139
140
141## 解析XML事件类型和元素深度
142
1431. 引入模块。
144
145   ```js
146   import xml from '@ohos.xml';
147   import util from '@ohos.util'; // 需要使用util模块函数对文件编码
148   ```
149
1502. 对XML文件编码后调用XmlPullParser。
151
152   ```js
153   let strXml =
154     '<?xml version="1.0" encoding="utf-8"?>' +
155     '<note importance="high" logged="true">' +
156     '<title>Play</title>' +
157     '</note>';
158   let textEncoder = new util.TextEncoder();
159   let arrBuffer = textEncoder.encodeInto(strXml); // 对数据编码,防止包含中文字符乱码
160   let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
161   ```
162
1633. 自定义回调函数,本例直接打印元素事件类型及元素深度。
164
165   ```js
166   let str = '';
167   function func(name, value){
168     str = name + ' ' + value.getDepth(); // getDepth 获取元素的当前深度
169     console.info(str)
170     return true; //true:继续解析 false:停止解析
171   }
172   ```
173
1744. 设置解析选项,调用parse函数。
175
176   ```js
177   let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func};
178   that.parse(options);
179   ```
180
181   输出结果如下所示:
182
183
184   ```js
185   0 0 // 0:<?xml version="1.0" encoding="utf-8"?> 对应事件类型START_DOCUMENT值为0  0:起始深度为0
186   2 1 // 2:<note importance="high" logged="true"> 对应事件类型START_TAG值为2       1:深度为1
187   2 2 // 2:<title>对应事件类型START_TAG值为2                                       2:深度为2
188   4 2 // 4:Play对应事件类型TEXT值为4                                               2:深度为2
189   3 2 // 3:</title>对应事件类型END_TAG值为3                                        2:深度为2
190   3 1 // 3:</note>对应事件类型END_TAG值为3                                         1:深度为1(与<note对应>)
191   1 0 // 1:对应事件类型END_DOCUMENT值为1                                           0:深度为0
192   ```
193
194
195## 场景示例
196
197此处以调用所有解析选项为例,提供解析XML标签、属性和事件类型的开发示例。
198
199
200```js
201import xml from '@ohos.xml';
202import util from '@ohos.util';
203
204let strXml =
205  '<?xml version="1.0" encoding="UTF-8"?>' +
206    '<book category="COOKING">' +
207    '<title lang="en">Everyday</title>' +
208    '<author>Giada</author>' +
209    '</book>';
210let textEncoder = new util.TextEncoder();
211let arrBuffer = textEncoder.encodeInto(strXml);
212let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
213let str = '';
214
215function tagFunc(name, value) {
216  str = name + value;
217  console.info('tag-' + str);
218  return true;
219}
220
221function attFunc(name, value) {
222  str = name + ' ' + value;
223  console.info('attri-' + str);
224  return true;
225}
226
227function tokenFunc(name, value) {
228  str = name + ' ' + value.getDepth();
229  console.info('token-' + str);
230  return true;
231}
232
233let options = {
234  supportDocType: true,
235  ignoreNameSpace: true,
236  tagValueCallbackFunction: tagFunc,
237  attributeValueCallbackFunction: attFunc,
238  tokenValueCallbackFunction: tokenFunc
239};
240that.parse(options);
241
242```
243
244输出结果如下所示:
245
246
247```js
248tag-
249token-0 0
250tag-book
251attri-category COOKING
252token-2 1
253tag-title
254attri-lang en
255token-2 2
256tag-Everyday
257token-4 2
258tag-title
259token-3 2
260tag-author
261token-2 2
262tag-Giada
263token-4 2
264tag-author
265token-3 2
266tag-book
267token-3 1
268tag-
269token-1 0
270```
271