js-apis-xml.md 28.4 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 51 52

设置Attributes方法。

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

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

57 58 59 60
| 参数名 | 类型   | 必填 | 说明            |
| ------ | ------ | ---- | --------------- |
| name   | string | 是   | 属性的key值。   |
| value  | string | 是   | 属性的value值。 |
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 116 117


### setDeclaration

setDeclaration(): void

设置Declaration方法。

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 239 240

写入comment属性。

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

Z
zengyawen 已提交
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 301 302


### setText

setText(text: string): void

设置Text方法。

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 335 336


### setDocType

setDocType(text: string): void

写入DocType属性。

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
let strXml =
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    '<?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>';
403 404 405
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer, 'UTF-8');
406 407 408 409 410 411 412 413
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 已提交
414
```
Z
zengyawen 已提交
415 416 417 418 419 420 421 422


### parse

parse(option: ParseOptions): void

该接口用于解析xml。

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

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

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

**示例:**

433
```js
434
let strXml =
Z
zengyawen 已提交
435 436 437 438 439 440
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
441 442 443
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
444 445 446
let arrTag = {};
let str = "";
let i = 0;
Z
zengyawen 已提交
447 448
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
449
    str += arrTag[i];
Z
zengyawen 已提交
450
    i++;
J
Jiefeng Li 已提交
451
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
Z
zengyawen 已提交
452
}
453
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
Z
zengyawen 已提交
454
that.parse(options);
455 456 457 458 459
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代表含义为:
460
// 0(START_DOCUMENT):0(起始深度为0), 2(START_TAG):1(解析到开始标签node, 对应深度为1), 10(WHITESPACE):1(解析到空白标签空格, 对应深度为1), 2(START_TAG):2(解析到开始标签title, 对应深度为2), ...
Z
zengyawen 已提交
461
```
Z
zengyawen 已提交
462 463 464 465 466 467


## ParseOptions

xml解析选项。

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

Z
zengyawen 已提交
470

471 472 473 474
| 名称                           | 类型                                                         | 必填 | 说明                                    |
| ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- |
| supportDoctype                 | boolean                                                      | 否   | 是否忽略Doctype , 默认false。 |
| ignoreNameSpace                | boolean                                                      | 否   | 是否忽略NameSpace,默认false。          |
475 476 477
| 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回调函数, 默认null。     |
Z
zengyawen 已提交
478 479 480 481 482 483 484 485 486 487

## ParseInfo

当前xml解析信息。


### getColumnNumber

getColumnNumber(): number

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

Z
zengyawen 已提交
490 491
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
492 493
**返回值:**

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

498 499 500 501 502 503 504 505 506 507
**示例:**

```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
508 509 510
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
511 512 513 514 515 516 517
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 已提交
518
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
519 520 521 522 523 524 525
}
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 已提交
526 527 528 529 530 531 532

### getDepth

getDepth(): number

获取元素的当前深度。

Z
zengyawen 已提交
533 534
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
535 536
**返回值:**

537 538
| 类型   | 说明                 |
| ------ | -------------------- |
Z
zengyawen 已提交
539
| number | 返回元素的当前深度。 |
Z
zengyawen 已提交
540

541 542 543 544 545 546 547 548 549 550
**示例:**

```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
551 552 553
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
554 555 556 557 558 559 560
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 已提交
561
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
562 563 564 565 566 567 568 569 570 571
}
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 已提交
572 573 574 575 576

### getLineNumber

getLineNumber(): number

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

Z
zengyawen 已提交
579 580
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
581 582
**返回值:**

583 584
| 类型   | 说明           |
| ------ | -------------- |
Z
zengyawen 已提交
585
| number | 返回当前行号。 |
Z
zengyawen 已提交
586

587 588 589 590 591 592 593 594 595 596
**示例:**

```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
597 598 599
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
600 601 602 603 604 605 606
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 已提交
607
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
608 609 610 611 612 613 614
}
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 已提交
615 616 617 618 619 620 621

### getName

getName(): string

获取当前元素名称。

Z
zengyawen 已提交
622 623
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
624 625
**返回值:**

626 627
| 类型   | 说明               |
| ------ | ------------------ |
Z
zengyawen 已提交
628
| string | 返回当前元素名称。 |
Z
zengyawen 已提交
629

630
**示例:**
Z
zengyawen 已提交
631

632 633 634 635 636 637 638 639
```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
640 641 642
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
643 644 645 646 647 648 649
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 已提交
650
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
651 652 653 654 655 656 657
}
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 已提交
658 659 660 661 662 663
### getNamespace

getNamespace(): string

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

Z
zengyawen 已提交
664 665
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
666 667
**返回值:**

668 669
| 类型   | 说明                     |
| ------ | ------------------------ |
Z
zengyawen 已提交
670
| string | 返回当前元素的命名空间。 |
Z
zengyawen 已提交
671

672
**示例:**
Z
zengyawen 已提交
673

674 675 676 677 678 679 680 681
```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
682 683 684
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
685 686 687 688 689 690 691
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 已提交
692
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
693 694 695 696 697 698 699
}
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 已提交
700 701 702 703 704 705
### getPrefix

getPrefix(): string

获取当前元素前缀。

Z
zengyawen 已提交
706 707
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
708 709
**返回值:**

710 711
| 类型   | 说明               |
| ------ | ------------------ |
Z
zengyawen 已提交
712
| string | 返回当前元素前缀。 |
Z
zengyawen 已提交
713

714 715 716 717 718 719 720 721 722 723
**示例:**

```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
724 725 726
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
727 728 729 730 731 732 733
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 已提交
734
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
735 736 737 738 739 740 741
}
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 已提交
742 743 744 745 746 747 748

### getText

getText(): string

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

Z
zengyawen 已提交
749 750
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
751 752
**返回值:**

753 754
| 类型   | 说明                     |
| ------ | ------------------------ |
Z
zengyawen 已提交
755
| string | 返回当前事件的文本内容。 |
Z
zengyawen 已提交
756

757
**示例:**
Z
zengyawen 已提交
758

759 760 761 762 763 764 765 766
```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
767 768 769
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
770 771 772 773 774 775 776
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 已提交
777
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
778 779 780 781 782 783 784
}
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 已提交
785 786 787 788 789 790
### isEmptyElementTag

isEmptyElementTag(): boolean

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

Z
zengyawen 已提交
791 792
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
793 794
**返回值:**

795 796
| 类型    | 说明                         |
| ------- | ---------------------------- |
Z
zengyawen 已提交
797
| boolean | 返回true,当前元素为空元素。 |
Z
zengyawen 已提交
798

799
**示例:**
Z
zengyawen 已提交
800

801 802 803 804 805 806 807 808
```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
809 810 811
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
812 813 814 815 816 817 818
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 已提交
819
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
820 821 822 823 824 825 826
}
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 已提交
827 828 829 830 831 832
### isWhitespace

isWhitespace(): boolean

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

Z
zengyawen 已提交
833 834
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
835 836
**返回值:**

837 838
| 类型    | 说明                                   |
| ------- | -------------------------------------- |
Z
zengyawen 已提交
839
| boolean | 返回true,当前文本事件仅包含空格字符。 |
Z
zengyawen 已提交
840

841
**示例:**
Z
zengyawen 已提交
842

843 844 845 846 847 848 849 850
```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
851 852 853
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
854 855 856 857 858 859 860
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 已提交
861
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
862 863 864 865 866 867 868
}
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 已提交
869 870 871 872 873 874
### getAttributeCount

getAttributeCount(): number

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

Z
zengyawen 已提交
875 876
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
877
**返回值:**
878 879
| 类型   | 说明                   |
| ------ | ---------------------- |
Z
zengyawen 已提交
880
| number | 当前开始标记的属性数。 |
Z
zengyawen 已提交
881

882 883 884 885 886 887 888 889 890 891
**示例:**

```js
let strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
892 893 894
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
895 896 897 898 899 900 901
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 已提交
902
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
903 904 905 906 907 908 909
}
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 已提交
910 911 912 913 914

## EventType

事件枚举。

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

917 918 919 920 921 922 923 924 925 926 927 928 929
| 名称             | 值   | 说明                  |
| ---------------- | ---- | --------------------- |
| 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   | 空白事件。            |