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 | 否 | 编码格式。 | 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 arrayBuffer = new ArrayBuffer(strXml.length); 404let bufView = new Uint8Array(arrayBuffer); 405let strLen = strXml.length; 406for (let i = 0; i < strLen; ++i) { 407 bufView[i] = strXml.charCodeAt(i); 408} 409let that = new xml.XmlPullParser(arrayBuffer, 'UTF-8'); 410let str1 = ''; 411function func1(name, value){ 412 str1 += name+':'+value; 413 return true; 414} 415let options = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func1} 416that.parse(options); 417console.log(str1) //'note:company:title:title:lens:lens:a:b:h:table:h:tr:h:td:h:td:' 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```js 438let strXml = 439 '<?xml version="1.0" encoding="utf-8"?>' + 440 '<note importance="high" logged="true">' + 441 ' <title>Happy</title>' + 442 ' <todo>Work</todo>' + 443 ' <todo>Play</todo>' + 444 '</note>'; 445let arrayBuffer = new ArrayBuffer(strXml.length); 446let bufView = new Uint8Array(arrayBuffer); 447let strLen = strXml.length; 448for (let tmp = 0; tmp < strLen; ++tmp) { 449 bufView[tmp] = strXml.charCodeAt(tmp); 450} 451let that = new xml.XmlPullParser(arrayBuffer); 452let arrTag = {}; 453let str = ""; 454let i = 0; 455function func(key, value){ 456 arrTag[i] = 'key:'+key+' value:'+ value.getDepth(); 457 str += arrTag[i]; 458 i++; 459 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 460} 461let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 462that.parse(options); 463console.log(str); 464// 输出: 465// 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 466// 解析: 467// key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为: 468// 0(START_DOCUMENT):0(起始深度为0), 2(START_TAG):1(解析到开始标签node, 对应深度为1), 10(WHITESPACE):1(解析到空白标签空格, 对应深度为1), 2(START_TAG):2(解析到开始标签title, 对应深度为2), ... 469``` 470 471 472## ParseOptions 473 474xml解析选项。 475 476**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang 477 478 479| 名称 | 类型 | 必填 | 说明 | 480| ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- | 481| supportDoctype | boolean | 否 | 是否忽略Doctype , 默认false。 | 482| ignoreNameSpace | boolean | 否 | 是否忽略NameSpace,默认false。 | 483| tagValueCallbackFunction | (name: string, value: string) => boolean | 否 | 获取tagValue回调函数。 | 484| attributeValueCallbackFunction | (name: string, value: string) => boolean | 否 | 获取attributeValue回调函数。 | 485| tokenValueCallbackFunction | (eventType: [EventType](#eventtype), value: [ParseInfo](#parseinfo)) => boolean | 否 | 获取tokenValue回调函数。 | 486 487## ParseInfo 488 489当前xml解析信息。 490 491 492### getColumnNumber 493 494getColumnNumber(): number 495 496获取当前列号,从1开始。 497 498**系统能力:** SystemCapability.Utils.Lang 499 500**返回值:** 501 502| 类型 | 说明 | 503| ------ | -------------- | 504| number | 返回当前列号。 | 505 506**示例:** 507 508```js 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 arrayBuffer = new ArrayBuffer(strXml.length); 517let bufView = new Uint8Array(arrayBuffer); 518let strLen = strXml.length; 519for (let tmp = 0; tmp < strLen; ++tmp) { 520 bufView[tmp] = strXml.charCodeAt(tmp); 521} 522let that = new xml.XmlPullParser(arrayBuffer); 523let arrTag = {}; 524let str = ""; 525let i = 0; 526function func(key, value){ 527 arrTag[i] = 'key:'+key+' value:'+ value.getColumnNumber(); 528 str += arrTag[i]; 529 i++; 530 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 531} 532let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 533that.parse(options); 534console.log(str); 535// 输出: 536// 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 537``` 538 539### getDepth 540 541getDepth(): number 542 543获取元素的当前深度。 544 545**系统能力:** SystemCapability.Utils.Lang 546 547**返回值:** 548 549| 类型 | 说明 | 550| ------ | -------------------- | 551| number | 返回元素的当前深度。 | 552 553**示例:** 554 555```js 556let strXml = 557 '<?xml version="1.0" encoding="utf-8"?>' + 558 '<note importance="high" logged="true">' + 559 ' <title>Happy</title>' + 560 ' <todo>Work</todo>' + 561 ' <todo>Play</todo>' + 562 '</note>'; 563let arrayBuffer = new ArrayBuffer(strXml.length); 564let bufView = new Uint8Array(arrayBuffer); 565let strLen = strXml.length; 566for (let tmp = 0; tmp < strLen; ++tmp) { 567 bufView[tmp] = strXml.charCodeAt(tmp); 568} 569let that = new xml.XmlPullParser(arrayBuffer); 570let arrTag = {}; 571let str = ""; 572let i = 0; 573function func(key, value){ 574 arrTag[i] = 'key:'+key+' value:'+ value.getDepth(); 575 str += arrTag[i]; 576 i++; 577 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 578} 579let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 580that.parse(options); 581console.log(str); 582// 输出: 583// 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 584// 解析: 585// key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为: 586// 0(START_DOCUMENT):0(起始深度为0), 2(START_TAG):1(解析到开始标签node, 对应深度为1), 10(WHITESPACE):1(解析到空白标签空格, 对应深度为1), 2(START_TAG):2(解析到开始标签title, 对应深度为2), ... 587``` 588 589### getLineNumber 590 591getLineNumber(): number 592 593获取当前行号,从1开始。 594 595**系统能力:** SystemCapability.Utils.Lang 596 597**返回值:** 598 599| 类型 | 说明 | 600| ------ | -------------- | 601| number | 返回当前行号。 | 602 603**示例:** 604 605```js 606let strXml = 607 '<?xml version="1.0" encoding="utf-8"?>' + 608 '<note importance="high" logged="true">' + 609 ' <title>Happy</title>' + 610 ' <todo>Work</todo>' + 611 ' <todo>Play</todo>' + 612 '</note>'; 613let arrayBuffer = new ArrayBuffer(strXml.length); 614let bufView = new Uint8Array(arrayBuffer); 615let strLen = strXml.length; 616for (let tmp = 0; tmp < strLen; ++tmp) { 617 bufView[tmp] = strXml.charCodeAt(tmp); 618} 619let that = new xml.XmlPullParser(arrayBuffer); 620let arrTag = {}; 621let str = ""; 622let i = 0; 623function func(key, value){ 624 arrTag[i] = 'key:'+key+' value:'+ value.getLineNumber(); 625 str += arrTag[i]; 626 i++; 627 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 628} 629let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 630that.parse(options); 631console.log(str); 632// 输出: 633// 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 634``` 635 636### getName 637 638getName(): string 639 640获取当前元素名称。 641 642**系统能力:** SystemCapability.Utils.Lang 643 644**返回值:** 645 646| 类型 | 说明 | 647| ------ | ------------------ | 648| string | 返回当前元素名称。 | 649 650**示例:** 651 652```js 653let strXml = 654 '<?xml version="1.0" encoding="utf-8"?>' + 655 '<note importance="high" logged="true">' + 656 ' <title>Happy</title>' + 657 ' <todo>Work</todo>' + 658 ' <todo>Play</todo>' + 659 '</note>'; 660let arrayBuffer = new ArrayBuffer(strXml.length); 661let bufView = new Uint8Array(arrayBuffer); 662let strLen = strXml.length; 663for (let tmp = 0; tmp < strLen; ++tmp) { 664 bufView[tmp] = strXml.charCodeAt(tmp); 665} 666let that = new xml.XmlPullParser(arrayBuffer); 667let arrTag = {}; 668let str = ""; 669let i = 0; 670function func(key, value){ 671 arrTag[i] = 'key:'+key+' value:'+ value.getName(); 672 str += arrTag[i]; 673 i++; 674 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 675} 676let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 677that.parse(options); 678console.log(str); 679// 输出: 680// 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: 681``` 682### getNamespace 683 684getNamespace(): string 685 686获取当前元素的命名空间。 687 688**系统能力:** SystemCapability.Utils.Lang 689 690**返回值:** 691 692| 类型 | 说明 | 693| ------ | ------------------------ | 694| string | 返回当前元素的命名空间。 | 695 696**示例:** 697 698```js 699let strXml = 700 '<?xml version="1.0" encoding="utf-8"?>' + 701 '<note importance="high" logged="true">' + 702 ' <title>Happy</title>' + 703 ' <todo>Work</todo>' + 704 ' <todo>Play</todo>' + 705 '</note>'; 706let arrayBuffer = new ArrayBuffer(strXml.length); 707let bufView = new Uint8Array(arrayBuffer); 708let strLen = strXml.length; 709for (let tmp = 0; tmp < strLen; ++tmp) { 710 bufView[tmp] = strXml.charCodeAt(tmp); 711} 712let that = new xml.XmlPullParser(arrayBuffer); 713let arrTag = {}; 714let str = ""; 715let i = 0; 716function func(key, value){ 717 arrTag[i] = 'key:'+key+' value:'+ value.getNamespace(); 718 str += arrTag[i]; 719 i++; 720 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 721} 722let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 723that.parse(options); 724console.log(str); 725// 输出: 726// 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: 727``` 728### getPrefix 729 730getPrefix(): string 731 732获取当前元素前缀。 733 734**系统能力:** SystemCapability.Utils.Lang 735 736**返回值:** 737 738| 类型 | 说明 | 739| ------ | ------------------ | 740| string | 返回当前元素前缀。 | 741 742**示例:** 743 744```js 745let strXml = 746 '<?xml version="1.0" encoding="utf-8"?>' + 747 '<note importance="high" logged="true">' + 748 ' <title>Happy</title>' + 749 ' <todo>Work</todo>' + 750 ' <todo>Play</todo>' + 751 '</note>'; 752let arrayBuffer = new ArrayBuffer(strXml.length); 753let bufView = new Uint8Array(arrayBuffer); 754let strLen = strXml.length; 755for (let tmp = 0; tmp < strLen; ++tmp) { 756 bufView[tmp] = strXml.charCodeAt(tmp); 757} 758let that = new xml.XmlPullParser(arrayBuffer); 759let arrTag = {}; 760let str = ""; 761let i = 0; 762function func(key, value){ 763 arrTag[i] = 'key:'+key+' value:'+ value.getPrefix(); 764 str += arrTag[i]; 765 i++; 766 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 767} 768let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 769that.parse(options); 770console.log(str); 771// 输出: 772// 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: 773``` 774 775### getText 776 777getText(): string 778 779获取当前事件的文本内容。 780 781**系统能力:** SystemCapability.Utils.Lang 782 783**返回值:** 784 785| 类型 | 说明 | 786| ------ | ------------------------ | 787| string | 返回当前事件的文本内容。 | 788 789**示例:** 790 791```js 792let strXml = 793 '<?xml version="1.0" encoding="utf-8"?>' + 794 '<note importance="high" logged="true">' + 795 ' <title>Happy</title>' + 796 ' <todo>Work</todo>' + 797 ' <todo>Play</todo>' + 798 '</note>'; 799let arrayBuffer = new ArrayBuffer(strXml.length); 800let bufView = new Uint8Array(arrayBuffer); 801let strLen = strXml.length; 802for (let tmp = 0; tmp < strLen; ++tmp) { 803 bufView[tmp] = strXml.charCodeAt(tmp); 804} 805let that = new xml.XmlPullParser(arrayBuffer); 806let arrTag = {}; 807let str = ""; 808let i = 0; 809function func(key, value){ 810 arrTag[i] = 'key:'+key+' value:'+ value.getText(); 811 str += arrTag[i]; 812 i++; 813 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 814} 815let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 816that.parse(options); 817console.log(str); 818// 输出: 819// 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: 820``` 821### isEmptyElementTag 822 823isEmptyElementTag(): boolean 824 825判断当前元素是否为空元素。 826 827**系统能力:** SystemCapability.Utils.Lang 828 829**返回值:** 830 831| 类型 | 说明 | 832| ------- | ---------------------------- | 833| boolean | 返回true,当前元素为空元素。 | 834 835**示例:** 836 837```js 838let strXml = 839 '<?xml version="1.0" encoding="utf-8"?>' + 840 '<note importance="high" logged="true">' + 841 ' <title>Happy</title>' + 842 ' <todo>Work</todo>' + 843 ' <todo>Play</todo>' + 844 '</note>'; 845let arrayBuffer = new ArrayBuffer(strXml.length); 846let bufView = new Uint8Array(arrayBuffer); 847let strLen = strXml.length; 848for (let tmp = 0; tmp < strLen; ++tmp) { 849 bufView[tmp] = strXml.charCodeAt(tmp); 850} 851let that = new xml.XmlPullParser(arrayBuffer); 852let arrTag = {}; 853let str = ""; 854let i = 0; 855function func(key, value){ 856 arrTag[i] = 'key:'+key+' value:'+ value.isEmptyElementTag(); 857 str += arrTag[i]; 858 i++; 859 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 860} 861let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 862that.parse(options); 863console.log(str); 864// 输出: 865// 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 866``` 867### isWhitespace 868 869isWhitespace(): boolean 870 871判断当前文本事件是否仅包含空格字符。 872 873**系统能力:** SystemCapability.Utils.Lang 874 875**返回值:** 876 877| 类型 | 说明 | 878| ------- | -------------------------------------- | 879| boolean | 返回true,当前文本事件仅包含空格字符。 | 880 881**示例:** 882 883```js 884let strXml = 885 '<?xml version="1.0" encoding="utf-8"?>' + 886 '<note importance="high" logged="true">' + 887 ' <title>Happy</title>' + 888 ' <todo>Work</todo>' + 889 ' <todo>Play</todo>' + 890 '</note>'; 891let arrayBuffer = new ArrayBuffer(strXml.length); 892let bufView = new Uint8Array(arrayBuffer); 893let strLen = strXml.length; 894for (let tmp = 0; tmp < strLen; ++tmp) { 895 bufView[tmp] = strXml.charCodeAt(tmp); 896} 897let that = new xml.XmlPullParser(arrayBuffer); 898let arrTag = {}; 899let str = ""; 900let i = 0; 901function func(key, value){ 902 arrTag[i] = 'key:'+key+' value:'+ value.isWhitespace(); 903 str += arrTag[i]; 904 i++; 905 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 906} 907let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 908that.parse(options); 909console.log(str); 910// 输出: 911// 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 912``` 913### getAttributeCount 914 915getAttributeCount(): number 916 917获取当前开始标记的属性数。 918 919**系统能力:** SystemCapability.Utils.Lang 920 921**返回值:** 922| 类型 | 说明 | 923| ------ | ---------------------- | 924| number | 当前开始标记的属性数。 | 925 926**示例:** 927 928```js 929let strXml = 930 '<?xml version="1.0" encoding="utf-8"?>' + 931 '<note importance="high" logged="true">' + 932 ' <title>Happy</title>' + 933 ' <todo>Work</todo>' + 934 ' <todo>Play</todo>' + 935 '</note>'; 936let arrayBuffer = new ArrayBuffer(strXml.length); 937let bufView = new Uint8Array(arrayBuffer); 938let strLen = strXml.length; 939for (let tmp = 0; tmp < strLen; ++tmp) { 940 bufView[tmp] = strXml.charCodeAt(tmp); 941} 942let that = new xml.XmlPullParser(arrayBuffer); 943let arrTag = {}; 944let str = ""; 945let i = 0; 946function func(key, value){ 947 arrTag[i] = 'key:'+key+' value:'+ value.getAttributeCount(); 948 str += arrTag[i]; 949 i++; 950 return true; // Determines whether to continuely parse, which is used to continue or terminate parsing. 951} 952let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} 953that.parse(options); 954console.log(str); 955// 输出: 956// 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 957``` 958 959## EventType 960 961事件枚举。 962 963**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang 964 965| 名称 | 值 | 说明 | 966| ---------------- | ---- | --------------------- | 967| START_DOCUMENT | 0 | 启动文件事件。 | 968| END_DOCUMENT | 1 | 结束文件事件。 | 969| START_TAG | 2 | 启动标签事件。 | 970| END_TAG | 3 | 结束标签事件。 | 971| TEXT | 4 | 文本事件。 | 972| CDSECT | 5 | CDATA事件。 | 973| COMMENT | 6 | XML注释事件。 | 974| DOCDECL | 7 | XML文档类型声明事件。 | 975| INSTRUCTION | 8 | XML处理指令声明事件。 | 976| ENTITY_REFERENCE | 9 | 实体引用事件。 | 977| WHITESPACE | 10 | 空白事件。 |