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
W
wusongqing 已提交
35 36 37 38
var arrayBuffer = new ArrayBuffer(1024);
var bufView = new DataView(arrayBuffer);
var thatSer = new xml.XmlSerializer(bufView);
```
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
var arrayBuffer = new ArrayBuffer(1024);
var bufView = new DataView(arrayBuffer);
W
wusongqing 已提交
61 62 63
var thatSer = new xml.XmlSerializer(bufView);
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
var arrayBuffer = new ArrayBuffer(1024);
var bufView = new DataView(arrayBuffer);
85
var 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
var arrayBuffer = new ArrayBuffer(1024);
var bufView = new DataView(arrayBuffer);
W
wusongqing 已提交
103 104 105
var thatSer = new xml.XmlSerializer(bufView);
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
W
wusongqing 已提交
125 126 127 128 129 130 131 132 133 134
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
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
var arrayBuffer = new ArrayBuffer(1024);
var bufView = new DataView(arrayBuffer);
W
wusongqing 已提交
145 146 147 148 149
var thatSer = new xml.XmlSerializer(bufView);
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
W
wusongqing 已提交
172 173 174 175 176 177 178 179 180 181 182
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
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
W
wusongqing 已提交
197 198 199 200 201 202 203 204 205 206 207
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
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
W
wusongqing 已提交
222 223 224 225 226 227 228 229 230 231 232 233
var arrayBuffer = new ArrayBuffer(1028);
var thatSer = new xml.XmlSerializer(arrayBuffer);
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
W
wusongqing 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
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
W
wusongqing 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
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
W
wusongqing 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
var strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
var arrayBuffer = new ArrayBuffer(strXml.length*2);
var bufView = new Uint8Array(arrayBuffer);
var strLen = strXml.length;
for (var i = 0; i < strLen; ++i) {
    bufView[i] = strXml.charCodeAt(i);// Set the ArrayBuffer mode.
}
var that = new xml.XmlPullParser(arrayBuffer);
```


### 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
W
wusongqing 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
var strXml =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<note importance="high" logged="true">' +
            '    <title>Happy</title>' +
            '    <todo>Work</todo>' +
            '    <todo>Play</todo>' +
            '</note>';
var arrayBuffer = new ArrayBuffer(strXml.length*2);
var bufView = new Uint8Array(arrayBuffer);
var strLen = strXml.length;
for (var i = 0; i < strLen; ++i) {
    bufView[i] = strXml.charCodeAt(i);
}
var that = new xml.XmlPullParser(arrayBuffer);
var arrTag = {};
347 348
var str = "";
var 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 356
}
var options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
357 358 359 360 361 362
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
// Notes:
// The key represents the current event type, and the value represents the depth of the current parsing. You can know the parsed event according to EVENTTYPE. For example, the result 'key: value' in this example means:
// 0(START_DOCUMENT):0(Parse to the START_DOCUMENT, and the depth is 0), 2(START_TAG):1(Parse to the START_TAG node, and the depth is 1), 10(WHITESPACE):1(Parse to the WHITESPACE space, and the depth is 1), 2(START_TAG):2(Parse to the START_TAG title, 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 391

Obtains the column line number, which starts from 1.

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.|