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);
W
wusongqing 已提交
62 63
thatSer.setAttributes("importance", "high");  
```
Z
zengyawen 已提交
64 65


W
wusongqing 已提交
66 67 68 69 70
### addEmptyElement

addEmptyElement(name: string): void

Adds an empty element.
Z
zengyawen 已提交
71

W
wusongqing 已提交
72 73
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
74
**Parameters**
Z
zengyawen 已提交
75

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

W
wusongqing 已提交
80
**Example**
Z
zengyawen 已提交
81

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


W
wusongqing 已提交
90
### setDeclaration
Z
zengyawen 已提交
91

W
wusongqing 已提交
92
setDeclaration(): void
Z
zengyawen 已提交
93 94 95

Sets a declaration.

W
wusongqing 已提交
96 97
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
98
**Example**
Z
zengyawen 已提交
99

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


W
wusongqing 已提交
108
### startElement
Z
zengyawen 已提交
109

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

Writes the start tag based on the given element name.

W
wusongqing 已提交
114 115
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
116 117 118 119 120 121 122 123
**Parameters**

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

**Example**

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


### endElement

endElement(): void
Z
zengyawen 已提交
135 136 137

Writes the end tag of the element.

W
wusongqing 已提交
138 139
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
140
**Example**
Z
zengyawen 已提交
141

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


W
wusongqing 已提交
154
### setNamespace
Z
zengyawen 已提交
155

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

Sets the namespace for an element tag.

W
wusongqing 已提交
160 161
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
162 163 164 165 166 167 168 169 170
**Parameters**

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

**Example**

171
```js
172 173
let arrayBuffer = new ArrayBuffer(1024);
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
174 175 176 177 178 179 180 181 182
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 已提交
183 184 185

Sets the comment.

W
wusongqing 已提交
186 187
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
188 189 190 191 192 193 194 195
**Parameters**

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

**Example**

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


### setCDATA

setCDATA(text: string): void
Z
zengyawen 已提交
208 209 210

Sets CDATA attributes.

W
wusongqing 已提交
211 212
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
213 214 215 216 217 218 219 220
**Parameters**

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

**Example**

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


### setText

setText(text: string): void

Sets **Text**.

W
wusongqing 已提交
234 235
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
236 237 238 239 240 241 242 243
**Parameters**

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

**Example**

244
```js
245 246
let arrayBuffer = new ArrayBuffer(1024);
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259
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 已提交
260 261
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
262 263 264 265 266 267 268 269
**Parameters**

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

**Example**

270
```js
271 272
let arrayBuffer = new ArrayBuffer(1024);
let thatSer = new xml.XmlSerializer(arrayBuffer);
W
wusongqing 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285
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 已提交
286 287
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
288 289 290 291 292 293 294 295 296
**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**

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


### parse

parse(option: ParseOptions): void
Z
zengyawen 已提交
318 319 320

Parses XML information.

W
wusongqing 已提交
321 322
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
323 324 325 326 327 328 329 330
**Parameters**

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

**Example**

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


## ParseOptions
Z
zengyawen 已提交
367 368 369

Defines the XML parsing options.

W
wusongqing 已提交
370 371
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
372

W
wusongqing 已提交
373 374 375 376 377 378 379 380 381
| 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 已提交
382

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


W
wusongqing 已提交
386 387 388
### getColumnNumber

getColumnNumber(): number
Z
zengyawen 已提交
389

390
Obtains the column line number, starting from 1.
Z
zengyawen 已提交
391

W
wusongqing 已提交
392 393
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
394
**Return value**
Z
zengyawen 已提交
395

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


W
wusongqing 已提交
401
### getDepth
Z
zengyawen 已提交
402

W
wusongqing 已提交
403
getDepth(): number
Z
zengyawen 已提交
404 405 406

Obtains the depth of this element.

W
wusongqing 已提交
407 408
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
409
**Return value**
Z
zengyawen 已提交
410

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


W
wusongqing 已提交
416
### getLineNumber
Z
zengyawen 已提交
417

W
wusongqing 已提交
418
getLineNumber(): number
Z
zengyawen 已提交
419 420 421

Obtains the current line number, starting from 1.

W
wusongqing 已提交
422 423
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
424
**Return value**
Z
zengyawen 已提交
425

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


W
wusongqing 已提交
431
### getName
Z
zengyawen 已提交
432

W
wusongqing 已提交
433
getName(): string
Z
zengyawen 已提交
434 435 436

Obtains the name of this element.

W
wusongqing 已提交
437 438
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
439
**Return value**
Z
zengyawen 已提交
440

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


W
wusongqing 已提交
446
### getNamespace
Z
zengyawen 已提交
447

W
wusongqing 已提交
448
getNamespace(): string
Z
zengyawen 已提交
449 450 451

Obtains the namespace of this element.

W
wusongqing 已提交
452 453
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
454
**Return value**
Z
zengyawen 已提交
455

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


W
wusongqing 已提交
461
### getPrefix
Z
zengyawen 已提交
462

W
wusongqing 已提交
463
getPrefix(): string
Z
zengyawen 已提交
464 465 466

Obtains the prefix of this element.

W
wusongqing 已提交
467 468
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
469
**Return value**
Z
zengyawen 已提交
470

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


W
wusongqing 已提交
476
### getText
Z
zengyawen 已提交
477

W
wusongqing 已提交
478
getText(): string
Z
zengyawen 已提交
479 480 481

Obtains the text of the current event.

W
wusongqing 已提交
482 483
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
484
**Return value**
Z
zengyawen 已提交
485

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


W
wusongqing 已提交
491
### isEmptyElementTag
Z
zengyawen 已提交
492

W
wusongqing 已提交
493
isEmptyElementTag(): boolean
Z
zengyawen 已提交
494 495 496

Checks whether the current element is empty.

W
wusongqing 已提交
497 498
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
499
**Return value**
Z
zengyawen 已提交
500

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


W
wusongqing 已提交
506
### isWhitespace
Z
zengyawen 已提交
507

W
wusongqing 已提交
508
isWhitespace(): boolean
Z
zengyawen 已提交
509 510 511

Checks whether the current text event contains only whitespace characters.

W
wusongqing 已提交
512 513
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
514
**Return value**
Z
zengyawen 已提交
515

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


W
wusongqing 已提交
521
### getAttributeCount
Z
zengyawen 已提交
522

W
wusongqing 已提交
523
getAttributeCount(): number
Z
zengyawen 已提交
524 525 526

Obtains the number of attributes for the current start tag.

W
wusongqing 已提交
527 528
**System capability**: SystemCapability.Utils.Lang

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


W
wusongqing 已提交
535
## EventType
Z
zengyawen 已提交
536 537 538

Enumerates the events.

W
wusongqing 已提交
539 540
**System capability**: SystemCapability.Utils.Lang

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