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