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