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