js-apis-xml.md 13.1 KB
Newer Older
W
wusongqing 已提交
1
# 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

W
wusongqing 已提交
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
let arrayBuffer = new ArrayBuffer(1024);
let bufView = new DataView(arrayBuffer);
let thatSer = new xml.XmlSerializer(bufView);
W
wusongqing 已提交
38
```
Z
zengyawen 已提交
39 40


W
wusongqing 已提交
41
### setAttributes
Z
zengyawen 已提交
42

W
wusongqing 已提交
43
setAttributes(name: string, value: string): void
Z
zengyawen 已提交
44 45 46

Sets an attribute.

W
wusongqing 已提交
47 48
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
49 50 51 52 53 54 55 56 57
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the attribute.|
| value | string | Yes| Value of the attribute.|

**Example**

58
```js
59 60 61
let arrayBuffer = new ArrayBuffer(1024);
let bufView = new DataView(arrayBuffer);
let thatSer = new xml.XmlSerializer(bufView);
小马奔腾 已提交
62 63 64
thatSer.startElement("note");
thatSer.setAttributes("importance", "high");
thatSer.endElement();
W
wusongqing 已提交
65
```
Z
zengyawen 已提交
66 67


W
wusongqing 已提交
68 69 70 71 72
### addEmptyElement

addEmptyElement(name: string): void

Adds an empty element.
Z
zengyawen 已提交
73

W
wusongqing 已提交
74 75
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
76
**Parameters**
Z
zengyawen 已提交
77

W
wusongqing 已提交
78 79 80
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the empty element to add.|
Z
zengyawen 已提交
81

W
wusongqing 已提交
82
**Example**
Z
zengyawen 已提交
83

84
```js
85 86 87
let arrayBuffer = new ArrayBuffer(1024);
let bufView = new DataView(arrayBuffer);
let thatSer = new xml.XmlSerializer(bufView);
W
wusongqing 已提交
88 89
thatSer.addEmptyElement("b"); // => <b/>
```
Z
zengyawen 已提交
90 91


W
wusongqing 已提交
92
### setDeclaration
Z
zengyawen 已提交
93

W
wusongqing 已提交
94
setDeclaration(): void
Z
zengyawen 已提交
95 96 97

Sets a declaration.

W
wusongqing 已提交
98 99
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
100
**Example**
Z
zengyawen 已提交
101

102
```js
103 104 105
let arrayBuffer = new ArrayBuffer(1024);
let bufView = new DataView(arrayBuffer);
let thatSer = new xml.XmlSerializer(bufView);
W
wusongqing 已提交
106 107
thatSer.setDeclaration() // => <?xml version="1.0" encoding="utf-8"?>;
```
Z
zengyawen 已提交
108 109


W
wusongqing 已提交
110
### startElement
Z
zengyawen 已提交
111

W
wusongqing 已提交
112
startElement(name: string): void
Z
zengyawen 已提交
113 114 115

Writes the start tag based on the given element name.

W
wusongqing 已提交
116 117
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
118 119 120 121 122 123 124 125
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the element.|

**Example**

126
```js
127 128
let arrayBuffer = new ArrayBuffer(1024);
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
129 130 131 132 133 134 135 136
thatSer.startElement("notel");
thatSer.endElement();// => '<notel/>';
```


### endElement

endElement(): void
Z
zengyawen 已提交
137 138 139

Writes the end tag of the element.

W
wusongqing 已提交
140 141
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
142
**Example**
Z
zengyawen 已提交
143

144
```js
145 146 147
let arrayBuffer = new ArrayBuffer(1024);
let bufView = new DataView(arrayBuffer);
let thatSer = new xml.XmlSerializer(bufView);
W
wusongqing 已提交
148 149 150 151
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("table");
thatSer.setAttributes("importance", "high");
thatSer.setText("Happy");
S
shikai-123 已提交
152
thatSer.endElement(); // => <h:table importance="high" xmlns:h="http://www.w3.org/TR/html4/">Happy</h:table>
W
wusongqing 已提交
153
```
Z
zengyawen 已提交
154 155


W
wusongqing 已提交
156
### setNamespace
Z
zengyawen 已提交
157

W
wusongqing 已提交
158
setNamespace(prefix: string, namespace: string): void
Z
zengyawen 已提交
159 160 161

Sets the namespace for an element tag.

W
wusongqing 已提交
162 163
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
164 165 166 167 168 169 170 171 172
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| prefix | string | Yes| Prefix of the element and its child elements.|
| namespace | string | Yes| Namespace to set.|

**Example**

173
```js
174 175
let arrayBuffer = new ArrayBuffer(1024);
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
176 177 178 179 180 181 182 183 184
thatSer.setDeclaration();
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("note");
thatSer.endElement();// = >'<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
```

### setComment

setComment(text: string): void
Z
zengyawen 已提交
185 186 187

Sets the comment.

W
wusongqing 已提交
188 189
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
190 191 192 193 194 195 196 197
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Comment to set.|

**Example**

198
```js
199 200
let arrayBuffer = new ArrayBuffer(1024);
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
201 202 203 204 205 206 207 208 209
thatSer.startElement("note");
thatSer.setComment("Hi!");
thatSer.endElement(); // => '<note>\r\n  <!--Hi!-->\r\n</note>';
```


### setCDATA

setCDATA(text: string): void
Z
zengyawen 已提交
210 211 212

Sets CDATA attributes.

W
wusongqing 已提交
213 214
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
215 216 217 218 219 220 221 222
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| CDATA attribute to set.|

**Example**

223
```js
224 225
let arrayBuffer = new ArrayBuffer(1028);
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
226 227 228 229 230 231 232 233 234 235
thatSer.setCDATA('root SYSTEM') // => '<![CDATA[root SYSTEM]]>';
```


### setText

setText(text: string): void

Sets **Text**.

W
wusongqing 已提交
236 237
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
238 239 240 241 242 243 244 245
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Content of the **Text** to set.|

**Example**

246
```js
247 248
let arrayBuffer = new ArrayBuffer(1024);
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261
thatSer.startElement("note");
thatSer.setAttributes("importance", "high");
thatSer.setText("Happy1");
thatSer.endElement(); // => '<note importance="high">Happy1</note>';
```


### setDocType

setDocType(text: string): void

Sets **DocType**.

W
wusongqing 已提交
262 263
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
264 265 266 267 268 269 270 271
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Content of **DocType** to set.|

**Example**

272
```js
273 274
let arrayBuffer = new ArrayBuffer(1024);
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287
thatSer.setDocType('root SYSTEM'); // => '<!DOCTYPE root SYSTEM>';
```


## 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 已提交
288 289
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
290 291 292 293 294 295 296 297 298
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| buffer | ArrayBuffer&nbsp;\|&nbsp;DataView | Yes| **ArrayBuffer** or **DataView** that contains XML text information.|
| encoding | string | No| Encoding format. Only UTF-8 is supported.|

**Example**

299
```js
300
let strXml =
W
wusongqing 已提交
301 302 303 304 305 306
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
307 308 309
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
W
wusongqing 已提交
310 311 312
for (var i = 0; i < strLen; ++i) {
    bufView[i] = strXml.charCodeAt(i);// Set the ArrayBuffer mode.
}
313
let that = new xml.XmlPullParser(arrayBuffer);
W
wusongqing 已提交
314 315 316 317 318 319
```


### parse

parse(option: ParseOptions): void
Z
zengyawen 已提交
320 321 322

Parses XML information.

W
wusongqing 已提交
323 324
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
325 326 327 328 329 330 331 332
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| option | [ParseOptions](#parseoptions) | Yes| Options for controlling and obtaining the parsed information.|

**Example**

333
```js
334
let strXml =
W
wusongqing 已提交
335 336 337 338 339 340
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
341 342 343
let arrayBuffer = new ArrayBuffer(strXml.length);
let bufView = new Uint8Array(arrayBuffer);
let strLen = strXml.length;
344 345
for (var tmp = 0; tmp < strLen; ++tmp) {
    bufView[tmp] = strXml.charCodeAt(tmp);
W
wusongqing 已提交
346
}
347 348 349 350
let that = new xml.XmlPullParser(arrayBuffer);
let arrTag = {};
let str = "";
let i = 0;
W
wusongqing 已提交
351 352
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
353
    str += arrTag[i];
W
wusongqing 已提交
354
    i++;
355
    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
W
wusongqing 已提交
356
}
357
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
W
wusongqing 已提交
358
that.parse(options);
359 360 361
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 已提交
362 363 364
// 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 已提交
365 366 367 368
```


## ParseOptions
Z
zengyawen 已提交
369 370 371

Defines the XML parsing options.

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

W
wusongqing 已提交
374

W
wusongqing 已提交
375 376 377 378 379 380 381 382 383
| 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:&nbsp;string,&nbsp;value:&nbsp;string)=&gt;&nbsp;boolean | No| Callback used to return **tagValue**.|
| attributeValueCallbackFunction | (name:&nbsp;string,&nbsp;value:&nbsp;string)=&gt;&nbsp;boolean | No| Callback used to return **attributeValue**.|
| tokenValueCallbackFunction | (eventType:&nbsp;[EventType](#eventtype),&nbsp;value:&nbsp;[ParseInfo](#parseinfo))=&gt;&nbsp;boolean | No| Callback used to return **tokenValue**.|

## ParseInfo
Z
zengyawen 已提交
384

W
wusongqing 已提交
385
Provides APIs to manage the parsed XML information.
Z
zengyawen 已提交
386 387


W
wusongqing 已提交
388 389 390
### getColumnNumber

getColumnNumber(): number
Z
zengyawen 已提交
391

392
Obtains the column line number, starting from 1.
Z
zengyawen 已提交
393

W
wusongqing 已提交
394 395
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
396
**Return value**
Z
zengyawen 已提交
397

W
wusongqing 已提交
398 399 400
| Type| Description|
| -------- | -------- |
| number | Column number obtained.|
Z
zengyawen 已提交
401 402


W
wusongqing 已提交
403
### getDepth
Z
zengyawen 已提交
404

W
wusongqing 已提交
405
getDepth(): number
Z
zengyawen 已提交
406 407 408

Obtains the depth of this element.

W
wusongqing 已提交
409 410
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
411
**Return value**
Z
zengyawen 已提交
412

W
wusongqing 已提交
413 414 415
| Type| Description|
| -------- | -------- |
| number | Depth obtained.|
Z
zengyawen 已提交
416 417


W
wusongqing 已提交
418
### getLineNumber
Z
zengyawen 已提交
419

W
wusongqing 已提交
420
getLineNumber(): number
Z
zengyawen 已提交
421 422 423

Obtains the current line number, starting from 1.

W
wusongqing 已提交
424 425
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
426
**Return value**
Z
zengyawen 已提交
427

W
wusongqing 已提交
428 429 430
| Type| Description|
| -------- | -------- |
| number | Line number obtained.|
Z
zengyawen 已提交
431 432


W
wusongqing 已提交
433
### getName
Z
zengyawen 已提交
434

W
wusongqing 已提交
435
getName(): string
Z
zengyawen 已提交
436 437 438

Obtains the name of this element.

W
wusongqing 已提交
439 440
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
441
**Return value**
Z
zengyawen 已提交
442

W
wusongqing 已提交
443 444 445
| Type| Description|
| -------- | -------- |
| string | Element name obtained.|
Z
zengyawen 已提交
446 447


W
wusongqing 已提交
448
### getNamespace
Z
zengyawen 已提交
449

W
wusongqing 已提交
450
getNamespace(): string
Z
zengyawen 已提交
451 452 453

Obtains the namespace of this element.

W
wusongqing 已提交
454 455
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
456
**Return value**
Z
zengyawen 已提交
457

W
wusongqing 已提交
458 459 460
| Type| Description|
| -------- | -------- |
| string | Namespace obtained.|
Z
zengyawen 已提交
461 462


W
wusongqing 已提交
463
### getPrefix
Z
zengyawen 已提交
464

W
wusongqing 已提交
465
getPrefix(): string
Z
zengyawen 已提交
466 467 468

Obtains the prefix of this element.

W
wusongqing 已提交
469 470
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
471
**Return value**
Z
zengyawen 已提交
472

W
wusongqing 已提交
473 474 475
| Type| Description|
| -------- | -------- |
| string | Element prefix obtained.|
Z
zengyawen 已提交
476 477


W
wusongqing 已提交
478
### getText
Z
zengyawen 已提交
479

W
wusongqing 已提交
480
getText(): string
Z
zengyawen 已提交
481 482 483

Obtains the text of the current event.

W
wusongqing 已提交
484 485
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
486
**Return value**
Z
zengyawen 已提交
487

W
wusongqing 已提交
488 489 490
| Type| Description|
| -------- | -------- |
| string | Text content obtained.|
Z
zengyawen 已提交
491 492


W
wusongqing 已提交
493
### isEmptyElementTag
Z
zengyawen 已提交
494

W
wusongqing 已提交
495
isEmptyElementTag(): boolean
Z
zengyawen 已提交
496 497 498

Checks whether the current element is empty.

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

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

W
wusongqing 已提交
503 504 505
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the element is empty; returns **false** otherwise.|
Z
zengyawen 已提交
506 507


W
wusongqing 已提交
508
### isWhitespace
Z
zengyawen 已提交
509

W
wusongqing 已提交
510
isWhitespace(): boolean
Z
zengyawen 已提交
511 512 513

Checks whether the current text event contains only whitespace characters.

W
wusongqing 已提交
514 515
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
516
**Return value**
Z
zengyawen 已提交
517

W
wusongqing 已提交
518 519 520
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the text event contains only whitespace characters; returns **false** otherwise.|
Z
zengyawen 已提交
521 522


W
wusongqing 已提交
523
### getAttributeCount
Z
zengyawen 已提交
524

W
wusongqing 已提交
525
getAttributeCount(): number
Z
zengyawen 已提交
526 527 528

Obtains the number of attributes for the current start tag.

W
wusongqing 已提交
529 530
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
531 532 533 534
**Return value**
| Type| Description|
| -------- | -------- |
| number | Number of attributes obtained.|
Z
zengyawen 已提交
535 536


W
wusongqing 已提交
537
## EventType
Z
zengyawen 已提交
538 539 540

Enumerates the events.

W
wusongqing 已提交
541 542
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555
| 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.|