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