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