js-apis-xml.md 29.1 KB
Newer Older
1
# @ohos.xml (xml解析与生成)
Z
zengyawen 已提交
2

zyjhandsome's avatar
zyjhandsome 已提交
3
> **说明:**
Z
zengyawen 已提交
4 5 6 7 8 9
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。


## 导入模块

```
Z
zengyawen 已提交
10
import xml from '@ohos.xml';
Z
zengyawen 已提交
11 12 13 14 15 16 17 18 19 20 21
```

## XmlSerializer


### constructor

constructor(buffer: ArrayBuffer | DataView, encoding?: string)

XmlSerializer的构造函数。

Z
zengyawen 已提交
22 23
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
24 25
**参数:**

26 27 28
| 参数名   | 类型                              | 必填 | 说明                                             |
| -------- | --------------------------------- | ---- | ------------------------------------------------ |
| buffer   | ArrayBuffer \| DataView | 是   | 用于接收写入xml信息的ArrayBuffer或DataView内存。 |
29
| encoding | string                            | 否   | 编码格式 , 默认'utf-8'(目前仅支持'utf-8')。               |
Z
zengyawen 已提交
30

Z
zengyawen 已提交
31 32
**示例:**

33
```js
34
let arrayBuffer = new ArrayBuffer(2048);
35
let thatSer = new xml.XmlSerializer(arrayBuffer, "utf-8");
36 37 38 39 40 41 42 43
thatSer.setDeclaration();
let result = '<?xml version="1.0" encoding="utf-8"?>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) //<?xml version="1.0" encoding="utf-8"?>
Z
zengyawen 已提交
44
```
Z
zengyawen 已提交
45 46 47 48


### setAttributes

Z
zengyawen 已提交
49
setAttributes(name: string, value: string): void
Z
zengyawen 已提交
50

J
jiangkai43 已提交
51
写入元素的属性和属性值。
Z
zengyawen 已提交
52

Z
zengyawen 已提交
53 54
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
55 56
**参数:**

57 58
| 参数名 | 类型   | 必填 | 说明            |
| ------ | ------ | ---- | --------------- |
J
jiangkai43 已提交
59 60
| name   | string | 是   | 属性。   |
| value  | string | 是   | 属性值。 |
Z
zengyawen 已提交
61

Z
zengyawen 已提交
62 63
**示例:**

64
```js
65 66 67
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
小马奔腾 已提交
68
thatSer.startElement("note");
69 70 71 72 73 74 75 76 77
thatSer.setAttributes("importance1", "high1");
thatSer.endElement();
let result = '<note importance1="high1"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) //<note importance1="high1"/>
Z
zengyawen 已提交
78
```
Z
zengyawen 已提交
79 80 81 82 83 84 85 86


### addEmptyElement

addEmptyElement(name: string): void

写入一个空元素。

Z
zengyawen 已提交
87 88
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
89 90
**参数:**

91 92 93
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| name   | string | 是   | 该空元素的元素名。 |
Z
zengyawen 已提交
94

Z
zengyawen 已提交
95 96
**示例:**

97
```js
98 99 100 101 102 103 104 105 106 107 108
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.addEmptyElement("d");
let result = '<d/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) //<d/>
Z
zengyawen 已提交
109
```
Z
zengyawen 已提交
110 111 112 113 114 115


### setDeclaration

setDeclaration(): void

J
jiangkai43 已提交
116
写入XML文件声明。
Z
zengyawen 已提交
117

Z
zengyawen 已提交
118 119
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
120 121
**示例:**

122
```js
123 124 125 126 127 128 129 130 131 132 133 134 135 136
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setDeclaration();
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("note");
thatSer.endElement();
let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) //<?xml version="1.0" encoding="utf-8"?>
Z
zengyawen 已提交
137
```
Z
zengyawen 已提交
138 139 140 141 142 143


### startElement

startElement(name: string): void

Z
zengyawen 已提交
144
根据给定名称写入元素开始标记。
Z
zengyawen 已提交
145

Z
zengyawen 已提交
146 147
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
148
**参数:**
Z
zengyawen 已提交
149

150 151 152
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| name   | string | 是   | 当前元素的元素名。 |
Z
zengyawen 已提交
153 154 155

**示例:**

156
```js
157 158
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
159
let thatSer = new xml.XmlSerializer(arrayBuffer);
160 161 162 163 164 165 166 167 168 169 170
thatSer.setDeclaration();
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("note");
thatSer.endElement();
let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(JSON.stringify(view1)) //<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>
Z
zengyawen 已提交
171
```
Z
zengyawen 已提交
172 173 174 175 176

### endElement

endElement(): void

Z
zengyawen 已提交
177
写入元素结束标记。
Z
zengyawen 已提交
178

Z
zengyawen 已提交
179 180
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
181 182
**示例:**

183
```js
184 185 186 187 188 189 190 191 192 193 194 195 196 197
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setDeclaration();
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("note");
thatSer.endElement();
let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(JSON.stringify(view1)) //<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>
Z
zengyawen 已提交
198
```
Z
zengyawen 已提交
199 200 201 202 203 204 205 206


### setNamespace

setNamespace(prefix: string, namespace: string): void

写入当前元素标记的命名空间。

Z
zengyawen 已提交
207 208
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
209 210
**参数:**

211 212 213 214
| 参数名    | 类型   | 必填 | 说明                           |
| --------- | ------ | ---- | ------------------------------ |
| prefix    | string | 是   | 当前元素及其子元素的前缀。     |
| namespace | string | 是   | 当前元素及其子元素的命名空间。 |
Z
zengyawen 已提交
215 216 217

**示例:**

218
```js
219 220
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
221
let thatSer = new xml.XmlSerializer(arrayBuffer);
Z
zengyawen 已提交
222
thatSer.setDeclaration();
223
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
Z
zengyawen 已提交
224
thatSer.startElement("note");
225 226 227 228 229 230 231 232
thatSer.endElement();
let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(JSON.stringify(view1)) //<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>
Z
zengyawen 已提交
233
```
Z
zengyawen 已提交
234

Z
zengyawen 已提交
235
### setComment
Z
zengyawen 已提交
236

Z
zengyawen 已提交
237
setComment(text: string): void
Z
zengyawen 已提交
238

J
jiangkai43 已提交
239
写入注释内容。
Z
zengyawen 已提交
240

Z
zengyawen 已提交
241 242
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
243
**参数:**
Z
zengyawen 已提交
244

245 246 247
| 参数名 | 类型   | 必填 | 说明                 |
| ------ | ------ | ---- | -------------------- |
| text   | string | 是   | 当前元素的注释内容。 |
Z
zengyawen 已提交
248 249 250

**示例:**

251
```js
252 253
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
254
let thatSer = new xml.XmlSerializer(arrayBuffer);
255 256 257 258 259 260 261 262
thatSer.setComment("Hello, World!");
let result = '<!--Hello, World!-->';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) //<!--Hello, World!-->'
Z
zengyawen 已提交
263
```
Z
zengyawen 已提交
264 265


Z
zengyawen 已提交
266
### setCDATA
Z
zengyawen 已提交
267

Z
zengyawen 已提交
268
setCDATA(text: string): void
Z
zengyawen 已提交
269

J
jiangkai43 已提交
270
写入CDATA数据。
Z
zengyawen 已提交
271

Z
zengyawen 已提交
272 273
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
274 275
**参数:**

276 277 278
| 参数名 | 类型   | 必填 | 说明              |
| ------ | ------ | ---- | ----------------- |
| text   | string | 是   | CDATA属性的内容。 |
Z
zengyawen 已提交
279

Z
zengyawen 已提交
280 281
**示例:**

282
```js
283 284
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
285
let thatSer = new xml.XmlSerializer(arrayBuffer);
286 287 288 289 290 291 292 293
thatSer.setCDATA('root SYSTEM')
let result = '<![CDATA[root SYSTEM]]>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) //'<![CDATA[root SYSTEM]]>''
Z
zengyawen 已提交
294
```
Z
zengyawen 已提交
295 296 297 298 299 300


### setText

setText(text: string): void

J
jiangkai43 已提交
301
写入标签值。
Z
zengyawen 已提交
302

Z
zengyawen 已提交
303 304
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
305 306
**参数:**

307 308 309
| 参数名 | 类型   | 必填 | 说明             |
| ------ | ------ | ---- | ---------------- |
| text   | string | 是   | text属性的内容。 |
Z
zengyawen 已提交
310

Z
zengyawen 已提交
311 312
**示例:**

313
```js
314 315
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
316
let thatSer = new xml.XmlSerializer(arrayBuffer);
Z
zengyawen 已提交
317 318 319
thatSer.startElement("note");
thatSer.setAttributes("importance", "high");
thatSer.setText("Happy1");
320 321 322 323 324 325 326 327
thatSer.endElement();
let result = '<note importance="high">Happy1</note>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) // '<note importance="high">Happy1</note>'
Z
zengyawen 已提交
328
```
Z
zengyawen 已提交
329 330 331 332 333 334


### setDocType

setDocType(text: string): void

J
jiangkai43 已提交
335
写入文档类型。
Z
zengyawen 已提交
336

Z
zengyawen 已提交
337 338
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
339 340
**参数:**

341 342 343
| 参数名 | 类型   | 必填 | 说明                |
| ------ | ------ | ---- | ------------------- |
| text   | string | 是   | DocType属性的内容。 |
Z
zengyawen 已提交
344

Z
zengyawen 已提交
345 346
**示例:**

347
```js
348 349
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
350
let thatSer = new xml.XmlSerializer(arrayBuffer);
351 352 353 354 355 356 357 358
thatSer.setDocType('root SYSTEM "http://www.test.org/test.dtd"');
let result = '<!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {
    view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) //'<!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">'
Z
zengyawen 已提交
359
```
Z
zengyawen 已提交
360 361 362 363 364


## XmlPullParser


365
### constructor
Z
zengyawen 已提交
366 367 368

constructor(buffer: ArrayBuffer | DataView, encoding?: string)

369
构造并返回一个XmlPullParser对象。
Z
zengyawen 已提交
370

Z
zengyawen 已提交
371 372
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
373 374
**参数:**

375 376
| 参数名   | 类型                              | 必填 | 说明                                       |
| -------- | --------------------------------- | ---- | ------------------------------------------ |
377 378
| buffer   | ArrayBuffer \| DataView | 是   | 需要解析的xml文本信息。 |
| encoding | string                            | 否   | 编码格式 , 默认'utf-8'(目前仅支持'utf-8')。         |
Z
zengyawen 已提交
379 380 381

**示例:**

382
```js
383
import util from '@ohos.util';
384
let strXml =
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<!DOCTYPE note [\n<!ENTITY foo "baa">]>' +
    '<note importance="high" logged="true">' +
    '    <![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>' +
    '    <!--Hello, World!-->' +
    '    <company>John &amp; Hans</company>' +
    '    <title>Happy</title>' +
    '    <title>Happy</title>' +
    '    <lens>Work</lens>' +
    '    <lens>Play</lens>' +
    '    <?go there?>' +
    '    <a><b/></a>' +
    '    <h:table xmlns:h="http://www.w3.org/TR/html4/">' +
    '        <h:tr>' +
    '            <h:td>Apples</h:td>' +
    '            <h:td>Bananas</h:td>' +
    '        </h:tr>' +
    '    </h:table>' +
    '</note>';
404 405 406
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer, 'UTF-8');
407 408 409 410 411 412 413 414
let str1 = '';
function func1(name, value){
    str1 += name+':'+value;
    return true;
}
let options = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func1}
that.parse(options);
console.log(str1) //'note:company:title:title:lens:lens:a:b:h:table:h:tr:h:td:h:td:'
Z
zengyawen 已提交
415
```
Z
zengyawen 已提交
416 417 418 419 420 421 422 423


### parse

parse(option: ParseOptions): void

该接口用于解析xml。

Z
zengyawen 已提交
424 425
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
426 427
**参数:**

428 429 430
| 参数名 | 类型                          | 必填 | 说明                             |
| ------ | ----------------------------- | ---- | -------------------------------- |
| option | [ParseOptions](#parseoptions) | 是   | 用户控制以及获取解析信息的选项。 |
Z
zengyawen 已提交
431 432 433

**示例:**

434
```js
435
import util from '@ohos.util';
436
let strXml =
Z
zengyawen 已提交
437 438 439 440 441 442
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
443 444 445
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
446 447 448
let arrTag = {};
let str = "";
let i = 0;
Z
zengyawen 已提交
449 450
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
451
    str += arrTag[i];
Z
zengyawen 已提交
452
    i++;
J
Jiefeng Li 已提交
453
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
Z
zengyawen 已提交
454
}
455
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
Z
zengyawen 已提交
456
that.parse(options);
457 458 459 460 461
console.log(str);
// 输出:
// key:0 value:0key:2 value:1key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:3 value:1key:1 value:0
// 解析:
// key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为:
462
// 0(START_DOCUMENT):0(起始深度为0), 2(START_TAG):1(解析到开始标签node, 对应深度为1), 10(WHITESPACE):1(解析到空白标签空格, 对应深度为1), 2(START_TAG):2(解析到开始标签title, 对应深度为2), ...
Z
zengyawen 已提交
463
```
Z
zengyawen 已提交
464 465 466 467 468 469


## ParseOptions

xml解析选项。

Z
zengyawen 已提交
470 471
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang

Z
zengyawen 已提交
472

473 474
| 名称                           | 类型                                                         | 必填 | 说明                                    |
| ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- |
J
jiangkai43 已提交
475 476 477 478 479
| supportDoctype                 | boolean                                                      | 否   | 是否忽略文档类型,默认false,表示解析文档类型。 |
| ignoreNameSpace                | boolean                                                      | 否   | 是否忽略命名空间,默认false,表示解析命名空间。 |
| tagValueCallbackFunction       | (name: string, value: string) =&gt; boolean | 否   | 获取tagValue回调函数,解析标签和标签值,默认null,表示不解析标签和标签值。  |
| attributeValueCallbackFunction | (name: string, value: string) =&gt; boolean | 否   | 获取attributeValue回调函数,解析属性和属性值,默认null,表示不解析属性和属性值。|
| tokenValueCallbackFunction     | (eventType: [EventType](#eventtype), value: [ParseInfo](#parseinfo)) =&gt; boolean | 否   | 获取tokenValue回调函数,,解析元素事件类型([EventType](#eventtype))和[ParseInfo](#parseinfo)属性,默认null,表示不解析元素事件类型和ParseInfo属性。|
Z
zengyawen 已提交
480 481 482 483 484 485 486 487 488 489

## ParseInfo

当前xml解析信息。


### getColumnNumber

getColumnNumber(): number

Z
zengyawen 已提交
490
获取当前列号,从1开始。
Z
zengyawen 已提交
491

Z
zengyawen 已提交
492 493
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
494 495
**返回值:**

496 497
| 类型   | 说明           |
| ------ | -------------- |
Z
zengyawen 已提交
498
| number | 返回当前列号。 |
Z
zengyawen 已提交
499

500 501 502
**示例:**

```js
503
import util from '@ohos.util';
504 505 506 507 508 509 510
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
511 512 513
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
514 515 516 517 518 519 520
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getColumnNumber();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
521
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
522 523 524 525 526 527 528
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:1key:2 value:77key:10 value:81key:2 value:88key:4 value:93key:3 value:101key:10 value:105key:2 value:111key:4 value:115key:3 value:122key:10 value:126key:2 value:132key:4 value:136key:3 value:143key:3 value:150key:1 value:299
```
Z
zengyawen 已提交
529 530 531 532 533 534 535

### getDepth

getDepth(): number

获取元素的当前深度。

Z
zengyawen 已提交
536 537
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
538 539
**返回值:**

540 541
| 类型   | 说明                 |
| ------ | -------------------- |
Z
zengyawen 已提交
542
| number | 返回元素的当前深度。 |
Z
zengyawen 已提交
543

544 545 546
**示例:**

```js
547
import util from '@ohos.util';
548 549 550 551 552 553 554
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
555 556 557
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
558 559 560 561 562 563 564
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
565
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
566 567 568 569 570 571 572 573 574 575
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:0key:2 value:1key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:3 value:1key:1 value:0
// 解析:
// key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为:
// 0(START_DOCUMENT):0(起始深度为0), 2(START_TAG):1(解析到开始标签node, 对应深度为1), 10(WHITESPACE):1(解析到空白标签空格, 对应深度为1), 2(START_TAG):2(解析到开始标签title, 对应深度为2), ...
```
Z
zengyawen 已提交
576 577 578 579 580

### getLineNumber

getLineNumber(): number

Z
zengyawen 已提交
581
获取当前行号,从1开始。
Z
zengyawen 已提交
582

Z
zengyawen 已提交
583 584
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
585 586
**返回值:**

587 588
| 类型   | 说明           |
| ------ | -------------- |
Z
zengyawen 已提交
589
| number | 返回当前行号。 |
Z
zengyawen 已提交
590

591 592 593
**示例:**

```js
594
import util from '@ohos.util';
595 596 597 598 599 600 601
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
602 603 604
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
605 606 607 608 609 610 611
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getLineNumber();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
612
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
613 614 615 616 617 618 619
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:1key:2 value:1key:10 value:1key:2 value:1key:4 value:1key:3 value:1key:10 value:1key:2 value:1key:4 value:1key:3 value:1key:10 value:1key:2 value:1key:4 value:1key:3 value:1key:3 value:1key:1 value:1
```
Z
zengyawen 已提交
620 621 622 623 624 625 626

### getName

getName(): string

获取当前元素名称。

Z
zengyawen 已提交
627 628
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
629 630
**返回值:**

631 632
| 类型   | 说明               |
| ------ | ------------------ |
Z
zengyawen 已提交
633
| string | 返回当前元素名称。 |
Z
zengyawen 已提交
634

635
**示例:**
Z
zengyawen 已提交
636

637
```js
638
import util from '@ohos.util';
639 640 641 642 643 644 645
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
646 647 648
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
649 650 651 652 653 654 655
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getName();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
656
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
657 658 659 660 661 662 663
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:key:2 value:notekey:10 value:key:2 value:titlekey:4 value:key:3 value:titlekey:10 value:key:2 value:todokey:4 value:key:3 value:todokey:10 value:key:2 value:todokey:4 value:key:3 value:todokey:3 value:notekey:1 value:
```
Z
zengyawen 已提交
664 665 666 667 668 669
### getNamespace

getNamespace(): string

获取当前元素的命名空间。

Z
zengyawen 已提交
670 671
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
672 673
**返回值:**

674 675
| 类型   | 说明                     |
| ------ | ------------------------ |
Z
zengyawen 已提交
676
| string | 返回当前元素的命名空间。 |
Z
zengyawen 已提交
677

678
**示例:**
Z
zengyawen 已提交
679

680
```js
681
import util from '@ohos.util';
682 683 684 685 686 687 688
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
689 690 691
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
692 693 694 695 696 697 698
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getNamespace();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
699
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
700 701 702 703 704 705 706
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:key:2 value:key:10 value:key:2 value:key:4 value:key:3 value:key:10 value:key:2 value:key:4 value:key:3 value:key:10 value:key:2 value:key:4 value:key:3 value:key:3 value:key:1 value:
```
Z
zengyawen 已提交
707 708 709 710 711 712
### getPrefix

getPrefix(): string

获取当前元素前缀。

Z
zengyawen 已提交
713 714
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
715 716
**返回值:**

717 718
| 类型   | 说明               |
| ------ | ------------------ |
Z
zengyawen 已提交
719
| string | 返回当前元素前缀。 |
Z
zengyawen 已提交
720

721 722 723
**示例:**

```js
724
import util from '@ohos.util';
725 726 727 728 729 730 731
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
732 733 734
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
735 736 737 738 739 740 741
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getPrefix();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
742
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
743 744 745 746 747 748 749
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:key:2 value:key:10 value:key:2 value:key:4 value:key:3 value:key:10 value:key:2 value:key:4 value:key:3 value:key:10 value:key:2 value:key:4 value:key:3 value:key:3 value:key:1 value:
```
Z
zengyawen 已提交
750 751 752 753 754 755 756

### getText

getText(): string

获取当前事件的文本内容。

Z
zengyawen 已提交
757 758
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
759 760
**返回值:**

761 762
| 类型   | 说明                     |
| ------ | ------------------------ |
Z
zengyawen 已提交
763
| string | 返回当前事件的文本内容。 |
Z
zengyawen 已提交
764

765
**示例:**
Z
zengyawen 已提交
766

767
```js
768
import util from '@ohos.util';
769 770 771 772 773 774 775
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
776 777 778
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
779 780 781 782 783 784 785
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getText();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
786
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
787 788 789 790 791 792 793
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:key:2 value:key:10 value:    key:2 value:key:4 value:Happykey:3 value:key:10 value:    key:2 value:key:4 value:Workkey:3 value:key:10 value:    key:2 value:key:4 value:Playkey:3 value:key:3 value:key:1 value:
```
Z
zengyawen 已提交
794 795 796 797 798 799
### isEmptyElementTag

isEmptyElementTag(): boolean

判断当前元素是否为空元素。

Z
zengyawen 已提交
800 801
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
802 803
**返回值:**

804 805
| 类型    | 说明                         |
| ------- | ---------------------------- |
Z
zengyawen 已提交
806
| boolean | 返回true,当前元素为空元素。 |
Z
zengyawen 已提交
807

808
**示例:**
Z
zengyawen 已提交
809

810
```js
811
import util from '@ohos.util';
812 813 814 815 816 817 818
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
819 820 821
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
822 823 824 825 826 827 828
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.isEmptyElementTag();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
829
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
830 831 832 833 834 835 836
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:falsekey:2 value:falsekey:10 value:falsekey:2 value:falsekey:4 value:falsekey:3 value:falsekey:10 value:falsekey:2 value:falsekey:4 value:falsekey:3 value:falsekey:10 value:falsekey:2 value:falsekey:4 value:falsekey:3 value:falsekey:3 value:falsekey:1 value:false
```
Z
zengyawen 已提交
837 838 839 840 841 842
### isWhitespace

isWhitespace(): boolean

判断当前文本事件是否仅包含空格字符。

Z
zengyawen 已提交
843 844
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
845 846
**返回值:**

847 848
| 类型    | 说明                                   |
| ------- | -------------------------------------- |
Z
zengyawen 已提交
849
| boolean | 返回true,当前文本事件仅包含空格字符。 |
Z
zengyawen 已提交
850

851
**示例:**
Z
zengyawen 已提交
852

853
```js
854
import util from '@ohos.util';
855 856 857 858 859 860 861
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
862 863 864
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
865 866 867 868 869 870 871
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.isWhitespace();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
872
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
873 874 875 876 877 878 879
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:truekey:2 value:falsekey:10 value:truekey:2 value:truekey:4 value:falsekey:3 value:truekey:10 value:truekey:2 value:truekey:4 value:falsekey:3 value:truekey:10 value:truekey:2 value:truekey:4 value:falsekey:3 value:truekey:3 value:truekey:1 value:true
```
Z
zengyawen 已提交
880 881 882 883 884 885
### getAttributeCount

getAttributeCount(): number

获取当前开始标记的属性数。

Z
zengyawen 已提交
886 887
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
888
**返回值:**
889 890
| 类型   | 说明                   |
| ------ | ---------------------- |
Z
zengyawen 已提交
891
| number | 当前开始标记的属性数。 |
Z
zengyawen 已提交
892

893 894 895
**示例:**

```js
896
import util from '@ohos.util';
897 898 899 900 901 902 903
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
904 905 906
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
907 908 909 910 911 912 913
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getAttributeCount();
    str += arrTag[i];
    i++;
J
Jiefeng Li 已提交
914
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
915 916 917 918 919 920 921
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:0key:2 value:2key:10 value:0key:2 value:0key:4 value:0key:3 value:0key:10 value:0key:2 value:0key:4 value:0key:3 value:0key:10 value:0key:2 value:0key:4 value:0key:3 value:0key:3 value:0key:1 value:0
```
Z
zengyawen 已提交
922 923 924

## EventType

J
jiangkai43 已提交
925
事件类型枚举。
Z
zengyawen 已提交
926

Z
zengyawen 已提交
927 928
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang

929 930 931 932 933 934 935 936 937 938 939 940 941
| 名称             | 值   | 说明                  |
| ---------------- | ---- | --------------------- |
| START_DOCUMENT   | 0    | 启动文件事件。        |
| END_DOCUMENT     | 1    | 结束文件事件。        |
| START_TAG        | 2    | 启动标签事件。        |
| END_TAG          | 3    | 结束标签事件。        |
| TEXT             | 4    | 文本事件。            |
| CDSECT           | 5    | CDATA事件。           |
| COMMENT          | 6    | XML注释事件。         |
| DOCDECL          | 7    | XML文档类型声明事件。 |
| INSTRUCTION      | 8    | XML处理指令声明事件。 |
| ENTITY_REFERENCE | 9    | 实体引用事件。        |
| WHITESPACE       | 10   | 空白事件。            |