1# @ohos.xml (xml解析与生成) 2 3> **说明:** 4> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 5 6 7## 导入模块 8 9``` 10import xml from '@ohos.xml'; 11``` 12 13## XmlSerializer 14 15 16### constructor 17 18constructor(buffer: ArrayBuffer | DataView, encoding?: string) 19 20XmlSerializer的构造函数。 21 22**系统能力:** SystemCapability.Utils.Lang 23 24**参数:** 25 26| 参数名 | 类型 | 必填 | 说明 | 27| -------- | --------------------------------- | ---- | ------------------------------------------------ | 28| buffer | ArrayBuffer \| DataView | 是 | 用于接收写入xml信息的ArrayBuffer或DataView内存。 | 29| encoding | string | 否 | 编码格式 , 默认'utf-8'(目前仅支持'utf-8')。 | 30 31**示例:** 32 33```ts 34let arrayBuffer = new ArrayBuffer(2048); 35let thatSer = new xml.XmlSerializer(arrayBuffer, "utf-8"); 36thatSer.setDeclaration(); 37let result = '<?xml version="1.0" encoding="utf-8"?>'; 38let view = new Uint8Array(arrayBuffer); 39let view1 = ""; 40for (let i = 0; i < result.length; ++i) { 41 view1 = view1 + String.fromCodePoint(view[i]); 42} 43console.log(view1) // <?xml version="1.0" encoding="utf-8"?> 44``` 45 46 47### setAttributes 48 49setAttributes(name: string, value: string): void 50 51写入元素的属性和属性值。 52 53**系统能力:** SystemCapability.Utils.Lang 54 55**参数:** 56 57| 参数名 | 类型 | 必填 | 说明 | 58| ------ | ------ | ---- | --------------- | 59| name | string | 是 | 属性。 | 60| value | string | 是 | 属性值。 | 61 62**示例:** 63 64```ts 65const myMAX = 2048; 66let arrayBuffer = new ArrayBuffer(myMAX); 67let thatSer = new xml.XmlSerializer(arrayBuffer); 68thatSer.startElement("note"); 69thatSer.setAttributes("importance1", "high1"); 70thatSer.endElement(); 71let result = '<note importance1="high1"/>'; 72let view = new Uint8Array(arrayBuffer); 73let view1 = ""; 74for (let i = 0; i < result.length; ++i) { 75 view1 = view1 + String.fromCodePoint(view[i]); 76} 77console.log(view1) // <note importance1="high1"/> 78``` 79 80 81### addEmptyElement 82 83addEmptyElement(name: string): void 84 85写入一个空元素。 86 87**系统能力:** SystemCapability.Utils.Lang 88 89**参数:** 90 91| 参数名 | 类型 | 必填 | 说明 | 92| ------ | ------ | ---- | ------------------ | 93| name | string | 是 | 该空元素的元素名。 | 94 95**示例:** 96 97```ts 98const myMAX = 2048; 99let arrayBuffer = new ArrayBuffer(myMAX); 100let thatSer = new xml.XmlSerializer(arrayBuffer); 101thatSer.addEmptyElement("d"); 102let result = '<d/>'; 103let view = new Uint8Array(arrayBuffer); 104let view1 = ""; 105for (let i = 0; i < result.length; ++i) { 106 view1 = view1 + String.fromCodePoint(view[i]); 107} 108console.log(view1) // <d/> 109``` 110 111 112### setDeclaration 113 114setDeclaration(): void 115 116写入XML文件声明。 117 118**系统能力:** SystemCapability.Utils.Lang 119 120**示例:** 121 122```ts 123const myMAX = 2048; 124let arrayBuffer = new ArrayBuffer(myMAX); 125let thatSer = new xml.XmlSerializer(arrayBuffer); 126thatSer.setDeclaration(); 127thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 128thatSer.startElement("note"); 129thatSer.endElement(); 130let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>'; 131let view = new Uint8Array(arrayBuffer); 132let view1 = ""; 133for (let i = 0; i < result.length; ++i) { 134 view1 = view1 + String.fromCodePoint(view[i]); 135} 136console.log(view1) 137// <?xml version="1.0" encoding="utf-8"?> 138// <h:note xmlns:h="http://www.w3.org/TR/html4/"/> 139``` 140 141 142### startElement 143 144startElement(name: string): void 145 146根据给定名称写入元素开始标记。 147 148**系统能力:** SystemCapability.Utils.Lang 149 150**参数:** 151 152| 参数名 | 类型 | 必填 | 说明 | 153| ------ | ------ | ---- | ------------------ | 154| name | string | 是 | 当前元素的元素名。 | 155 156**示例:** 157 158```ts 159const myMAX = 2048; 160let arrayBuffer = new ArrayBuffer(myMAX); 161let thatSer = new xml.XmlSerializer(arrayBuffer); 162thatSer.setDeclaration(); 163thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 164thatSer.startElement("note"); 165thatSer.endElement(); 166let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>'; 167let view = new Uint8Array(arrayBuffer); 168let view1 = ""; 169for (let i = 0; i < result.length; ++i) { 170 view1 = view1 + String.fromCodePoint(view[i]); 171} 172console.log(JSON.stringify(view1)) // <?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/> 173``` 174 175### endElement 176 177endElement(): void 178 179写入元素结束标记。 180 181**系统能力:** SystemCapability.Utils.Lang 182 183**示例:** 184 185```ts 186const myMAX = 2048; 187let arrayBuffer = new ArrayBuffer(myMAX); 188let thatSer = new xml.XmlSerializer(arrayBuffer); 189thatSer.setDeclaration(); 190thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 191thatSer.startElement("note"); 192thatSer.endElement(); 193let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>'; 194let view = new Uint8Array(arrayBuffer); 195let view1 = ""; 196for (let i = 0; i < result.length; ++i) { 197 view1 = view1 + String.fromCodePoint(view[i]); 198} 199console.log(JSON.stringify(view1)) // <?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/> 200``` 201 202 203### setNamespace 204 205setNamespace(prefix: string, namespace: string): void 206 207写入当前元素标记的命名空间。 208 209**系统能力:** SystemCapability.Utils.Lang 210 211**参数:** 212 213| 参数名 | 类型 | 必填 | 说明 | 214| --------- | ------ | ---- | ------------------------------ | 215| prefix | string | 是 | 当前元素及其子元素的前缀。 | 216| namespace | string | 是 | 当前元素及其子元素的命名空间。 | 217 218**示例:** 219 220```ts 221const myMAX = 2048; 222let arrayBuffer = new ArrayBuffer(myMAX); 223let thatSer = new xml.XmlSerializer(arrayBuffer); 224thatSer.setDeclaration(); 225thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); 226thatSer.startElement("note"); 227thatSer.endElement(); 228let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>'; 229let view = new Uint8Array(arrayBuffer); 230let view1 = ""; 231for (let i = 0; i < result.length; ++i) { 232 view1 = view1 + String.fromCodePoint(view[i]); 233} 234console.log(JSON.stringify(view1)) // <?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/> 235``` 236 237### setComment 238 239setComment(text: string): void 240 241写入注释内容。 242 243**系统能力:** SystemCapability.Utils.Lang 244 245**参数:** 246 247| 参数名 | 类型 | 必填 | 说明 | 248| ------ | ------ | ---- | -------------------- | 249| text | string | 是 | 当前元素的注释内容。 | 250 251**示例:** 252 253```ts 254const myMAX = 2048; 255let arrayBuffer = new ArrayBuffer(myMAX); 256let thatSer = new xml.XmlSerializer(arrayBuffer); 257thatSer.setComment("Hello, World!"); 258let result = '<!--Hello, World!-->'; 259let view = new Uint8Array(arrayBuffer); 260let view1 = ""; 261for (let i = 0; i < result.length; ++i) { 262 view1 = view1 + String.fromCodePoint(view[i]); 263} 264console.log(view1) // <!--Hello, World!--> 265``` 266 267 268### setCDATA 269 270setCDATA(text: string): void 271 272写入CDATA数据。 273 274**系统能力:** SystemCapability.Utils.Lang 275 276**参数:** 277 278| 参数名 | 类型 | 必填 | 说明 | 279| ------ | ------ | ---- | ----------------- | 280| text | string | 是 | CDATA属性的内容。 | 281 282**示例:** 283 284```ts 285const myMAX = 2048; 286let arrayBuffer = new ArrayBuffer(myMAX); 287let thatSer = new xml.XmlSerializer(arrayBuffer); 288thatSer.setCDATA('root SYSTEM') 289let result = '<![CDATA[root SYSTEM]]>'; 290let view = new Uint8Array(arrayBuffer); 291let view1 = ""; 292for (let i = 0; i < result.length; ++i) { 293 view1 = view1 + String.fromCodePoint(view[i]); 294} 295console.log(view1) // <![CDATA[root SYSTEM]]> 296``` 297 298 299### setText 300 301setText(text: string): void 302 303写入标签值。 304 305**系统能力:** SystemCapability.Utils.Lang 306 307**参数:** 308 309| 参数名 | 类型 | 必填 | 说明 | 310| ------ | ------ | ---- | ---------------- | 311| text | string | 是 | text属性的内容。 | 312 313**示例:** 314 315```ts 316const myMAX = 2048; 317let arrayBuffer = new ArrayBuffer(myMAX); 318let thatSer = new xml.XmlSerializer(arrayBuffer); 319thatSer.startElement("note"); 320thatSer.setAttributes("importance", "high"); 321thatSer.setText("Happy1"); 322thatSer.endElement(); 323let result = '<note importance="high">Happy1</note>'; 324let view = new Uint8Array(arrayBuffer); 325let view1 = ""; 326for (let i = 0; i < result.length; ++i) { 327 view1 = view1 + String.fromCodePoint(view[i]); 328} 329console.log(view1) // <note importance="high">Happy1</note> 330``` 331 332 333### setDocType 334 335setDocType(text: string): void 336 337写入文档类型。 338 339**系统能力:** SystemCapability.Utils.Lang 340 341**参数:** 342 343| 参数名 | 类型 | 必填 | 说明 | 344| ------ | ------ | ---- | ------------------- | 345| text | string | 是 | DocType属性的内容。 | 346 347**示例:** 348 349```ts 350const myMAX = 2048; 351let arrayBuffer = new ArrayBuffer(myMAX); 352let thatSer = new xml.XmlSerializer(arrayBuffer); 353thatSer.setDocType('root SYSTEM "http://www.test.org/test.dtd"'); 354let result = '<!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">'; 355let view = new Uint8Array(arrayBuffer); 356let view1 = ""; 357for (let i = 0; i < result.length; ++i) { 358 view1 = view1 + String.fromCodePoint(view[i]); 359} 360console.log(view1) // <!DOCTYPE root SYSTEM "http://www.test.org/test.dtd"> 361``` 362 363 364## XmlPullParser 365 366 367### constructor 368 369constructor(buffer: ArrayBuffer | DataView, encoding?: string) 370 371构造并返回一个XmlPullParser对象。 372 373**系统能力:** SystemCapability.Utils.Lang 374 375**参数:** 376 377| 参数名 | 类型 | 必填 | 说明 | 378| -------- | --------------------------------- | ---- | ------------------------------------------ | 379| buffer | ArrayBuffer \| DataView | 是 | 需要解析的xml文本信息。 | 380| encoding | string | 否 | 编码格式 , 默认'utf-8'(目前仅支持'utf-8')。 | 381 382**示例:** 383 384```ts 385import util from '@ohos.util'; 386let strXml = 387 '<?xml version="1.0" encoding="utf-8"?>' + 388 '<!DOCTYPE note [\n<!ENTITY foo "baa">]>' + 389 '<note importance="high" logged="true">' + 390 ' <![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>' + 391 ' <!--Hello, World!-->' + 392 ' <company>John & Hans</company>' + 393 ' <title>Happy</title>' + 394 ' <title>Happy</title>' + 395 ' <lens>Work</lens>' + 396 ' <lens>Play</lens>' + 397 ' <?go there?>' + 398 ' <a><b/></a>' + 399 ' <h:table xmlns:h="http://www.w3.org/TR/html4/">' + 400 ' <h:tr>' + 401 ' <h:td>Apples</h:td>' + 402 ' <h:td>Bananas</h:td>' + 403 ' </h:tr>' + 404 ' </h:table>' + 405 '</note>'; 406let textEncoder = new util.TextEncoder(); 407let arrbuffer = textEncoder.encodeInto(strXml); 408let that = new xml.XmlPullParser(arrbuffer.buffer, 'UTF-8'); 409let str1 = ''; 410function func1(name: string, value: string) { 411 str1 += name + value; 412 return true; 413} 414let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func1} 415that.parse(options); 416console.log(str1) 417// 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 418``` 419 420 421### parse 422 423parse(option: ParseOptions): void 424 425该接口用于解析xml。 426 427**系统能力:** SystemCapability.Utils.Lang 428 429**参数:** 430 431| 参数名 | 类型 | 必填 | 说明 | 432| ------ | ----------------------------- | ---- | -------------------------------- | 433| option | [ParseOptions](#parseoptions) | 是 | 用户控制以及获取解析信息的选项。 | 434 435**示例:** 436 437```ts 438import util from '@ohos.util'; 439let strXml = 440 '<?xml version="1.0" encoding="utf-8"?>' + 441 '<note importance="high" logged="true">' + 442 ' <title>Happy</title>' + 443 ' <todo>Work</todo>' + 444 ' <todo>Play</todo>' + 445 '</note>'; 446let textEncoder = new util.TextEncoder(); 447let arrbuffer = textEncoder.encodeInto(strXml); 448let that = new xml.XmlPullParser(arrbuffer.buffer); 449let str = ""; 450function func(key: xml.EventType, value: xml.ParseInfo) { 451 str += 'key:' + key + ' value:' + value.getDepth() + ' '; 452 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 453} 454let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 455that.parse(options); 456console.log(str); 457// 输出: 458// 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 459// 解析: 460// key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为: 461// 0(START_DOCUMENT):0(起始深度为0), 2(START_TAG):1(解析到开始标签node, 对应深度为1), 10(WHITESPACE):1(解析到空白标签空格, 对应深度为1), 2(START_TAG):2(解析到开始标签title, 对应深度为2), ... 462``` 463 464 465## ParseOptions 466 467xml解析选项。 468 469**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang 470 471 472| 名称 | 类型 | 必填 | 说明 | 473| ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- | 474| supportDoctype | boolean | 否 | 是否忽略文档类型,默认false,表示解析文档类型。 | 475| ignoreNameSpace | boolean | 否 | 是否忽略命名空间,默认false,表示解析命名空间。 | 476| tagValueCallbackFunction | (name: string, value: string) => boolean | 否 | 获取tagValue回调函数,解析标签和标签值,默认null,表示不解析标签和标签值。 | 477| attributeValueCallbackFunction | (name: string, value: string) => boolean | 否 | 获取attributeValue回调函数,解析属性和属性值,默认null,表示不解析属性和属性值。| 478| tokenValueCallbackFunction | (eventType: [EventType](#eventtype), value: [ParseInfo](#parseinfo)) => boolean | 否 | 获取tokenValue回调函数,,解析元素事件类型([EventType](#eventtype))和[ParseInfo](#parseinfo)属性,默认null,表示不解析元素事件类型和ParseInfo属性。| 479 480## ParseInfo 481 482当前xml解析信息。 483 484 485### getColumnNumber 486 487getColumnNumber(): number 488 489获取当前列号,从1开始。 490 491**系统能力:** SystemCapability.Utils.Lang 492 493**返回值:** 494 495| 类型 | 说明 | 496| ------ | -------------- | 497| number | 返回当前列号。 | 498 499**示例:** 500 501```ts 502import util from '@ohos.util'; 503let strXml = 504 '<?xml version="1.0" encoding="utf-8"?>' + 505 '<note importance="high" logged="true">' + 506 ' <title>Happy</title>' + 507 ' <todo>Work</todo>' + 508 ' <todo>Play</todo>' + 509 '</note>'; 510let textEncoder = new util.TextEncoder(); 511let arrbuffer = textEncoder.encodeInto(strXml); 512let that = new xml.XmlPullParser(arrbuffer.buffer); 513let str = ""; 514function func(key: xml.EventType, value: xml.ParseInfo) { 515 str += 'key:' + key + ' value:' + value.getColumnNumber() + ' '; 516 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 517} 518let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 519that.parse(options); 520console.log(str); 521// 输出: 522// 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 523``` 524 525### getDepth 526 527getDepth(): number 528 529获取元素的当前深度。 530 531**系统能力:** SystemCapability.Utils.Lang 532 533**返回值:** 534 535| 类型 | 说明 | 536| ------ | -------------------- | 537| number | 返回元素的当前深度。 | 538 539**示例:** 540 541```ts 542import util from '@ohos.util'; 543let strXml = 544 '<?xml version="1.0" encoding="utf-8"?>' + 545 '<note importance="high" logged="true">' + 546 ' <title>Happy</title>' + 547 ' <todo>Work</todo>' + 548 ' <todo>Play</todo>' + 549 '</note>'; 550let textEncoder = new util.TextEncoder(); 551let arrbuffer = textEncoder.encodeInto(strXml); 552let that = new xml.XmlPullParser(arrbuffer.buffer); 553let str = ""; 554function func(key: xml.EventType, value: xml.ParseInfo) { 555 str += 'key:' + key + ' value:' + value.getDepth() + ' '; 556 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 557} 558let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 559that.parse(options); 560console.log(str); 561// 输出: 562// 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 563// 解析: 564// key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为: 565// 0(START_DOCUMENT):0(起始深度为0), 2(START_TAG):1(解析到开始标签node, 对应深度为1), 10(WHITESPACE):1(解析到空白标签空格, 对应深度为1), 2(START_TAG):2(解析到开始标签title, 对应深度为2), ... 566``` 567 568### getLineNumber 569 570getLineNumber(): number 571 572获取当前行号,从1开始。 573 574**系统能力:** SystemCapability.Utils.Lang 575 576**返回值:** 577 578| 类型 | 说明 | 579| ------ | -------------- | 580| number | 返回当前行号。 | 581 582**示例:** 583 584```ts 585import util from '@ohos.util'; 586let strXml = 587 '<?xml version="1.0" encoding="utf-8"?>' + 588 '<note importance="high" logged="true">' + 589 ' <title>Happy</title>' + 590 ' <todo>Work</todo>' + 591 ' <todo>Play</todo>' + 592 '</note>'; 593let textEncoder = new util.TextEncoder(); 594let arrbuffer = textEncoder.encodeInto(strXml); 595let that = new xml.XmlPullParser(arrbuffer.buffer); 596let str = ""; 597function func(key: xml.EventType, value: xml.ParseInfo) { 598 str += 'key:' + key + ' value:' + value.getLineNumber() + ' '; 599 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 600} 601let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 602that.parse(options); 603console.log(str); 604// 输出: 605// 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 606``` 607 608### getName 609 610getName(): string 611 612获取当前元素名称。 613 614**系统能力:** SystemCapability.Utils.Lang 615 616**返回值:** 617 618| 类型 | 说明 | 619| ------ | ------------------ | 620| string | 返回当前元素名称。 | 621 622**示例:** 623 624```ts 625import util from '@ohos.util'; 626let strXml = 627 '<?xml version="1.0" encoding="utf-8"?>' + 628 '<note importance="high" logged="true">' + 629 ' <title>Happy</title>' + 630 ' <todo>Work</todo>' + 631 ' <todo>Play</todo>' + 632 '</note>'; 633let textEncoder = new util.TextEncoder(); 634let arrbuffer = textEncoder.encodeInto(strXml); 635let that = new xml.XmlPullParser(arrbuffer.buffer); 636let str = ""; 637function func(key: xml.EventType, value: xml.ParseInfo) { 638 str += 'key:' + key + ' value:' + value.getName() + ' '; 639 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 640} 641let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 642that.parse(options); 643console.log(str); 644// 输出: 645// 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: 646``` 647### getNamespace 648 649getNamespace(): string 650 651获取当前元素的命名空间。 652 653**系统能力:** SystemCapability.Utils.Lang 654 655**返回值:** 656 657| 类型 | 说明 | 658| ------ | ------------------------ | 659| string | 返回当前元素的命名空间。 | 660 661**示例:** 662 663```ts 664import util from '@ohos.util'; 665let strXml = 666 '<?xml version="1.0" encoding="utf-8"?>' + 667 '<note importance="high" logged="true">' + 668 ' <title>Happy</title>' + 669 ' <todo>Work</todo>' + 670 ' <todo>Play</todo>' + 671 '</note>'; 672let textEncoder = new util.TextEncoder(); 673let arrbuffer = textEncoder.encodeInto(strXml); 674let that = new xml.XmlPullParser(arrbuffer.buffer); 675let str = ""; 676function func(key: xml.EventType, value: xml.ParseInfo) { 677 str += 'key:' + key + ' value:' + value.getNamespace() + ' '; 678 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 679} 680let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 681that.parse(options); 682console.log(str); 683// 输出: 684// 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: 685``` 686### getPrefix 687 688getPrefix(): string 689 690获取当前元素前缀。 691 692**系统能力:** SystemCapability.Utils.Lang 693 694**返回值:** 695 696| 类型 | 说明 | 697| ------ | ------------------ | 698| string | 返回当前元素前缀。 | 699 700**示例:** 701 702```ts 703import util from '@ohos.util'; 704let strXml = 705 '<?xml version="1.0" encoding="utf-8"?>' + 706 '<note importance="high" logged="true">' + 707 ' <title>Happy</title>' + 708 ' <todo>Work</todo>' + 709 ' <todo>Play</todo>' + 710 '</note>'; 711let textEncoder = new util.TextEncoder(); 712let arrbuffer = textEncoder.encodeInto(strXml); 713let that = new xml.XmlPullParser(arrbuffer.buffer); 714let str = ""; 715function func(key: xml.EventType, value: xml.ParseInfo) { 716 str += 'key:' + key + ' value:' + value.getPrefix() + ' '; 717 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 718} 719let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 720that.parse(options); 721console.log(str); 722// 输出: 723// 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: 724``` 725 726### getText 727 728getText(): string 729 730获取当前事件的文本内容。 731 732**系统能力:** SystemCapability.Utils.Lang 733 734**返回值:** 735 736| 类型 | 说明 | 737| ------ | ------------------------ | 738| string | 返回当前事件的文本内容。 | 739 740**示例:** 741 742```ts 743import util from '@ohos.util'; 744let strXml = 745 '<?xml version="1.0" encoding="utf-8"?>' + 746 '<note importance="high" logged="true">' + 747 ' <title>Happy</title>' + 748 ' <todo>Work</todo>' + 749 ' <todo>Play</todo>' + 750 '</note>'; 751let textEncoder = new util.TextEncoder(); 752let arrbuffer = textEncoder.encodeInto(strXml); 753let that = new xml.XmlPullParser(arrbuffer.buffer); 754let str = ""; 755function func(key: xml.EventType, value: xml.ParseInfo) { 756 str += ' key:' + key + ' value:' + value.getText() + ' '; 757 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 758} 759let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 760that.parse(options); 761console.log(str); 762// 输出: 763// 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: 764``` 765### isEmptyElementTag 766 767isEmptyElementTag(): boolean 768 769判断当前元素是否为空元素。 770 771**系统能力:** SystemCapability.Utils.Lang 772 773**返回值:** 774 775| 类型 | 说明 | 776| ------- | ---------------------------- | 777| boolean | 返回true,当前元素为空元素。 | 778 779**示例:** 780 781```ts 782import util from '@ohos.util'; 783let strXml = 784 '<?xml version="1.0" encoding="utf-8"?>' + 785 '<note importance="high" logged="true">' + 786 ' <title>Happy</title>' + 787 ' <todo>Work</todo>' + 788 ' <todo>Play</todo>' + 789 '</note>'; 790let textEncoder = new util.TextEncoder(); 791let arrbuffer = textEncoder.encodeInto(strXml); 792let that = new xml.XmlPullParser(arrbuffer.buffer); 793let str = ""; 794function func(key: xml.EventType, value: xml.ParseInfo) { 795 str += 'key:' + key + ' value:' + value.isEmptyElementTag() + ' '; 796 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 797} 798let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 799that.parse(options); 800console.log(str); 801// 输出: 802// 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 803``` 804### isWhitespace 805 806isWhitespace(): boolean 807 808判断当前文本事件是否仅包含空格字符。 809 810**系统能力:** SystemCapability.Utils.Lang 811 812**返回值:** 813 814| 类型 | 说明 | 815| ------- | -------------------------------------- | 816| boolean | 返回true,当前文本事件仅包含空格字符。 | 817 818**示例:** 819 820```ts 821import util from '@ohos.util'; 822let strXml = 823 '<?xml version="1.0" encoding="utf-8"?>' + 824 '<note importance="high" logged="true">' + 825 ' <title>Happy</title>' + 826 ' <todo>Work</todo>' + 827 ' <todo>Play</todo>' + 828 '</note>'; 829let textEncoder = new util.TextEncoder(); 830let arrbuffer = textEncoder.encodeInto(strXml); 831let that = new xml.XmlPullParser(arrbuffer.buffer); 832let str = ""; 833function func(key: xml.EventType, value: xml.ParseInfo) { 834 str += 'key:' + key + ' value:' + value.isWhitespace() + ' '; 835 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 836} 837let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 838that.parse(options); 839console.log(str); 840// 输出: 841// 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 842``` 843### getAttributeCount 844 845getAttributeCount(): number 846 847获取当前开始标记的属性数。 848 849**系统能力:** SystemCapability.Utils.Lang 850 851**返回值:** 852| 类型 | 说明 | 853| ------ | ---------------------- | 854| number | 当前开始标记的属性数。 | 855 856**示例:** 857 858```ts 859import util from '@ohos.util'; 860let strXml = 861 '<?xml version="1.0" encoding="utf-8"?>' + 862 '<note importance="high" logged="true">' + 863 ' <title>Happy</title>' + 864 ' <todo>Work</todo>' + 865 ' <todo>Play</todo>' + 866 '</note>'; 867let textEncoder = new util.TextEncoder(); 868let arrbuffer = textEncoder.encodeInto(strXml); 869let that = new xml.XmlPullParser(arrbuffer.buffer); 870let str = ""; 871function func(key: xml.EventType, value: xml.ParseInfo) { 872 str += 'key:' + key + ' value:' + value.getAttributeCount() + ' '; 873 return true; // Determines whether to continually parse, which is used to continue or terminate parsing. 874} 875let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 876that.parse(options); 877console.log(str); 878// 输出: 879// 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 880``` 881 882## EventType 883 884事件类型枚举。 885 886**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang 887 888| 名称 | 值 | 说明 | 889| ---------------- | ---- | --------------------- | 890| START_DOCUMENT | 0 | 启动文件事件。 | 891| END_DOCUMENT | 1 | 结束文件事件。 | 892| START_TAG | 2 | 启动标签事件。 | 893| END_TAG | 3 | 结束标签事件。 | 894| TEXT | 4 | 文本事件。 | 895| CDSECT | 5 | CDATA事件。 | 896| COMMENT | 6 | XML注释事件。 | 897| DOCDECL | 7 | XML文档类型声明事件。 | 898| INSTRUCTION | 8 | XML处理指令声明事件。 | 899| ENTITY_REFERENCE | 9 | 实体引用事件。 | 900| WHITESPACE | 10 | 空白事件。 |