1# @ohos.xml (XML Parsing and Generation) 2 3> **NOTE** 4> 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. 6 7 8## Modules to Import 9 10``` 11import xml from '@ohos.xml'; 12``` 13 14## XmlSerializer 15 16 17### constructor 18 19constructor(buffer: ArrayBuffer | DataView, encoding?: string) 20 21A constructor used to create an **XmlSerializer** instance. 22 23**System capability**: SystemCapability.Utils.Lang 24 25**Parameters** 26 27| Name | Type | Mandatory| Description | 28| -------- | --------------------------------- | ---- | ------------------------------------------------ | 29| buffer | ArrayBuffer \| DataView | Yes | **ArrayBuffer** or **DataView** for storing the XML information to write.| 30| encoding | string | No | Encoding format. The default value is **'utf-8'** (the only format currently supported). | 31 32**Example** 33 34```js 35let arrayBuffer = new ArrayBuffer(2048); 36let thatSer = new xml.XmlSerializer(arrayBuffer, "utf-8"); 37thatSer.setDeclaration(); 38let result = '<?xml version="1.0" encoding="utf-8"?>'; 39let view = new Uint8Array(arrayBuffer); 40let view1 = ""; 41for (let i = 0; i < result.length; ++i) { 42 view1 = view1 + String.fromCodePoint(view[i]); 43} 44console.log(view1) //<?xml version="1.0" encoding="utf-8"?> 45``` 46 47 48### setAttributes 49 50setAttributes(name: string, value: string): void 51 52Sets an attribute. 53 54**System capability**: SystemCapability.Utils.Lang 55 56**Parameters** 57 58| Name| Type | Mandatory| Description | 59| ------ | ------ | ---- | --------------- | 60| name | string | Yes | Key of the attribute. | 61| value | string | Yes | Value of the attribute.| 62 63**Example** 64 65```js 66const myMAX = 2048; 67let arrayBuffer = new ArrayBuffer(myMAX); 68let thatSer = new xml.XmlSerializer(arrayBuffer); 69thatSer.startElement("note"); 70thatSer.setAttributes("importance1", "high1"); 71thatSer.endElement(); 72let result = '<note importance1="high1"/>'; 73let view = new Uint8Array(arrayBuffer); 74let view1 = ""; 75for (let i = 0; i < result.length; ++i) { 76 view1 = view1 + String.fromCodePoint(view[i]); 77} 78console.log(view1) //<note importance1="high1"/> 79``` 80 81 82### addEmptyElement 83 84addEmptyElement(name: string): void 85 86Adds an empty element. 87 88**System capability**: SystemCapability.Utils.Lang 89 90**Parameters** 91 92| Name| Type | Mandatory| Description | 93| ------ | ------ | ---- | ------------------ | 94| name | string | Yes | Name of the empty element to add.| 95 96**Example** 97 98```js 99const myMAX = 2048; 100let arrayBuffer = new ArrayBuffer(myMAX); 101let thatSer = new xml.XmlSerializer(arrayBuffer); 102thatSer.addEmptyElement("d"); 103let result = '<d/>'; 104let view = new Uint8Array(arrayBuffer); 105let view1 = ""; 106for (let i = 0; i < result.length; ++i) { 107 view1 = view1 + String.fromCodePoint(view[i]); 108} 109console.log(view1) //<d/> 110``` 111 112 113### setDeclaration 114 115setDeclaration(): void 116 117Sets a declaration. 118 119**System capability**: SystemCapability.Utils.Lang 120 121**Example** 122 123```js 124const myMAX = 2048; 125let arrayBuffer = new ArrayBuffer(myMAX); 126let thatSer = new xml.XmlSerializer(arrayBuffer); 127thatSer.setDeclaration(); 128thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 129thatSer.startElement("note"); 130thatSer.endElement(); 131let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>'; 132let view = new Uint8Array(arrayBuffer); 133let view1 = ""; 134for (let i = 0; i < result.length; ++i) { 135 view1 = view1 + String.fromCodePoint(view[i]); 136} 137console.log(view1) //<?xml version="1.0" encoding="utf-8"?> 138``` 139 140 141### startElement 142 143startElement(name: string): void 144 145Writes the start tag based on the given element name. 146 147**System capability**: SystemCapability.Utils.Lang 148 149**Parameters** 150 151| Name| Type | Mandatory| Description | 152| ------ | ------ | ---- | ------------------ | 153| name | string | Yes | Name of the element.| 154 155**Example** 156 157```js 158const myMAX = 2048; 159let arrayBuffer = new ArrayBuffer(myMAX); 160let thatSer = new xml.XmlSerializer(arrayBuffer); 161thatSer.setDeclaration(); 162thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 163thatSer.startElement("note"); 164thatSer.endElement(); 165let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>'; 166let view = new Uint8Array(arrayBuffer); 167let view1 = ""; 168for (let i = 0; i < result.length; ++i) { 169 view1 = view1 + String.fromCodePoint(view[i]); 170} 171console.log(JSON.stringify(view1)) //<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/> 172``` 173 174### endElement 175 176endElement(): void 177 178Writes the end tag of the element. 179 180**System capability**: SystemCapability.Utils.Lang 181 182**Example** 183 184```js 185const myMAX = 2048; 186let arrayBuffer = new ArrayBuffer(myMAX); 187let thatSer = new xml.XmlSerializer(arrayBuffer); 188thatSer.setDeclaration(); 189thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 190thatSer.startElement("note"); 191thatSer.endElement(); 192let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>'; 193let view = new Uint8Array(arrayBuffer); 194let view1 = ""; 195for (let i = 0; i < result.length; ++i) { 196 view1 = view1 + String.fromCodePoint(view[i]); 197} 198console.log(JSON.stringify(view1)) //<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/> 199``` 200 201 202### setNamespace 203 204setNamespace(prefix: string, namespace: string): void 205 206Sets the namespace for an element tag. 207 208**System capability**: SystemCapability.Utils.Lang 209 210**Parameters** 211 212| Name | Type | Mandatory| Description | 213| --------- | ------ | ---- | ------------------------------ | 214| prefix | string | Yes | Prefix of the element and its child elements. | 215| namespace | string | Yes | Namespace to set.| 216 217**Example** 218 219```js 220const myMAX = 2048; 221let arrayBuffer = new ArrayBuffer(myMAX); 222let thatSer = new xml.XmlSerializer(arrayBuffer); 223thatSer.setDeclaration(); 224thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 225thatSer.startElement("note"); 226thatSer.endElement(); 227let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>'; 228let view = new Uint8Array(arrayBuffer); 229let view1 = ""; 230for (let i = 0; i < result.length; ++i) { 231 view1 = view1 + String.fromCodePoint(view[i]); 232} 233console.log(JSON.stringify(view1)) //<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/> 234``` 235 236### setComment 237 238setComment(text: string): void 239 240Sets the comment. 241 242**System capability**: SystemCapability.Utils.Lang 243 244**Parameters** 245 246| Name| Type | Mandatory| Description | 247| ------ | ------ | ---- | -------------------- | 248| text | string | Yes | Comment to set.| 249 250**Example** 251 252```js 253const myMAX = 2048; 254let arrayBuffer = new ArrayBuffer(myMAX); 255let thatSer = new xml.XmlSerializer(arrayBuffer); 256thatSer.setComment("Hello, World!"); 257let result = '<!--Hello, World!-->'; 258let view = new Uint8Array(arrayBuffer); 259let view1 = ""; 260for (let i = 0; i < result.length; ++i) { 261 view1 = view1 + String.fromCodePoint(view[i]); 262} 263console.log(view1) //<!--Hello, World!-->' 264``` 265 266 267### setCDATA 268 269setCDATA(text: string): void 270 271Sets CDATA attributes. 272 273**System capability**: SystemCapability.Utils.Lang 274 275**Parameters** 276 277| Name| Type | Mandatory| Description | 278| ------ | ------ | ---- | ----------------- | 279| text | string | Yes | CDATA attribute to set.| 280 281**Example** 282 283```js 284const myMAX = 2048; 285let arrayBuffer = new ArrayBuffer(myMAX); 286let thatSer = new xml.XmlSerializer(arrayBuffer); 287thatSer.setCDATA('root SYSTEM') 288let result = '<![CDATA[root SYSTEM]]>'; 289let view = new Uint8Array(arrayBuffer); 290let view1 = ""; 291for (let i = 0; i < result.length; ++i) { 292 view1 = view1 + String.fromCodePoint(view[i]); 293} 294console.log(view1) //'<![CDATA[root SYSTEM]]>'' 295``` 296 297 298### setText 299 300setText(text: string): void 301 302Sets **Text**. 303 304**System capability**: SystemCapability.Utils.Lang 305 306**Parameters** 307 308| Name| Type | Mandatory| Description | 309| ------ | ------ | ---- | ---------------- | 310| text | string | Yes | Content of the **Text** to set.| 311 312**Example** 313 314```js 315const myMAX = 2048; 316let arrayBuffer = new ArrayBuffer(myMAX); 317let thatSer = new xml.XmlSerializer(arrayBuffer); 318thatSer.startElement("note"); 319thatSer.setAttributes("importance", "high"); 320thatSer.setText("Happy1"); 321thatSer.endElement(); 322let result = '<note importance="high">Happy1</note>'; 323let view = new Uint8Array(arrayBuffer); 324let view1 = ""; 325for (let i = 0; i < result.length; ++i) { 326 view1 = view1 + String.fromCodePoint(view[i]); 327} 328console.log(view1) // '<note importance="high">Happy1</note>' 329``` 330 331 332### setDocType 333 334setDocType(text: string): void 335 336Sets **DocType**. 337 338**System capability**: SystemCapability.Utils.Lang 339 340**Parameters** 341 342| Name| Type | Mandatory| Description | 343| ------ | ------ | ---- | ------------------- | 344| text | string | Yes | Content of **DocType** to set.| 345 346**Example** 347 348```js 349const myMAX = 2048; 350let arrayBuffer = new ArrayBuffer(myMAX); 351let thatSer = new xml.XmlSerializer(arrayBuffer); 352thatSer.setDocType('root SYSTEM "http://www.test.org/test.dtd"'); 353let result = '<!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">'; 354let view = new Uint8Array(arrayBuffer); 355let view1 = ""; 356for (let i = 0; i < result.length; ++i) { 357 view1 = view1 + String.fromCodePoint(view[i]); 358} 359console.log(view1) //'<!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">' 360``` 361 362 363## XmlPullParser 364 365 366### constructor 367 368constructor(buffer: ArrayBuffer | DataView, encoding?: string) 369 370Creates and returns an **XmlPullParser** object. 371 372**System capability**: SystemCapability.Utils.Lang 373 374**Parameters** 375 376| Name | Type | Mandatory| Description | 377| -------- | --------------------------------- | ---- | ------------------------------------------ | 378| buffer | ArrayBuffer \| DataView | Yes | XML text information to be parsed.| 379| encoding | string | No | Encoding format. The default value is **'utf-8'** (the only format currently supported). | 380 381**Example** 382 383```js 384let strXml = 385 '<?xml version="1.0" encoding="utf-8"?>' + 386 '<!DOCTYPE note [\n<!ENTITY foo "baa">]>' + 387 '<note importance="high" logged="true">' + 388 ' <![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>' + 389 ' <!--Hello, World!-->' + 390 ' <company>John & Hans</company>' + 391 ' <title>Happy</title>' + 392 ' <title>Happy</title>' + 393 ' <lens>Work</lens>' + 394 ' <lens>Play</lens>' + 395 ' <?go there?>' + 396 ' <a><b/></a>' + 397 ' <h:table xmlns:h="http://www.w3.org/TR/html4/">' + 398 ' <h:tr>' + 399 ' <h:td>Apples</h:td>' + 400 ' <h:td>Bananas</h:td>' + 401 ' </h:tr>' + 402 ' </h:table>' + 403 '</note>'; 404let textEncoder = new util.TextEncoder(); 405let arrbuffer = textEncoder.encodeInto(strXml); 406let that = new xml.XmlPullParser(arrbuffer.buffer, 'UTF-8'); 407let str1 = ''; 408function func1(name, value){ 409 str1 += name+':'+value; 410 return true; 411} 412let options = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func1} 413that.parse(options); 414console.log(str1) //'note:company:title:title:lens:lens:a:b:h:table:h:tr:h:td:h:td:' 415``` 416 417 418### parse 419 420parse(option: ParseOptions): void 421 422Parses XML information. 423 424**System capability**: SystemCapability.Utils.Lang 425 426**Parameters** 427 428| Name| Type | Mandatory| Description | 429| ------ | ----------------------------- | ---- | -------------------------------- | 430| option | [ParseOptions](#parseoptions) | Yes | Options for controlling and obtaining the parsed information.| 431 432**Example** 433 434```js 435let strXml = 436 '<?xml version="1.0" encoding="utf-8"?>' + 437 '<note importance="high" logged="true">' + 438 ' <title>Happy</title>' + 439 ' <todo>Work</todo>' + 440 ' <todo>Play</todo>' + 441 '</note>'; 442let textEncoder = new util.TextEncoder(); 443let arrbuffer = textEncoder.encodeInto(strXml); 444let that = new xml.XmlPullParser(arrbuffer.buffer); 445let arrTag = {}; 446let str = ""; 447let i = 0; 448function func(key, value){ 449 arrTag[i] = 'key:'+key+' value:'+ value.getDepth(); 450 str += arrTag[i]; 451 i++; 452 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 453} 454let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 455that.parse(options); 456console.log(str); 457// Output: 458// 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 459// Note: 460// 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: 461// 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), ... 462``` 463 464 465## ParseOptions 466 467Defines the XML parsing options. 468 469**System capability**: SystemCapability.Utils.Lang 470 471 472| Name | Type | Mandatory| Description | 473| ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- | 474| supportDoctype | boolean | No | Whether to ignore **Doctype**. The default value is **false**.| 475| ignoreNameSpace | boolean | No | Whether to ignore **Namespace**. The default value is **false**. | 476| tagValueCallbackFunction | (name: string, value: string) => boolean | No | Callback used to return **tagValue**. The default value is **null**. | 477| attributeValueCallbackFunction | (name: string, value: string) => boolean | No | Callback used to return **attributeValue**. The default value is **null**. | 478| tokenValueCallbackFunction | (eventType: [EventType](#eventtype), value: [ParseInfo](#parseinfo)) => boolean | No | Callback used to return **tokenValue**. The default value is **null**. | 479 480## ParseInfo 481 482Provides APIs to manage the parsed XML information. 483 484 485### getColumnNumber 486 487getColumnNumber(): number 488 489Obtains the column line number, starting from 1. 490 491**System capability**: SystemCapability.Utils.Lang 492 493**Return value** 494 495| Type | Description | 496| ------ | -------------- | 497| number | Column number obtained.| 498 499**Example** 500 501```js 502let strXml = 503 '<?xml version="1.0" encoding="utf-8"?>' + 504 '<note importance="high" logged="true">' + 505 ' <title>Happy</title>' + 506 ' <todo>Work</todo>' + 507 ' <todo>Play</todo>' + 508 '</note>'; 509let textEncoder = new util.TextEncoder(); 510let arrbuffer = textEncoder.encodeInto(strXml); 511let that = new xml.XmlPullParser(arrbuffer.buffer); 512let arrTag = {}; 513let str = ""; 514let i = 0; 515function func(key, value){ 516 arrTag[i] = 'key:'+key+' value:'+ value.getColumnNumber(); 517 str += arrTag[i]; 518 i++; 519 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 520} 521let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 522that.parse(options); 523console.log(str); 524// Output: 525// 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 526``` 527 528### getDepth 529 530getDepth(): number 531 532Obtains the depth of this element. 533 534**System capability**: SystemCapability.Utils.Lang 535 536**Return value** 537 538| Type | Description | 539| ------ | -------------------- | 540| number | Depth obtained.| 541 542**Example** 543 544```js 545let strXml = 546 '<?xml version="1.0" encoding="utf-8"?>' + 547 '<note importance="high" logged="true">' + 548 ' <title>Happy</title>' + 549 ' <todo>Work</todo>' + 550 ' <todo>Play</todo>' + 551 '</note>'; 552let textEncoder = new util.TextEncoder(); 553let arrbuffer = textEncoder.encodeInto(strXml); 554let that = new xml.XmlPullParser(arrbuffer.buffer); 555let arrTag = {}; 556let str = ""; 557let i = 0; 558function func(key, value){ 559 arrTag[i] = 'key:'+key+' value:'+ value.getDepth(); 560 str += arrTag[i]; 561 i++; 562 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 563} 564let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 565that.parse(options); 566console.log(str); 567// Output: 568// 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 569// Note: 570// 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: 571// 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), ... 572``` 573 574### getLineNumber 575 576getLineNumber(): number 577 578Obtains the current line number, starting from 1. 579 580**System capability**: SystemCapability.Utils.Lang 581 582**Return value** 583 584| Type | Description | 585| ------ | -------------- | 586| number | Line number obtained.| 587 588**Example** 589 590```js 591let strXml = 592 '<?xml version="1.0" encoding="utf-8"?>' + 593 '<note importance="high" logged="true">' + 594 ' <title>Happy</title>' + 595 ' <todo>Work</todo>' + 596 ' <todo>Play</todo>' + 597 '</note>'; 598let textEncoder = new util.TextEncoder(); 599let arrbuffer = textEncoder.encodeInto(strXml); 600let that = new xml.XmlPullParser(arrbuffer.buffer); 601let arrTag = {}; 602let str = ""; 603let i = 0; 604function func(key, value){ 605 arrTag[i] = 'key:'+key+' value:'+ value.getLineNumber(); 606 str += arrTag[i]; 607 i++; 608 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 609} 610let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 611that.parse(options); 612console.log(str); 613// Output: 614// 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 615``` 616 617### getName 618 619getName(): string 620 621Obtains the name of this element. 622 623**System capability**: SystemCapability.Utils.Lang 624 625**Return value** 626 627| Type | Description | 628| ------ | ------------------ | 629| string | Element name obtained.| 630 631**Example** 632 633```js 634let strXml = 635 '<?xml version="1.0" encoding="utf-8"?>' + 636 '<note importance="high" logged="true">' + 637 ' <title>Happy</title>' + 638 ' <todo>Work</todo>' + 639 ' <todo>Play</todo>' + 640 '</note>'; 641let textEncoder = new util.TextEncoder(); 642let arrbuffer = textEncoder.encodeInto(strXml); 643let that = new xml.XmlPullParser(arrbuffer.buffer); 644let arrTag = {}; 645let str = ""; 646let i = 0; 647function func(key, value){ 648 arrTag[i] = 'key:'+key+' value:'+ value.getName(); 649 str += arrTag[i]; 650 i++; 651 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 652} 653let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 654that.parse(options); 655console.log(str); 656// Output: 657// 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: 658``` 659### getNamespace 660 661getNamespace(): string 662 663Obtains the namespace of this element. 664 665**System capability**: SystemCapability.Utils.Lang 666 667**Return value** 668 669| Type | Description | 670| ------ | ------------------------ | 671| string | Namespace obtained.| 672 673**Example** 674 675```js 676let strXml = 677 '<?xml version="1.0" encoding="utf-8"?>' + 678 '<note importance="high" logged="true">' + 679 ' <title>Happy</title>' + 680 ' <todo>Work</todo>' + 681 ' <todo>Play</todo>' + 682 '</note>'; 683let textEncoder = new util.TextEncoder(); 684let arrbuffer = textEncoder.encodeInto(strXml); 685let that = new xml.XmlPullParser(arrbuffer.buffer); 686let arrTag = {}; 687let str = ""; 688let i = 0; 689function func(key, value){ 690 arrTag[i] = 'key:'+key+' value:'+ value.getNamespace(); 691 str += arrTag[i]; 692 i++; 693 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 694} 695let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 696that.parse(options); 697console.log(str); 698// Output: 699// 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: 700``` 701### getPrefix 702 703getPrefix(): string 704 705Obtains the prefix of this element. 706 707**System capability**: SystemCapability.Utils.Lang 708 709**Return value** 710 711| Type | Description | 712| ------ | ------------------ | 713| string | Element prefix obtained.| 714 715**Example** 716 717```js 718let strXml = 719 '<?xml version="1.0" encoding="utf-8"?>' + 720 '<note importance="high" logged="true">' + 721 ' <title>Happy</title>' + 722 ' <todo>Work</todo>' + 723 ' <todo>Play</todo>' + 724 '</note>'; 725let textEncoder = new util.TextEncoder(); 726let arrbuffer = textEncoder.encodeInto(strXml); 727let that = new xml.XmlPullParser(arrbuffer.buffer); 728let arrTag = {}; 729let str = ""; 730let i = 0; 731function func(key, value){ 732 arrTag[i] = 'key:'+key+' value:'+ value.getPrefix(); 733 str += arrTag[i]; 734 i++; 735 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 736} 737let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 738that.parse(options); 739console.log(str); 740// Output: 741// 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: 742``` 743 744### getText 745 746getText(): string 747 748Obtains the text of the current event. 749 750**System capability**: SystemCapability.Utils.Lang 751 752**Return value** 753 754| Type | Description | 755| ------ | ------------------------ | 756| string | Text content obtained.| 757 758**Example** 759 760```js 761let strXml = 762 '<?xml version="1.0" encoding="utf-8"?>' + 763 '<note importance="high" logged="true">' + 764 ' <title>Happy</title>' + 765 ' <todo>Work</todo>' + 766 ' <todo>Play</todo>' + 767 '</note>'; 768let textEncoder = new util.TextEncoder(); 769let arrbuffer = textEncoder.encodeInto(strXml); 770let that = new xml.XmlPullParser(arrbuffer.buffer); 771let arrTag = {}; 772let str = ""; 773let i = 0; 774function func(key, value){ 775 arrTag[i] = 'key:'+key+' value:'+ value.getText(); 776 str += arrTag[i]; 777 i++; 778 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 779} 780let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 781that.parse(options); 782console.log(str); 783// Output: 784// 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: 785``` 786### isEmptyElementTag 787 788isEmptyElementTag(): boolean 789 790Checks whether the current element is empty. 791 792**System capability**: SystemCapability.Utils.Lang 793 794**Return value** 795 796| Type | Description | 797| ------- | ---------------------------- | 798| boolean | Returns **true** if the element is empty; returns **false** otherwise.| 799 800**Example** 801 802```js 803let strXml = 804 '<?xml version="1.0" encoding="utf-8"?>' + 805 '<note importance="high" logged="true">' + 806 ' <title>Happy</title>' + 807 ' <todo>Work</todo>' + 808 ' <todo>Play</todo>' + 809 '</note>'; 810let textEncoder = new util.TextEncoder(); 811let arrbuffer = textEncoder.encodeInto(strXml); 812let that = new xml.XmlPullParser(arrbuffer.buffer); 813let arrTag = {}; 814let str = ""; 815let i = 0; 816function func(key, value){ 817 arrTag[i] = 'key:'+key+' value:'+ value.isEmptyElementTag(); 818 str += arrTag[i]; 819 i++; 820 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 821} 822let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 823that.parse(options); 824console.log(str); 825// Output: 826// 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 827``` 828### isWhitespace 829 830isWhitespace(): boolean 831 832Checks whether the current text event contains only whitespace characters. 833 834**System capability**: SystemCapability.Utils.Lang 835 836**Return value** 837 838| Type | Description | 839| ------- | -------------------------------------- | 840| boolean | Returns **true** if the text event contains only whitespace characters; returns **false** otherwise.| 841 842**Example** 843 844```js 845let strXml = 846 '<?xml version="1.0" encoding="utf-8"?>' + 847 '<note importance="high" logged="true">' + 848 ' <title>Happy</title>' + 849 ' <todo>Work</todo>' + 850 ' <todo>Play</todo>' + 851 '</note>'; 852let textEncoder = new util.TextEncoder(); 853let arrbuffer = textEncoder.encodeInto(strXml); 854let that = new xml.XmlPullParser(arrbuffer.buffer); 855let arrTag = {}; 856let str = ""; 857let i = 0; 858function func(key, value){ 859 arrTag[i] = 'key:'+key+' value:'+ value.isWhitespace(); 860 str += arrTag[i]; 861 i++; 862 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 863} 864let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 865that.parse(options); 866console.log(str); 867// Output: 868// 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 869``` 870### getAttributeCount 871 872getAttributeCount(): number 873 874Obtains the number of attributes for the current start tag. 875 876**System capability**: SystemCapability.Utils.Lang 877 878**Return value** 879| Type | Description | 880| ------ | ---------------------- | 881| number | Number of attributes obtained.| 882 883**Example** 884 885```js 886let strXml = 887 '<?xml version="1.0" encoding="utf-8"?>' + 888 '<note importance="high" logged="true">' + 889 ' <title>Happy</title>' + 890 ' <todo>Work</todo>' + 891 ' <todo>Play</todo>' + 892 '</note>'; 893let textEncoder = new util.TextEncoder(); 894let arrbuffer = textEncoder.encodeInto(strXml); 895let that = new xml.XmlPullParser(arrbuffer.buffer); 896let arrTag = {}; 897let str = ""; 898let i = 0; 899function func(key, value){ 900 arrTag[i] = 'key:'+key+' value:'+ value.getAttributeCount(); 901 str += arrTag[i]; 902 i++; 903 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 904} 905let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 906that.parse(options); 907console.log(str); 908// Output: 909// 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 910``` 911 912## EventType 913 914Enumerates the events. 915 916**System capability**: SystemCapability.Utils.Lang 917 918| Name | Value | Description | 919| ---------------- | ---- | --------------------- | 920| START_DOCUMENT | 0 | Indicates a start document event. | 921| END_DOCUMENT | 1 | Indicates an end document event. | 922| START_TAG | 2 | Indicates a start tag event. | 923| END_TAG | 3 | Indicates an end tag event. | 924| TEXT | 4 | Indicates a text event. | 925| CDSECT | 5 | Indicates a CDATA section event. | 926| COMMENT | 6 | Indicates an XML comment event. | 927| DOCDECL | 7 | Indicates an XML document type declaration event.| 928| INSTRUCTION | 8 | Indicates an XML processing instruction event.| 929| ENTITY_REFERENCE | 9 | Indicates an entity reference event. | 930| WHITESPACE | 10 | Indicates a whitespace character event. | 931