js-apis-xml.md 10.8 KB
Newer Older
W
wusongqing 已提交
1
# XML Parsing and Generation
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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 已提交
5

W
wusongqing 已提交
6 7

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

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

W
wusongqing 已提交
13
## System Capabilities
Z
zengyawen 已提交
14

W
wusongqing 已提交
15
SystemCapability.Utils.Lang
Z
zengyawen 已提交
16

W
wusongqing 已提交
17
## XmlSerializer
Z
zengyawen 已提交
18 19


W
wusongqing 已提交
20
### constructor
Z
zengyawen 已提交
21

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

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

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

W
wusongqing 已提交
28 29 30 31
| 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 已提交
32

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

W
wusongqing 已提交
35 36 37 38 39
```
var arrayBuffer = new ArrayBuffer(1024);
var bufView = new DataView(arrayBuffer);
var thatSer = new xml.XmlSerializer(bufView);
```
Z
zengyawen 已提交
40 41


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

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

Sets an attribute.

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

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

**Example**

```
var thatSer = new xml.XmlSerializer(bufView);
thatSer.setAttributes("importance", "high");  
```
Z
zengyawen 已提交
61 62


W
wusongqing 已提交
63 64 65 66 67
### addEmptyElement

addEmptyElement(name: string): void

Adds an empty element.
Z
zengyawen 已提交
68

W
wusongqing 已提交
69
**Parameters**
Z
zengyawen 已提交
70

W
wusongqing 已提交
71 72 73
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the empty element to add.|
Z
zengyawen 已提交
74

W
wusongqing 已提交
75
**Example**
Z
zengyawen 已提交
76

W
wusongqing 已提交
77 78 79 80
```
 var thatSer = new xml.XmlSerializer(bufView);
thatSer.addEmptyElement("b"); // => <b/>
```
Z
zengyawen 已提交
81 82


W
wusongqing 已提交
83
### setDeclaration
Z
zengyawen 已提交
84

W
wusongqing 已提交
85
setDeclaration(): void
Z
zengyawen 已提交
86 87 88

Sets a declaration.

W
wusongqing 已提交
89
**Example**
Z
zengyawen 已提交
90

W
wusongqing 已提交
91 92 93 94
```
var thatSer = new xml.XmlSerializer(bufView);
thatSer.setDeclaration() // => <?xml version="1.0" encoding="utf-8"?>;
```
Z
zengyawen 已提交
95 96


W
wusongqing 已提交
97
### startElement
Z
zengyawen 已提交
98

W
wusongqing 已提交
99
startElement(name: string): void
Z
zengyawen 已提交
100 101 102

Writes the start tag based on the given element name.

W
wusongqing 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
**Parameters**

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

**Example**

```
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.startElement("notel");
thatSer.endElement();// => '<notel/>';
```


### endElement

endElement(): void
Z
zengyawen 已提交
122 123 124

Writes the end tag of the element.

W
wusongqing 已提交
125
**Example**
Z
zengyawen 已提交
126

W
wusongqing 已提交
127 128 129 130 131 132 133 134
```
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");
endElement(); // => <h:table importance="high" xmlns:h="http://www.w3.org/TR/html4/">Happy</h:table>
```
Z
zengyawen 已提交
135 136


W
wusongqing 已提交
137
### setNamespace
Z
zengyawen 已提交
138

W
wusongqing 已提交
139
setNamespace(prefix: string, namespace: string): void
Z
zengyawen 已提交
140 141 142

Sets the namespace for an element tag.

W
wusongqing 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
**Parameters**

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

**Example**

```
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 已提交
164 165 166

Sets the comment.

W
wusongqing 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
**Parameters**

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

**Example**

```
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 已提交
187 188 189

Sets CDATA attributes.

W
wusongqing 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
**Parameters**

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

**Example**

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

**Parameters**

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

**Example**

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

**Parameters**

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

**Example**

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

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

```
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 已提交
289 290 291

Parses XML information.

W
wusongqing 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
**Parameters**

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

**Example**

```
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 = {};
arrTag[0] = '132';
var i = 1;
function func(key, value){
    arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
    i++;
    return true;
}
var options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
```


## ParseOptions
Z
zengyawen 已提交
329 330 331

Defines the XML parsing options.

W
wusongqing 已提交
332 333 334 335 336 337 338 339 340 341
| 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 已提交
342

W
wusongqing 已提交
343
Provides APIs to manage the parsed XML information.
Z
zengyawen 已提交
344 345


W
wusongqing 已提交
346 347 348
### getColumnNumber

getColumnNumber(): number
Z
zengyawen 已提交
349 350 351

Obtains the column line number, which starts from 1.

W
wusongqing 已提交
352
**Return value**
Z
zengyawen 已提交
353

W
wusongqing 已提交
354 355 356
| Type| Description|
| -------- | -------- |
| number | Column number obtained.|
Z
zengyawen 已提交
357 358


W
wusongqing 已提交
359
### getDepth
Z
zengyawen 已提交
360

W
wusongqing 已提交
361
getDepth(): number
Z
zengyawen 已提交
362 363 364

Obtains the depth of this element.

W
wusongqing 已提交
365
**Return value**
Z
zengyawen 已提交
366

W
wusongqing 已提交
367 368 369
| Type| Description|
| -------- | -------- |
| number | Depth obtained.|
Z
zengyawen 已提交
370 371


W
wusongqing 已提交
372
### getLineNumber
Z
zengyawen 已提交
373

W
wusongqing 已提交
374
getLineNumber(): number
Z
zengyawen 已提交
375 376 377

Obtains the current line number, starting from 1.

W
wusongqing 已提交
378
**Return value**
Z
zengyawen 已提交
379

W
wusongqing 已提交
380 381 382
| Type| Description|
| -------- | -------- |
| number | Line number obtained.|
Z
zengyawen 已提交
383 384


W
wusongqing 已提交
385
### getName
Z
zengyawen 已提交
386

W
wusongqing 已提交
387
getName(): string
Z
zengyawen 已提交
388 389 390

Obtains the name of this element.

W
wusongqing 已提交
391
**Return value**
Z
zengyawen 已提交
392

W
wusongqing 已提交
393 394 395
| Type| Description|
| -------- | -------- |
| string | Element name obtained.|
Z
zengyawen 已提交
396 397


W
wusongqing 已提交
398
### getNamespace
Z
zengyawen 已提交
399

W
wusongqing 已提交
400
getNamespace(): string
Z
zengyawen 已提交
401 402 403

Obtains the namespace of this element.

W
wusongqing 已提交
404
**Return value**
Z
zengyawen 已提交
405

W
wusongqing 已提交
406 407 408
| Type| Description|
| -------- | -------- |
| string | Namespace obtained.|
Z
zengyawen 已提交
409 410


W
wusongqing 已提交
411
### getPrefix
Z
zengyawen 已提交
412

W
wusongqing 已提交
413
getPrefix(): string
Z
zengyawen 已提交
414 415 416

Obtains the prefix of this element.

W
wusongqing 已提交
417
**Return value**
Z
zengyawen 已提交
418

W
wusongqing 已提交
419 420 421
| Type| Description|
| -------- | -------- |
| string | Element prefix obtained.|
Z
zengyawen 已提交
422 423


W
wusongqing 已提交
424
### getText
Z
zengyawen 已提交
425

W
wusongqing 已提交
426
getText(): string
Z
zengyawen 已提交
427 428 429

Obtains the text of the current event.

W
wusongqing 已提交
430
**Return value**
Z
zengyawen 已提交
431

W
wusongqing 已提交
432 433 434
| Type| Description|
| -------- | -------- |
| string | Text content obtained.|
Z
zengyawen 已提交
435 436


W
wusongqing 已提交
437
### isEmptyElementTag
Z
zengyawen 已提交
438

W
wusongqing 已提交
439
isEmptyElementTag(): boolean
Z
zengyawen 已提交
440 441 442

Checks whether the current element is empty.

W
wusongqing 已提交
443
**Return value**
Z
zengyawen 已提交
444

W
wusongqing 已提交
445 446 447
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the element is empty; returns **false** otherwise.|
Z
zengyawen 已提交
448 449


W
wusongqing 已提交
450
### isWhitespace
Z
zengyawen 已提交
451

W
wusongqing 已提交
452
isWhitespace(): boolean
Z
zengyawen 已提交
453 454 455

Checks whether the current text event contains only whitespace characters.

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

W
wusongqing 已提交
458 459 460
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the text event contains only whitespace characters; returns **false** otherwise.|
Z
zengyawen 已提交
461 462


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

W
wusongqing 已提交
465
getAttributeCount(): number
Z
zengyawen 已提交
466 467 468

Obtains the number of attributes for the current start tag.

W
wusongqing 已提交
469 470 471 472
**Return value**
| Type| Description|
| -------- | -------- |
| number | Number of attributes obtained.|
Z
zengyawen 已提交
473 474


W
wusongqing 已提交
475
## EventType
Z
zengyawen 已提交
476 477 478

Enumerates the events.

W
wusongqing 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491
| 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.|