js-apis-xml.md 30.3 KB
Newer Older
G
Gloria 已提交
1
# @ohos.xml (XML Parsing and Generation)
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

W
wusongqing 已提交
7 8

## Modules to Import
Z
zengyawen 已提交
9 10 11 12 13

```
import xml from '@ohos.xml';
```

W
wusongqing 已提交
14
## XmlSerializer
Z
zengyawen 已提交
15 16


W
wusongqing 已提交
17
### constructor
Z
zengyawen 已提交
18

W
wusongqing 已提交
19
constructor(buffer: ArrayBuffer | DataView, encoding?: string)
Z
zengyawen 已提交
20

W
wusongqing 已提交
21
A constructor used to create an **XmlSerializer** instance.
Z
zengyawen 已提交
22

W
wusongqing 已提交
23 24
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
25
**Parameters**
Z
zengyawen 已提交
26

G
Gloria 已提交
27 28 29 30
| Name  | Type                             | Mandatory| Description                                            |
| -------- | --------------------------------- | ---- | ------------------------------------------------ |
| buffer   | ArrayBuffer \| DataView | Yes  | **ArrayBuffer** or **DataView** for storing the XML information to write.|
| encoding | string                            | No  | Encoding format.                                      |
Z
zengyawen 已提交
31

W
wusongqing 已提交
32
**Example**
Z
zengyawen 已提交
33

34
```js
35 36 37 38 39 40 41 42 43 44
let arrayBuffer = new ArrayBuffer(2048);
let thatSer = new xml.XmlSerializer(arrayBuffer,"utf-8");
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"?>
W
wusongqing 已提交
45
```
Z
zengyawen 已提交
46 47


W
wusongqing 已提交
48
### setAttributes
Z
zengyawen 已提交
49

W
wusongqing 已提交
50
setAttributes(name: string, value: string): void
Z
zengyawen 已提交
51 52 53

Sets an attribute.

W
wusongqing 已提交
54 55
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
56 57
**Parameters**

G
Gloria 已提交
58 59 60 61
| Name| Type  | Mandatory| Description           |
| ------ | ------ | ---- | --------------- |
| name   | string | Yes  | Key of the attribute.  |
| value  | string | Yes  | Value of the attribute.|
W
wusongqing 已提交
62 63 64

**Example**

65
```js
66 67 68
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
小马奔腾 已提交
69
thatSer.startElement("note");
70 71 72 73 74 75 76 77 78
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"/>
W
wusongqing 已提交
79
```
Z
zengyawen 已提交
80 81


W
wusongqing 已提交
82 83 84 85 86
### addEmptyElement

addEmptyElement(name: string): void

Adds an empty element.
Z
zengyawen 已提交
87

W
wusongqing 已提交
88 89
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
90
**Parameters**
Z
zengyawen 已提交
91

G
Gloria 已提交
92 93 94
| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
| name   | string | Yes  | Name of the empty element to add.|
Z
zengyawen 已提交
95

W
wusongqing 已提交
96
**Example**
Z
zengyawen 已提交
97

98
```js
99 100 101 102 103 104 105 106 107 108 109
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/>
W
wusongqing 已提交
110
```
Z
zengyawen 已提交
111 112


W
wusongqing 已提交
113
### setDeclaration
Z
zengyawen 已提交
114

W
wusongqing 已提交
115
setDeclaration(): void
Z
zengyawen 已提交
116 117 118

Sets a declaration.

W
wusongqing 已提交
119 120
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
121
**Example**
Z
zengyawen 已提交
122

123
```js
124 125 126 127 128 129 130 131 132 133 134 135 136 137
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"?>
W
wusongqing 已提交
138
```
Z
zengyawen 已提交
139 140


W
wusongqing 已提交
141
### startElement
Z
zengyawen 已提交
142

W
wusongqing 已提交
143
startElement(name: string): void
Z
zengyawen 已提交
144 145 146

Writes the start tag based on the given element name.

W
wusongqing 已提交
147 148
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
149 150
**Parameters**

G
Gloria 已提交
151 152 153
| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
| name   | string | Yes  | Name of the element.|
W
wusongqing 已提交
154 155 156

**Example**

157
```js
158 159
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
160
let thatSer = new xml.XmlSerializer(arrayBuffer);
161 162 163 164 165 166 167 168 169 170 171
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/"/>
W
wusongqing 已提交
172 173 174 175 176
```

### endElement

endElement(): void
Z
zengyawen 已提交
177 178 179

Writes the end tag of the element.

W
wusongqing 已提交
180 181
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
182
**Example**
Z
zengyawen 已提交
183

184
```js
185 186 187 188 189 190 191 192 193 194 195 196 197 198
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/"/>
W
wusongqing 已提交
199
```
Z
zengyawen 已提交
200 201


W
wusongqing 已提交
202
### setNamespace
Z
zengyawen 已提交
203

W
wusongqing 已提交
204
setNamespace(prefix: string, namespace: string): void
Z
zengyawen 已提交
205 206 207

Sets the namespace for an element tag.

W
wusongqing 已提交
208 209
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
210 211
**Parameters**

G
Gloria 已提交
212 213 214 215
| Name   | Type  | Mandatory| Description                          |
| --------- | ------ | ---- | ------------------------------ |
| prefix    | string | Yes  | Prefix of the element and its child elements.    |
| namespace | string | Yes  | Namespace to set.|
W
wusongqing 已提交
216 217 218

**Example**

219
```js
220 221
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
222
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
223
thatSer.setDeclaration();
224
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
W
wusongqing 已提交
225
thatSer.startElement("note");
226 227 228 229 230 231 232 233
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/"/>
W
wusongqing 已提交
234 235 236 237 238
```

### setComment

setComment(text: string): void
Z
zengyawen 已提交
239 240 241

Sets the comment.

W
wusongqing 已提交
242 243
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
244 245
**Parameters**

G
Gloria 已提交
246 247 248
| Name| Type  | Mandatory| Description                |
| ------ | ------ | ---- | -------------------- |
| text   | string | Yes  | Comment to set.|
W
wusongqing 已提交
249 250 251

**Example**

252
```js
253 254
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
255
let thatSer = new xml.XmlSerializer(arrayBuffer);
256 257 258 259 260 261 262 263
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!-->'
W
wusongqing 已提交
264 265 266 267 268 269
```


### setCDATA

setCDATA(text: string): void
Z
zengyawen 已提交
270 271 272

Sets CDATA attributes.

W
wusongqing 已提交
273 274
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
275 276
**Parameters**

G
Gloria 已提交
277 278 279
| Name| Type  | Mandatory| Description             |
| ------ | ------ | ---- | ----------------- |
| text   | string | Yes  | CDATA attribute to set.|
W
wusongqing 已提交
280 281 282

**Example**

283
```js
284 285
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
286
let thatSer = new xml.XmlSerializer(arrayBuffer);
287 288 289 290 291 292 293 294
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]]>''
W
wusongqing 已提交
295 296 297 298 299 300 301 302 303
```


### setText

setText(text: string): void

Sets **Text**.

W
wusongqing 已提交
304 305
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
306 307
**Parameters**

G
Gloria 已提交
308 309 310
| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
| text   | string | Yes  | Content of the **Text** to set.|
W
wusongqing 已提交
311 312 313

**Example**

314
```js
315 316
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
317
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
318 319 320
thatSer.startElement("note");
thatSer.setAttributes("importance", "high");
thatSer.setText("Happy1");
321 322 323 324 325 326 327 328
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>'
W
wusongqing 已提交
329 330 331 332 333 334 335 336 337
```


### setDocType

setDocType(text: string): void

Sets **DocType**.

W
wusongqing 已提交
338 339
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
340 341
**Parameters**

G
Gloria 已提交
342 343 344
| Name| Type  | Mandatory| Description               |
| ------ | ------ | ---- | ------------------- |
| text   | string | Yes  | Content of **DocType** to set.|
W
wusongqing 已提交
345 346 347

**Example**

348
```js
349 350
const myMAX = 2048;
let arrayBuffer = new ArrayBuffer(myMAX);
351
let thatSer = new xml.XmlSerializer(arrayBuffer);
352 353 354 355 356 357 358 359
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">'
W
wusongqing 已提交
360 361 362 363 364 365 366 367 368 369 370 371
```


## XmlPullParser


### XmlPullParser

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

Creates and returns an **XmlPullParser** object. The **XmlPullParser** object passes two parameters. The first parameter is the memory of the **ArrayBuffer** or **DataView** type, and the second parameter is the file format (UTF-8 by default).

W
wusongqing 已提交
372 373
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
374 375
**Parameters**

G
Gloria 已提交
376 377 378 379
| Name  | Type                             | Mandatory| Description                                      |
| -------- | --------------------------------- | ---- | ------------------------------------------ |
| buffer   | ArrayBuffer \| DataView | Yes  | **ArrayBuffer** or **DataView** that contains XML text information.|
| encoding | string                            | No  | Encoding format. Only UTF-8 is supported.                 |
W
wusongqing 已提交
380 381 382

**Example**

383
```js
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 arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
407 408
for (let i = 0; i < strLen; ++i) {
    bufView[i] = strXml.charCodeAt(i);
W
wusongqing 已提交
409
}
410 411 412 413 414 415 416 417 418
let that = new xml.XmlPullParser(arrayBuffer, 'UTF-8');
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:'
W
wusongqing 已提交
419 420 421 422 423 424
```


### parse

parse(option: ParseOptions): void
Z
zengyawen 已提交
425 426 427

Parses XML information.

W
wusongqing 已提交
428 429
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
430 431
**Parameters**

G
Gloria 已提交
432 433 434
| Name| Type                         | Mandatory| Description                            |
| ------ | ----------------------------- | ---- | -------------------------------- |
| option | [ParseOptions](#parseoptions) | Yes  | Options for controlling and obtaining the parsed information.|
W
wusongqing 已提交
435 436 437

**Example**

438
```js
439
let strXml =
W
wusongqing 已提交
440 441 442 443 444 445
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
446 447 448
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
449
for (let tmp = 0; tmp < strLen; ++tmp) {
450
    bufView[tmp] = strXml.charCodeAt(tmp);
W
wusongqing 已提交
451
}
452 453 454 455
let that = new xml.XmlPullParser(arrayBuffer);
let arrTag = {};
let str = "";
let i = 0;
W
wusongqing 已提交
456 457
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
458
    str += arrTag[i];
W
wusongqing 已提交
459
    i++;
J
Jiefeng Li 已提交
460
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
W
wusongqing 已提交
461
}
462
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
W
wusongqing 已提交
463
that.parse(options);
464 465 466
console.log(str);
// Output:
// 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
G
Gloria 已提交
467 468 469
// Note:
// key indicates the event type, and value indicates the parsing depth. You can learn the specific parsed event based on EVENTTYPE. In this example, key: value means:
// 0(START_DOCUMENT):0 (START_DOCUMENT is being parsed, and the depth is 0), 2(START_TAG):1 (START_TAG is being parsed, and the depth is 1), 10(WHITESPACE):1 (WHITESPACE is being parsed, and the depth is 1), 2(START_TAG):2 (START_TAG is being parsed, and the depth is 2), ...
W
wusongqing 已提交
470 471 472 473
```


## ParseOptions
Z
zengyawen 已提交
474 475 476

Defines the XML parsing options.

W
wusongqing 已提交
477 478
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
479

G
Gloria 已提交
480 481 482 483 484 485 486
| Name                          | Type                                                        | Mandatory| Description                                   |
| ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- |
| supportDoctype                 | boolean                                                      | No  | Whether to ignore **Doctype**. The default value is **false**.|
| ignoreNameSpace                | boolean                                                      | No  | Whether to ignore **Namespace**. The default value is **false**.         |
| tagValueCallbackFunction       | (name: string, value: string) =&gt; boolean | No  | Callback used to return **tagValue**.                 |
| attributeValueCallbackFunction | (name: string, value: string) =&gt; boolean | No  | Callback used to return **attributeValue**.           |
| tokenValueCallbackFunction     | (eventType: [EventType](#eventtype), value: [ParseInfo](#parseinfo)) =&gt; boolean | No  | Callback used to return **tokenValue**.               |
W
wusongqing 已提交
487 488

## ParseInfo
Z
zengyawen 已提交
489

W
wusongqing 已提交
490
Provides APIs to manage the parsed XML information.
Z
zengyawen 已提交
491 492


W
wusongqing 已提交
493 494 495
### getColumnNumber

getColumnNumber(): number
Z
zengyawen 已提交
496

497
Obtains the column line number, starting from 1.
Z
zengyawen 已提交
498

W
wusongqing 已提交
499 500
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
501
**Return value**
Z
zengyawen 已提交
502

G
Gloria 已提交
503 504
| Type  | Description          |
| ------ | -------------- |
W
wusongqing 已提交
505
| number | Column number obtained.|
Z
zengyawen 已提交
506

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
**Example**

```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
531
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
532 533 534 535 536 537 538
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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 已提交
539

W
wusongqing 已提交
540
### getDepth
Z
zengyawen 已提交
541

W
wusongqing 已提交
542
getDepth(): number
Z
zengyawen 已提交
543 544 545

Obtains the depth of this element.

W
wusongqing 已提交
546 547
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
548
**Return value**
Z
zengyawen 已提交
549

G
Gloria 已提交
550 551
| Type  | Description                |
| ------ | -------------------- |
W
wusongqing 已提交
552
| number | Depth obtained.|
Z
zengyawen 已提交
553

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
**Example**

```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
578
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
579 580 581 582 583 584 585 586 587 588
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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
// Note:
// key indicates the event type, and value indicates the parsing depth. You can learn the specific parsed event based on EVENTTYPE. In this example, key: value means:
// 0(START_DOCUMENT):0 (START_DOCUMENT is being parsed, and the depth is 0), 2(START_TAG):1 (START_TAG is being parsed, and the depth is 1), 10(WHITESPACE):1 (WHITESPACE is being parsed, and the depth is 1), 2(START_TAG):2 (START_TAG is being parsed, and the depth is 2), ...
```
Z
zengyawen 已提交
589

W
wusongqing 已提交
590
### getLineNumber
Z
zengyawen 已提交
591

W
wusongqing 已提交
592
getLineNumber(): number
Z
zengyawen 已提交
593 594 595

Obtains the current line number, starting from 1.

W
wusongqing 已提交
596 597
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
598
**Return value**
Z
zengyawen 已提交
599

G
Gloria 已提交
600 601
| Type  | Description          |
| ------ | -------------- |
W
wusongqing 已提交
602
| number | Line number obtained.|
Z
zengyawen 已提交
603

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
**Example**

```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
628
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
629 630 631 632 633 634 635
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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 已提交
636

W
wusongqing 已提交
637
### getName
Z
zengyawen 已提交
638

W
wusongqing 已提交
639
getName(): string
Z
zengyawen 已提交
640 641 642

Obtains the name of this element.

W
wusongqing 已提交
643 644
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
645
**Return value**
Z
zengyawen 已提交
646

G
Gloria 已提交
647 648
| Type  | Description              |
| ------ | ------------------ |
W
wusongqing 已提交
649
| string | Element name obtained.|
Z
zengyawen 已提交
650

651
**Example**
Z
zengyawen 已提交
652

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
675
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
676 677 678 679 680 681 682
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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:
```
W
wusongqing 已提交
683
### getNamespace
Z
zengyawen 已提交
684

W
wusongqing 已提交
685
getNamespace(): string
Z
zengyawen 已提交
686 687 688

Obtains the namespace of this element.

W
wusongqing 已提交
689 690
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
691
**Return value**
Z
zengyawen 已提交
692

G
Gloria 已提交
693 694
| Type  | Description                    |
| ------ | ------------------------ |
W
wusongqing 已提交
695
| string | Namespace obtained.|
Z
zengyawen 已提交
696

697
**Example**
Z
zengyawen 已提交
698

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
721
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
722 723 724 725 726 727 728
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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:
```
W
wusongqing 已提交
729
### getPrefix
Z
zengyawen 已提交
730

W
wusongqing 已提交
731
getPrefix(): string
Z
zengyawen 已提交
732 733 734

Obtains the prefix of this element.

W
wusongqing 已提交
735 736
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
737
**Return value**
Z
zengyawen 已提交
738

G
Gloria 已提交
739 740
| Type  | Description              |
| ------ | ------------------ |
W
wusongqing 已提交
741
| string | Element prefix obtained.|
Z
zengyawen 已提交
742

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
**Example**

```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
767
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
768 769 770 771 772 773 774
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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 已提交
775

W
wusongqing 已提交
776
### getText
Z
zengyawen 已提交
777

W
wusongqing 已提交
778
getText(): string
Z
zengyawen 已提交
779 780 781

Obtains the text of the current event.

W
wusongqing 已提交
782 783
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
784
**Return value**
Z
zengyawen 已提交
785

G
Gloria 已提交
786 787
| Type  | Description                    |
| ------ | ------------------------ |
W
wusongqing 已提交
788
| string | Text content obtained.|
Z
zengyawen 已提交
789

790
**Example**
Z
zengyawen 已提交
791

792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
814
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
815 816 817 818 819 820 821
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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:
```
W
wusongqing 已提交
822
### isEmptyElementTag
Z
zengyawen 已提交
823

W
wusongqing 已提交
824
isEmptyElementTag(): boolean
Z
zengyawen 已提交
825 826 827

Checks whether the current element is empty.

W
wusongqing 已提交
828 829
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
830
**Return value**
Z
zengyawen 已提交
831

G
Gloria 已提交
832 833
| Type   | Description                        |
| ------- | ---------------------------- |
W
wusongqing 已提交
834
| boolean | Returns **true** if the element is empty; returns **false** otherwise.|
Z
zengyawen 已提交
835

836
**Example**
Z
zengyawen 已提交
837

838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
860
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
861 862 863 864 865 866 867
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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
```
W
wusongqing 已提交
868
### isWhitespace
Z
zengyawen 已提交
869

W
wusongqing 已提交
870
isWhitespace(): boolean
Z
zengyawen 已提交
871 872 873

Checks whether the current text event contains only whitespace characters.

W
wusongqing 已提交
874 875
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
876
**Return value**
Z
zengyawen 已提交
877

G
Gloria 已提交
878 879
| Type   | Description                                  |
| ------- | -------------------------------------- |
W
wusongqing 已提交
880
| boolean | Returns **true** if the text event contains only whitespace characters; returns **false** otherwise.|
Z
zengyawen 已提交
881

882
**Example**
Z
zengyawen 已提交
883

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
906
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
907 908 909 910 911 912 913
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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
```
W
wusongqing 已提交
914
### getAttributeCount
Z
zengyawen 已提交
915

W
wusongqing 已提交
916
getAttributeCount(): number
Z
zengyawen 已提交
917 918 919

Obtains the number of attributes for the current start tag.

W
wusongqing 已提交
920 921
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
922
**Return value**
G
Gloria 已提交
923 924
| Type  | Description                  |
| ------ | ---------------------- |
W
wusongqing 已提交
925
| number | Number of attributes obtained.|
Z
zengyawen 已提交
926

927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
**Example**

```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>';
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
for (let tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
}
let that = new xml.XmlPullParser(arrayBuffer);
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 已提交
951
    return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
952 953 954 955 956 957 958
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// Output:
// 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 已提交
959

W
wusongqing 已提交
960
## EventType
Z
zengyawen 已提交
961 962 963

Enumerates the events.

W
wusongqing 已提交
964 965
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978
| Name            | Value  | Description                 |
| ---------------- | ---- | --------------------- |
| START_DOCUMENT   | 0    | Indicates a start document event.       |
| END_DOCUMENT     | 1    | Indicates an end document event.       |
| START_TAG        | 2    | Indicates a start tag event.       |
| END_TAG          | 3    | Indicates an end tag event.       |
| TEXT             | 4    | Indicates a text event.           |
| CDSECT           | 5    | Indicates a CDATA section event.          |
| COMMENT          | 6    | Indicates an XML comment event.        |
| DOCDECL          | 7    | Indicates an XML document type declaration event.|
| INSTRUCTION      | 8    | Indicates an XML processing instruction event.|
| ENTITY_REFERENCE | 9    | Indicates an entity reference event.       |
| WHITESPACE       | 10   | Indicates a whitespace character event.           |