• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.xml (XML Parsing and Generation)
2
3The xml module provides APIs for generating and parsing XML files.
4
5The module offers two methods for generating XML files:
6* [XmlSerializer](#xmlserializer): suitable for scenarios where the size of the XML text is known in advance.
7* [XmlDynamicSerializer<sup>20+</sup>](#xmldynamicserializer20): suitable for scenarios where the size of the XML text is not known in advance.
8
9> **NOTE**
10>
11> 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.
12
13
14## Modules to Import
15
16```
17import { xml } from '@kit.ArkTS';
18```
19
20## XmlSerializer
21
22**XmlSerializer** provides APIs to generate an XML file.
23
24### constructor
25
26constructor(buffer: ArrayBuffer | DataView, encoding?: string)
27
28A constructor used to create an **XmlSerializer** instance.
29
30> **NOTE**
31>
32> The buffer is used to temporarily store XML text generated. Its size can be customized. Ensure that the buffer is large enough to hold the generated text.
33
34**Atomic service API**: This API can be used in atomic services since API version 11.
35
36**System capability**: SystemCapability.Utils.Lang
37
38**Parameters**
39
40| Name  | Type                             | Mandatory| Description                                            |
41| -------- | --------------------------------- | ---- | ------------------------------------------------ |
42| buffer   | ArrayBuffer \| DataView | Yes  | ArrayBuffer or DataView for storing the XML information to set.|
43| encoding | string                            | No  | Encoding format. The default value is **'utf-8'** (the only format currently supported).              |
44
45**Error codes**
46
47For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
48
49| ID| Error Message|
50| -------- | -------- |
51| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
52
53**Example**
54
55```ts
56let arrayBuffer = new ArrayBuffer(2048);
57let thatSer = new xml.XmlSerializer(arrayBuffer, "utf-8");
58```
59
60### setAttributes
61
62setAttributes(name: string, value: string): void
63
64Sets an attribute.
65
66> **NOTE**
67>
68> This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add an attribute name starting with a digit or add multiple attributes with the same name.
69
70**Atomic service API**: This API can be used in atomic services since API version 11.
71
72**System capability**: SystemCapability.Utils.Lang
73
74**Parameters**
75
76| Name| Type  | Mandatory| Description           |
77| ------ | ------ | ---- | --------------- |
78| name   | string | Yes  | Key of the attribute.  |
79| value  | string | Yes  | Value of the attribute.|
80
81**Error codes**
82
83For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
84
85| ID| Error Message|
86| -------- | -------- |
87| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
88
89**Example**
90
91```ts
92import { util } from '@kit.ArkTS';
93
94let arrayBuffer = new ArrayBuffer(2048);
95let thatSer = new xml.XmlSerializer(arrayBuffer);
96thatSer.startElement("note");
97thatSer.setAttributes("importance", "high");
98thatSer.endElement();
99let uint8 = new Uint8Array(arrayBuffer);
100let result = util.TextDecoder.create().decodeToString(uint8);
101console.log(result); // <note importance="high"/>
102```
103
104### addEmptyElement
105
106addEmptyElement(name: string): void
107
108Adds an empty element.
109
110> **NOTE**
111>
112> This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add an attribute name starting with a digit.
113
114**Atomic service API**: This API can be used in atomic services since API version 11.
115
116**System capability**: SystemCapability.Utils.Lang
117
118**Parameters**
119
120| Name| Type  | Mandatory| Description              |
121| ------ | ------ | ---- | ------------------ |
122| name   | string | Yes  | Name of the element.|
123
124**Error codes**
125
126For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
127
128| ID| Error Message|
129| -------- | -------- |
130| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
131
132**Example**
133
134```ts
135import { util } from '@kit.ArkTS';
136
137let arrayBuffer = new ArrayBuffer(2048);
138let thatSer = new xml.XmlSerializer(arrayBuffer);
139thatSer.addEmptyElement("d");
140let uint8 = new Uint8Array(arrayBuffer);
141let result = util.TextDecoder.create().decodeToString(uint8);
142console.log(result); // <d/>
143```
144
145### setDeclaration
146
147setDeclaration(): void
148
149Sets a file declaration with encoding.
150
151**Atomic service API**: This API can be used in atomic services since API version 11.
152
153**System capability**: SystemCapability.Utils.Lang
154
155**Example**
156
157```ts
158import { util } from '@kit.ArkTS';
159
160let arrayBuffer = new ArrayBuffer(2048);
161let thatSer = new xml.XmlSerializer(arrayBuffer);
162thatSer.setDeclaration();
163let uint8 = new Uint8Array(arrayBuffer);
164let result = util.TextDecoder.create().decodeToString(uint8);
165console.log(result);
166// <?xml version="1.0" encoding="utf-8"?>
167```
168
169### startElement
170
171startElement(name: string): void
172
173Adds the start tag based on the given element name.
174
175> **NOTE**
176>
177>- After calling this API, you must call [endElement](#endelement) to write the end flag to ensure that the node is closed correctly.
178>
179>- This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add an attribute name starting with a digit.
180
181**Atomic service API**: This API can be used in atomic services since API version 11.
182
183**System capability**: SystemCapability.Utils.Lang
184
185**Parameters**
186
187| Name| Type  | Mandatory| Description              |
188| ------ | ------ | ---- | ------------------ |
189| name   | string | Yes  | Name of the element.|
190
191**Error codes**
192
193For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
194
195| ID| Error Message|
196| -------- | -------- |
197| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
198
199**Example**
200
201```ts
202import { util } from '@kit.ArkTS';
203
204let arrayBuffer = new ArrayBuffer(2048);
205let thatSer = new xml.XmlSerializer(arrayBuffer);
206thatSer.startElement("note");
207thatSer.setText("Happy");
208thatSer.endElement();
209let uint8 = new Uint8Array(arrayBuffer);
210let result = util.TextDecoder.create().decodeToString(uint8);
211console.log(result);
212// <note>Happy</note>
213```
214
215### endElement
216
217endElement(): void
218
219Adds the end tag of the element.
220
221> **NOTE**
222>
223> Before calling this API, you must call [startElement](#startelement) to write the start flag.
224
225**Atomic service API**: This API can be used in atomic services since API version 11.
226
227**System capability**: SystemCapability.Utils.Lang
228
229**Example**
230
231```ts
232import { util } from '@kit.ArkTS';
233
234let arrayBuffer = new ArrayBuffer(2048);
235let thatSer = new xml.XmlSerializer(arrayBuffer);
236thatSer.startElement("note");
237thatSer.setText("Happy");
238thatSer.endElement();
239let uint8 = new Uint8Array(arrayBuffer);
240let result = util.TextDecoder.create().decodeToString(uint8);
241console.log(result);
242// <note>Happy</note>
243```
244
245### setNamespace
246
247setNamespace(prefix: string, namespace: string): void
248
249Sets the namespace for an element tag.
250
251> **NOTE**
252>
253> This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add a namespace starting with a digit or set multiple namespaces for the same element.
254
255**Atomic service API**: This API can be used in atomic services since API version 11.
256
257**System capability**: SystemCapability.Utils.Lang
258
259**Parameters**
260
261| Name   | Type  | Mandatory| Description                          |
262| --------- | ------ | ---- | ------------------------------ |
263| prefix    | string | Yes  | Prefix of the element and its child elements.    |
264| namespace | string | Yes  | Namespace to set.|
265
266**Error codes**
267
268For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
269
270| ID| Error Message|
271| -------- | -------- |
272| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
273
274**Example**
275
276```ts
277import { util } from '@kit.ArkTS';
278
279let arrayBuffer = new ArrayBuffer(2048);
280let thatSer = new xml.XmlSerializer(arrayBuffer);
281thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
282thatSer.startElement("note");
283thatSer.endElement();
284let uint8 = new Uint8Array(arrayBuffer);
285let result = util.TextDecoder.create().decodeToString(uint8);
286console.log(result);
287// <h:note xmlns:h="http://www.w3.org/TR/html4/"/>
288```
289
290### setComment
291
292setComment(text: string): void
293
294Sets a comment.
295
296**Atomic service API**: This API can be used in atomic services since API version 11.
297
298**System capability**: SystemCapability.Utils.Lang
299
300**Parameters**
301
302| Name| Type  | Mandatory| Description                |
303| ------ | ------ | ---- | -------------------- |
304| text   | string | Yes  | Comment to set.|
305
306**Error codes**
307
308For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
309
310| ID| Error Message|
311| -------- | -------- |
312| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
313
314**Example**
315
316```ts
317import { util } from '@kit.ArkTS';
318
319let arrayBuffer = new ArrayBuffer(2048);
320let thatSer = new xml.XmlSerializer(arrayBuffer);
321thatSer.setComment("Hello, World!");
322let uint8 = new Uint8Array(arrayBuffer);
323let result = util.TextDecoder.create().decodeToString(uint8);
324console.log(result); // <!--Hello, World!-->
325```
326
327### setCDATA
328
329setCDATA(text: string): void
330
331Adds data to the CDATA tag. The structure of the generated CDATA tag is "\<! <![CDATA\["+ Data added + "\]\]\>".
332
333> **NOTE**
334>
335> This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add data that contains the string \]\]\> to the CDATA tag.
336
337**Atomic service API**: This API can be used in atomic services since API version 11.
338
339**System capability**: SystemCapability.Utils.Lang
340
341**Parameters**
342
343| Name| Type  | Mandatory| Description             |
344| ------ | ------ | ---- | ----------------- |
345| text   | string | Yes  | CDATA data to set.|
346
347**Error codes**
348
349For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
350
351| ID| Error Message|
352| -------- | -------- |
353| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
354
355**Example**
356
357```ts
358import { util } from '@kit.ArkTS';
359
360let arrayBuffer = new ArrayBuffer(2048);
361let thatSer = new xml.XmlSerializer(arrayBuffer);
362thatSer.setCDATA('root SYSTEM')
363let uint8 = new Uint8Array(arrayBuffer);
364let result = util.TextDecoder.create().decodeToString(uint8);
365console.log(result); // <![CDATA[root SYSTEM]]>
366```
367
368### setText
369
370setText(text: string): void
371
372Sets a tag value.
373
374**Atomic service API**: This API can be used in atomic services since API version 11.
375
376**System capability**: SystemCapability.Utils.Lang
377
378**Parameters**
379
380| Name| Type  | Mandatory| Description            |
381| ------ | ------ | ---- | ---------------- |
382| text   | string | Yes  | Tag value to set, which is the content of the **text** attribute.|
383
384**Error codes**
385
386For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
387
388| ID| Error Message|
389| -------- | -------- |
390| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
391
392**Example**
393
394```ts
395import { util } from '@kit.ArkTS';
396
397let arrayBuffer = new ArrayBuffer(2048);
398let thatSer = new xml.XmlSerializer(arrayBuffer);
399thatSer.startElement("note");
400thatSer.setAttributes("importance", "high");
401thatSer.setText("Happy");
402thatSer.endElement();
403let uint8 = new Uint8Array(arrayBuffer);
404let result = util.TextDecoder.create().decodeToString(uint8);
405console.log(result); // <note importance="high">Happy</note>
406```
407
408### setDocType
409
410setDocType(text: string): void
411
412Sets a document type.
413
414**Atomic service API**: This API can be used in atomic services since API version 11.
415
416**System capability**: SystemCapability.Utils.Lang
417
418**Parameters**
419
420| Name| Type  | Mandatory| Description               |
421| ------ | ------ | ---- | ------------------- |
422| text   | string | Yes  | Content of **DocType** to set.|
423
424**Error codes**
425
426For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
427
428| ID| Error Message|
429| -------- | -------- |
430| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
431
432**Example**
433
434```ts
435import { util } from '@kit.ArkTS';
436
437let arrayBuffer = new ArrayBuffer(2048);
438let thatSer = new xml.XmlSerializer(arrayBuffer);
439thatSer.setDocType('root SYSTEM "http://www.test.org/test.dtd"');
440let uint8 = new Uint8Array(arrayBuffer);
441let result = util.TextDecoder.create().decodeToString(uint8);
442console.log(result); // <!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">
443```
444
445## XmlDynamicSerializer<sup>20+</sup>
446
447The **XmlDynamicSerializer** class is used to generate XML strings. It is recommended when the length of the XML content cannot be determined in advance.
448
449> **NOTE**
450>
451> Objects constructed from this class do not require manual creation of an ArrayBuffer. You can continuously add XML elements, and the upper limit for the length of the final serialized string result is 100,000 characters.
452
453### constructor<sup>20+</sup>
454
455constructor(encoding?: string)
456
457A constructor used to create an **XmlDynamicSerializer** instance.
458
459**Atomic service API**: This API can be used in atomic services since API version 20.
460
461**System capability**: SystemCapability.Utils.Lang
462
463**Parameters**
464
465| Name  | Type                             | Mandatory| Description                                            |
466| -------- | --------------------------------- | ---- | ------------------------------------------------ |
467| encoding | string                            | No  | Encoding format. The default value is **'utf-8'** (the only format currently supported).         |
468
469**Error codes**
470
471For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
472
473| ID| Error Message|
474| -------- | -------- |
475| 10200066 | Incorrect encoding format, only support utf-8. |
476
477**Example**
478
479```ts
480let serializer = new xml.XmlDynamicSerializer('utf-8');
481```
482
483### getOutput<sup>20+</sup>
484
485getOutput(): ArrayBuffer
486
487Obtains the ArrayBuffer of the XML string.
488
489**Atomic service API**: This API can be used in atomic services since API version 20.
490
491**System capability**: SystemCapability.Utils.Lang
492
493**Return value**
494
495| Type  | Description                |
496| ------ | -------------------- |
497| ArrayBuffer | ArrayBuffer for storing the XML information to set.|
498
499**Example**
500
501```ts
502import { util } from '@kit.ArkTS';
503
504let serializer = new xml.XmlDynamicSerializer('utf-8');
505serializer.startElement("note");
506serializer.setText("Happy");
507serializer.endElement();
508let arr = serializer.getOutput();
509let uint8 = new Uint8Array(arr);
510let result = util.TextDecoder.create().decodeToString(uint8);
511console.info(result); // <note>Happy</note>
512```
513
514### setAttributes<sup>20+</sup>
515
516setAttributes(name: string, value: string): void
517
518Sets an attribute.
519
520> **NOTE**
521>
522> This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add an attribute name starting with a digit or add multiple attributes with the same name.
523
524**Atomic service API**: This API can be used in atomic services since API version 20.
525
526**System capability**: SystemCapability.Utils.Lang
527
528**Parameters**
529
530| Name| Type  | Mandatory| Description           |
531| ------ | ------ | ---- | --------------- |
532| name   | string | Yes  | Key of the attribute. The total length of the XML cannot exceed 100,000 characters.|
533| value  | string | Yes  | Value of the attribute. The total length of the XML cannot exceed 100,000 characters.|
534
535**Error codes**
536
537For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
538
539| ID| Error Message|
540| -------- | -------- |
541| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
542| 10200063 | Illegal position for xml. |
543| 10200064 | Cannot be an empty string. |
544
545**Example**
546
547```ts
548import { util } from '@kit.ArkTS';
549
550let serializer = new xml.XmlDynamicSerializer('utf-8');
551serializer.startElement("note");
552serializer.setAttributes("importance", "high");
553serializer.endElement();
554let arrayBuffer = serializer.getOutput();
555let uint8 = new Uint8Array(arrayBuffer);
556let result = util.TextDecoder.create().decodeToString(uint8);
557console.info(result); // <note importance="high"/>
558```
559
560### addEmptyElement<sup>20+</sup>
561
562addEmptyElement(name: string): void
563
564Adds an empty element.
565
566> **NOTE**
567>
568> This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add an attribute name starting with a digit.
569
570**Atomic service API**: This API can be used in atomic services since API version 20.
571
572**System capability**: SystemCapability.Utils.Lang
573
574**Parameters**
575
576| Name| Type  | Mandatory| Description              |
577| ------ | ------ | ---- | ------------------ |
578| name   | string | Yes  | Name of the empty element. The total length of the XML cannot exceed 100,000 characters.|
579
580**Error codes**
581
582For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
583
584| ID| Error Message|
585| -------- | -------- |
586| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
587| 10200064 | Cannot be an empty string. |
588
589**Example**
590
591```ts
592import { util } from '@kit.ArkTS';
593
594let serializer = new xml.XmlDynamicSerializer('utf-8');
595serializer.addEmptyElement("d");
596let arrayBuffer = serializer.getOutput();
597let uint8 = new Uint8Array(arrayBuffer);
598let result = util.TextDecoder.create().decodeToString(uint8);
599console.info(result); // <d/>
600```
601
602### setDeclaration<sup>20+</sup>
603
604setDeclaration(): void
605
606Sets a file declaration with encoding.
607
608**Atomic service API**: This API can be used in atomic services since API version 20.
609
610**System capability**: SystemCapability.Utils.Lang
611
612**Error codes**
613
614For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
615
616| ID| Error Message|
617| -------- | -------- |
618| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
619| 10200063 | Illegal position for xml. |
620
621**Example**
622
623```ts
624import { util } from '@kit.ArkTS';
625
626let serializer = new xml.XmlDynamicSerializer('utf-8');
627serializer.setDeclaration();
628let arrayBuffer = serializer.getOutput();
629let uint8 = new Uint8Array(arrayBuffer);
630let result = util.TextDecoder.create().decodeToString(uint8);
631console.info(result); // <?xml version="1.0" encoding="utf-8"?>
632```
633
634### startElement<sup>20+</sup>
635
636startElement(name: string): void
637
638Writes the start tag of the element.
639
640> **NOTE**
641>
642>- After calling this API, you must call [endElement](#endelement) to write the end flag to ensure that the node is closed correctly.
643>
644>- This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add an attribute name starting with a digit.
645
646**Atomic service API**: This API can be used in atomic services since API version 20.
647
648**System capability**: SystemCapability.Utils.Lang
649
650**Parameters**
651
652| Name| Type  | Mandatory| Description              |
653| ------ | ------ | ---- | ------------------ |
654| name   | string | Yes  | Name of the element. The total length of the XML cannot exceed 100,000 characters.|
655
656**Error codes**
657
658For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
659
660| ID| Error Message|
661| -------- | -------- |
662| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
663| 10200064 | Cannot be an empty string. |
664
665**Example**
666
667```ts
668import { util } from '@kit.ArkTS';
669
670let serializer = new xml.XmlDynamicSerializer('utf-8');
671serializer.startElement("note");
672serializer.setText("Happy");
673serializer.endElement();
674let arrayBuffer = serializer.getOutput();
675let uint8 = new Uint8Array(arrayBuffer);
676let result = util.TextDecoder.create().decodeToString(uint8);
677console.info(result); // <note>Happy</note>
678```
679
680### endElement<sup>20+</sup>
681
682endElement(): void
683
684Writes the end tag of the element.
685
686> **NOTE**
687>
688> Before calling this API, you must call [startElement](#startelement) to write the start flag.
689
690**Atomic service API**: This API can be used in atomic services since API version 20.
691
692**System capability**: SystemCapability.Utils.Lang
693
694**Error codes**
695
696For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
697
698| ID| Error Message|
699| -------- | -------- |
700| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
701| 10200065 | There is no match between the startElement and the endElement. |
702
703**Example**
704
705```ts
706import { util } from '@kit.ArkTS';
707
708let serializer = new xml.XmlDynamicSerializer('utf-8');
709serializer.startElement("note");
710serializer.setText("Happy");
711serializer.endElement();
712let arrayBuffer = serializer.getOutput();
713let uint8 = new Uint8Array(arrayBuffer);
714let result = util.TextDecoder.create().decodeToString(uint8);
715console.info(result); // <note>Happy</note>
716```
717
718### setNamespace<sup>20+</sup>
719
720setNamespace(prefix: string, namespace: string): void
721
722Sets the namespace for an element tag.
723
724> **NOTE**
725>
726> This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add a namespace starting with a digit or set multiple namespaces for the same element.
727
728**Atomic service API**: This API can be used in atomic services since API version 20.
729
730**System capability**: SystemCapability.Utils.Lang
731
732**Parameters**
733
734| Name   | Type  | Mandatory| Description                          |
735| --------- | ------ | ---- | ------------------------------ |
736| prefix    | string | Yes  | Prefix of the element and its child elements. The total length of the XML cannot exceed 100,000 characters.|
737| namespace | string | Yes  | Namespace to set. The total length of the XML cannot exceed 100,000 characters.|
738
739**Error codes**
740
741For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
742
743| ID| Error Message|
744| -------- | -------- |
745| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
746| 10200064 | Cannot be an empty string. |
747
748**Example**
749
750```ts
751import { util } from '@kit.ArkTS';
752
753let serializer = new xml.XmlDynamicSerializer('utf-8');
754serializer.setNamespace("h", "http://www.w3.org/TR/html4/");
755serializer.startElement("note");
756serializer.endElement();
757let arrayBuffer = serializer.getOutput();
758let uint8 = new Uint8Array(arrayBuffer);
759let result = util.TextDecoder.create().decodeToString(uint8);
760console.info(result); // <h:note xmlns:h="http://www.w3.org/TR/html4/"/>
761```
762
763### setComment<sup>20+</sup>
764
765setComment(text: string): void
766
767Sets a comment.
768
769**Atomic service API**: This API can be used in atomic services since API version 20.
770
771**System capability**: SystemCapability.Utils.Lang
772
773**Parameters**
774
775| Name| Type  | Mandatory| Description                |
776| ------ | ------ | ---- | -------------------- |
777| text   | string | Yes  | Comment to set. The total length of the XML cannot exceed 100,000 characters.|
778
779**Error codes**
780
781For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
782
783| ID| Error Message|
784| -------- | -------- |
785| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
786| 10200064 | Cannot be an empty string. |
787
788**Example**
789
790```ts
791import { util } from '@kit.ArkTS';
792
793let serializer = new xml.XmlDynamicSerializer('utf-8');
794serializer.setComment("Hello, World!");
795let arrayBuffer = serializer.getOutput();
796let uint8 = new Uint8Array(arrayBuffer);
797let result = util.TextDecoder.create().decodeToString(uint8);
798console.info(result); // <!--Hello, World!-->
799```
800
801### setCdata<sup>20+</sup>
802
803setCdata(text: string): void
804
805Adds data to the CDATA tag. The structure of the generated CDATA tag is "\<! <![CDATA\["+ Data added + "\]\]\>".
806
807> **NOTE**
808>
809> This API does not perform standard XML verification on the data to add. Ensure that the data complies with the XML specifications. For example, as stipulated in the specifications, you are not allowed to add data that contains the string \]\]\> to the CDATA tag.
810
811**Atomic service API**: This API can be used in atomic services since API version 20.
812
813**System capability**: SystemCapability.Utils.Lang
814
815**Parameters**
816
817| Name| Type  | Mandatory| Description             |
818| ------ | ------ | ---- | ----------------- |
819| text   | string | Yes  | CDATA data to set. The total length of the XML cannot exceed 100,000 characters.|
820
821**Error codes**
822
823For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
824
825| ID| Error Message|
826| -------- | -------- |
827| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
828| 10200064 | Cannot be an empty string. |
829
830**Example**
831
832```ts
833import { util } from '@kit.ArkTS';
834
835let serializer = new xml.XmlDynamicSerializer('utf-8');
836serializer.setCdata('root SYSTEM')
837let arrayBuffer = serializer.getOutput();
838let uint8 = new Uint8Array(arrayBuffer);
839let result = util.TextDecoder.create().decodeToString(uint8);
840console.info(result); // <![CDATA[root SYSTEM]]>
841```
842
843### setText<sup>20+</sup>
844
845setText(text: string): void
846
847Sets a tag value.
848
849**Atomic service API**: This API can be used in atomic services since API version 20.
850
851**System capability**: SystemCapability.Utils.Lang
852
853**Parameters**
854
855| Name| Type  | Mandatory| Description            |
856| ------ | ------ | ---- | ---------------- |
857| text   | string | Yes  | Tag value. The total length of the XML cannot exceed 100,000 characters.|
858
859**Error codes**
860
861For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
862
863| ID| Error Message|
864| -------- | -------- |
865| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
866| 10200064 | Cannot be an empty string. |
867
868**Example**
869
870```ts
871import { util } from '@kit.ArkTS';
872
873let serializer = new xml.XmlDynamicSerializer('utf-8');
874serializer.startElement("note");
875serializer.setAttributes("importance", "high");
876serializer.setText("Happy");
877serializer.endElement();
878let arrayBuffer = serializer.getOutput();
879let uint8 = new Uint8Array(arrayBuffer);
880let result = util.TextDecoder.create().decodeToString(uint8);
881console.info(result); // <note importance="high">Happy</note>
882```
883
884### setDocType<sup>20+</sup>
885
886setDocType(text: string): void
887
888Sets a document type.
889
890**Atomic service API**: This API can be used in atomic services since API version 20.
891
892**System capability**: SystemCapability.Utils.Lang
893
894**Parameters**
895
896| Name| Type  | Mandatory| Description               |
897| ------ | ------ | ---- | ------------------- |
898| text   | string | Yes  | Content of **DocType** to set. The total length of the XML cannot exceed 100,000 characters.|
899
900**Error codes**
901
902For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
903
904| ID| Error Message|
905| -------- | -------- |
906| 10200062 | The cumulative length of xml has exceeded the upper limit 100000. |
907| 10200064 | Cannot be an empty string. |
908
909**Example**
910
911```ts
912import { util } from '@kit.ArkTS';
913
914let serializer = new xml.XmlDynamicSerializer('utf-8');
915serializer.setDocType('root SYSTEM "http://www.test.org/test.dtd"');
916let arrayBuffer = serializer.getOutput();
917let uint8 = new Uint8Array(arrayBuffer);
918let result = util.TextDecoder.create().decodeToString(uint8);
919console.info(result); // <!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">
920```
921
922## XmlPullParser
923
924Implements XML file parsing.
925
926### constructor
927
928constructor(buffer: ArrayBuffer | DataView, encoding?: string)
929
930Creates and returns an **XmlPullParser** object.
931
932**Atomic service API**: This API can be used in atomic services since API version 11.
933
934**System capability**: SystemCapability.Utils.Lang
935
936**Parameters**
937
938| Name  | Type                             | Mandatory| Description                                      |
939| -------- | --------------------------------- | ---- | ------------------------------------------ |
940| buffer   | ArrayBuffer \| DataView | Yes  | XML text information to be parsed.|
941| encoding | string                            | No  | Encoding format. The default value is **'utf-8'** (the only format currently supported).        |
942
943**Error codes**
944
945For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
946
947| ID| Error Message|
948| -------- | -------- |
949| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
950
951**Example**
952
953```ts
954import { util } from '@kit.ArkTS';
955
956let strXml = '<title>Happy</title>'
957let textEncoder = new util.TextEncoder();
958let uint8Array = textEncoder.encodeInto(strXml);
959let that = new xml.XmlPullParser(uint8Array.buffer as object as ArrayBuffer, 'UTF-8');
960```
961
962### parseXml<sup>14+</sup>
963
964parseXml(option: ParseOptions): void
965
966Parses XML information.
967
968**Atomic service API**: This API can be used in atomic services since API version 14.
969
970**System capability**: SystemCapability.Utils.Lang
971
972**Parameters**
973
974| Name| Type                         | Mandatory| Description         |
975| ------ | ----------------------------- | ---- | ------------- |
976| option | [ParseOptions](#parseoptions) | Yes  | XML parsing options.|
977
978**Error codes**
979
980For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
981
982| ID| Error Message|
983| -------- | -------- |
984| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
985
986**Example**
987
988```ts
989import { xml, util } from '@kit.ArkTS';
990
991let strxml =
992  '<?xml version="1.0" encoding="utf-8"?>' +
993    '<note importance="high" logged="true">' +
994    '    <title><![CDATA[Test\nTest]]></title>' +
995    '</note>';
996let textEncoder = new util.TextEncoder();
997let uint8 = textEncoder.encodeInto(strxml);
998
999function func(key: xml.EventType, value: xml.ParseInfo) {
1000  if (key == xml.EventType.CDSECT) {
1001    console.log(JSON.stringify(value.getText()));
1002  }
1003  return true;
1004}
1005let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
1006let pullParser = new xml.XmlPullParser(uint8.buffer as object as ArrayBuffer);
1007pullParser.parseXml(options);
1008// "Test\nTest"
1009```
1010
1011### parse<sup>(deprecated)</sup>
1012
1013parse(option: ParseOptions): void
1014
1015Parses XML information.
1016
1017> **NOTE**
1018>
1019> This API is supported since API version 8 and deprecated since API version 14. You are advised to use [parseXml<sup>14+</sup>](#parsexml14) instead.
1020
1021**Atomic service API**: This API can be used in atomic services since API version 11.
1022
1023**System capability**: SystemCapability.Utils.Lang
1024
1025**Parameters**
1026
1027| Name| Type                         | Mandatory| Description                            |
1028| ------ | ----------------------------- | ---- | -------------------------------- |
1029| option | [ParseOptions](#parseoptions) | Yes  | XML parsing options.|
1030
1031**Error codes**
1032
1033For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
1034
1035| ID| Error Message|
1036| -------- | -------- |
1037| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1038
1039**Example**
1040
1041```ts
1042import { util } from '@kit.ArkTS';
1043
1044let strXml =
1045  '<?xml version="1.0" encoding="utf-8"?>' +
1046  '<note importance="high" logged="true">' +
1047    '<company>John &amp; Hans</company>' +
1048    '<title>Happy</title>' +
1049  '</note>';
1050let textEncoder = new util.TextEncoder();
1051let arrbuffer = textEncoder.encodeInto(strXml);
1052let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer, 'UTF-8');
1053let str = '';
1054function func(name: string, value: string) {
1055  str = name + value;
1056  console.log(str);
1057  return true;
1058}
1059let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func}
1060that.parse(options);
1061// note
1062// company
1063// John & Hans
1064// company
1065// title
1066// Happy
1067// title
1068// note
1069```
1070
1071## ParseOptions
1072
1073Defines the XML parsing options.
1074
1075**Atomic service API**: This API can be used in atomic services since API version 11.
1076
1077**System capability**: SystemCapability.Utils.Lang
1078
1079
1080| Name                          | Type                                                        | Mandatory| Description                                   |
1081| ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- |
1082| supportDoctype                 | boolean                                                      | No  | Whether to parse the document type. The value **true** means to parse the document type, and **false** means the opposite. The default value is **false**.|
1083| ignoreNameSpace                | boolean                                                      | No  | Whether to ignore the namespace. If the namespace is ignored, it will not be parsed. The value **true** means to ignore the namespace, and **false** means the opposite. The default value is **false**.|
1084| tagValueCallbackFunction       | (name: string, value: string) =&gt; boolean | No  | Start tag, tag value, and end tag of parsing. The default value is **undefined**, indicating no parsing.|
1085| attributeValueCallbackFunction | (name: string, value: string) =&gt; boolean | No  | Parsing attribute and attribute value. The default value is **undefined**, indicating no parsing.|
1086| tokenValueCallbackFunction     | (eventType: [EventType](#eventtype), value: [ParseInfo](#parseinfo)) =&gt; boolean | No  | Parsing element's [EventType](#eventtype) and [ParseInfo](#parseinfo). The default value is **undefined**, indicating no parsing.|
1087
1088## ParseInfo
1089
1090Provides APIs to manage the parsed XML information.
1091
1092
1093### getColumnNumber
1094
1095getColumnNumber(): number
1096
1097Obtains the current column number, starting from 1.
1098
1099**Atomic service API**: This API can be used in atomic services since API version 11.
1100
1101**System capability**: SystemCapability.Utils.Lang
1102
1103**Return value**
1104
1105| Type  | Description          |
1106| ------ | -------------- |
1107| number | Column number obtained.|
1108
1109**Example**
1110
1111```ts
1112import { util } from '@kit.ArkTS';
1113
1114let strXml = '<?xml version="1.0" encoding="utf-8"?><note>Happy</note>';
1115let textEncoder = new util.TextEncoder();
1116let arrbuffer = textEncoder.encodeInto(strXml);
1117let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1118let str = "";
1119function func(key: xml.EventType, value: xml.ParseInfo) {
1120  str += 'key:' + key + ' value:' + value.getColumnNumber() + ' ';
1121  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1122}
1123let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
1124that.parse(options);
1125console.log(str);
1126// key:0 value:1 key:2 value:45 key:4 value:50 key:3 value:57 key:1 value:57
1127```
1128
1129### getDepth
1130
1131getDepth(): number
1132
1133Obtains the depth of this element.
1134
1135> **NOTE**
1136>
1137> The depth of the whitespace character event in the tag is the same as the depth of the tag.
1138
1139**Atomic service API**: This API can be used in atomic services since API version 11.
1140
1141**System capability**: SystemCapability.Utils.Lang
1142
1143**Return value**
1144
1145| Type  | Description                |
1146| ------ | -------------------- |
1147| number | Depth obtained.|
1148
1149**Example**
1150
1151```ts
1152import { util } from '@kit.ArkTS';
1153
1154let strXml =
1155  '<?xml version="1.0" encoding="utf-8"?>' +
1156  '<note importance="high">' +
1157    '<title>Happy</title>' +
1158  '</note>';
1159let textEncoder = new util.TextEncoder();
1160let arrbuffer = textEncoder.encodeInto(strXml);
1161let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1162let str = "";
1163function func(key: xml.EventType, value: xml.ParseInfo) {
1164  str += 'key:' + key + ' value:' + value.getDepth() + ' ';
1165  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1166}
1167let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
1168that.parse(options);
1169console.log(str);
1170// key:0 value:0 key:2 value:1 key:2 value:2 key:4 value:2 key:3 value:2 key:3 value:1 key:1 value:0
1171```
1172
1173### getLineNumber
1174
1175getLineNumber(): number
1176
1177Obtains the current line number, starting from 1.
1178
1179**Atomic service API**: This API can be used in atomic services since API version 11.
1180
1181**System capability**: SystemCapability.Utils.Lang
1182
1183**Return value**
1184
1185| Type  | Description          |
1186| ------ | -------------- |
1187| number | Line number obtained.|
1188
1189**Example**
1190
1191```ts
1192import { util } from '@kit.ArkTS';
1193
1194let strXml = '<?xml version="1.0" encoding="utf-8"?><note>Work</note>';
1195let textEncoder = new util.TextEncoder();
1196let arrbuffer = textEncoder.encodeInto(strXml);
1197let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1198let str = "";
1199function func(key: xml.EventType, value: xml.ParseInfo) {
1200  str += 'key:' + key + ' value:' + value.getLineNumber() + ' ';
1201  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1202}
1203let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
1204that.parse(options);
1205console.log(str);
1206// key:0 value:1 key:2 value:1 key:4 value:1 key:3 value:1 key:1 value:1
1207```
1208
1209### getName
1210
1211getName(): string
1212
1213Obtains the name of this element.
1214
1215**Atomic service API**: This API can be used in atomic services since API version 11.
1216
1217**System capability**: SystemCapability.Utils.Lang
1218
1219**Return value**
1220
1221| Type  | Description              |
1222| ------ | ------------------ |
1223| string | Element name obtained.|
1224
1225**Example**
1226
1227```ts
1228import { util } from '@kit.ArkTS';
1229
1230let strXml = '<?xml version="1.0" encoding="utf-8"?><note>Happy</note>';
1231let textEncoder = new util.TextEncoder();
1232let arrbuffer = textEncoder.encodeInto(strXml);
1233let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1234let str = "";
1235function func(key: xml.EventType, value: xml.ParseInfo) {
1236  str += 'key:' + key + ' value:' + value.getName() + ' ';
1237  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1238}
1239let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
1240that.parse(options);
1241console.log(str);
1242// key:0 value: key:2 value:note key:4 value: key:3 value:note key:1 value:
1243```
1244### getNamespace
1245
1246getNamespace(): string
1247
1248Obtains the namespace of this element.
1249
1250**Atomic service API**: This API can be used in atomic services since API version 11.
1251
1252**System capability**: SystemCapability.Utils.Lang
1253
1254**Return value**
1255
1256| Type  | Description                    |
1257| ------ | ------------------------ |
1258| string | Namespace obtained.|
1259
1260**Example**
1261
1262```ts
1263import { util } from '@kit.ArkTS';
1264
1265let strXml =
1266  '<?xml version="1.0" encoding="utf-8"?>' +
1267  '<note xmlns:h="http://www.w3.org">' +
1268    '<h:title>Happy</h:title>' +
1269  '</note>';
1270let textEncoder = new util.TextEncoder();
1271let arrbuffer = textEncoder.encodeInto(strXml);
1272let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1273let str = "";
1274function func(key: xml.EventType, value: xml.ParseInfo) {
1275  str += 'key:' + key + ' value:' + value.getNamespace() + ' ';
1276  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1277}
1278let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:false, tokenValueCallbackFunction:func}
1279that.parse(options);
1280console.log(str);
1281// key:0 value: key:2 value: key:2 value:http://www.w3.org key:4 value: key:3 value:http://www.w3.org key:3 value: key:1 value:
1282```
1283### getPrefix
1284
1285getPrefix(): string
1286
1287Obtains the prefix of this element.
1288
1289**Atomic service API**: This API can be used in atomic services since API version 11.
1290
1291**System capability**: SystemCapability.Utils.Lang
1292
1293**Return value**
1294
1295| Type  | Description              |
1296| ------ | ------------------ |
1297| string | Element prefix obtained.|
1298
1299**Example**
1300
1301```ts
1302import { util } from '@kit.ArkTS';
1303
1304let strXml =
1305  '<?xml version="1.0" encoding="utf-8"?>' +
1306  '<note xmlns:h="http://www.w3.org/TR/html4">' +
1307    '<h:title>Happy</h:title>' +
1308  '</note>';
1309let textEncoder = new util.TextEncoder();
1310let arrbuffer = textEncoder.encodeInto(strXml);
1311let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1312let str = "";
1313function func(key: xml.EventType, value: xml.ParseInfo) {
1314  str += 'key:' + key + ' value:' + value.getPrefix() + ' ';
1315  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1316}
1317let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:false, tokenValueCallbackFunction:func}
1318that.parse(options);
1319console.log(str);
1320// key:0 value: key:2 value: key:2 value:h key:4 value: key:3 value:h key:3 value: key:1 value:
1321```
1322
1323### getText
1324
1325getText(): string
1326
1327Obtains the text of the current event.
1328
1329**Atomic service API**: This API can be used in atomic services since API version 11.
1330
1331**System capability**: SystemCapability.Utils.Lang
1332
1333**Return value**
1334
1335| Type  | Description                    |
1336| ------ | ------------------------ |
1337| string | Text content obtained.|
1338
1339**Example**
1340
1341```ts
1342import { util } from '@kit.ArkTS';
1343
1344let strXml = '<?xml version="1.0" encoding="utf-8"?><note>Happy</note>';
1345let textEncoder = new util.TextEncoder();
1346let arrbuffer = textEncoder.encodeInto(strXml);
1347let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1348let str = "";
1349function func(key: xml.EventType, value: xml.ParseInfo) {
1350  str += 'key:' + key + ' value:' + value.getText() + ' ';
1351  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1352}
1353let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
1354that.parse(options);
1355console.log(str);
1356// key:0 value: key:2 value: key:4 value:Happy key:3 value: key:1 value:
1357```
1358### isEmptyElementTag
1359
1360isEmptyElementTag(): boolean
1361
1362Checks whether the current element is empty.
1363
1364**Atomic service API**: This API can be used in atomic services since API version 11.
1365
1366**System capability**: SystemCapability.Utils.Lang
1367
1368**Return value**
1369
1370| Type   | Description                        |
1371| ------- | ---------------------------- |
1372| boolean | Check result. The value **true** is returned if the element is empty; otherwise, **false** is returned.|
1373
1374**Example**
1375
1376```ts
1377import { util } from '@kit.ArkTS';
1378
1379let strXml =
1380  '<?xml version="1.0" encoding="utf-8"?>' +
1381  '<note importance="high" logged="true">' +
1382    '<title/>' +
1383  '</note>';
1384let textEncoder = new util.TextEncoder();
1385let arrbuffer = textEncoder.encodeInto(strXml);
1386let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1387let str = "";
1388function func(key: xml.EventType, value: xml.ParseInfo) {
1389  str += 'key:' + key + ' value:' + value.isEmptyElementTag() + ' ';
1390  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1391}
1392let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
1393that.parse(options);
1394console.log(str);
1395// key:0 value:false key:2 value:false key:2 value:true key:3 value:false key:3 value:false key:1 value:false
1396```
1397### isWhitespace
1398
1399isWhitespace(): boolean
1400
1401Checks whether the current event contains only whitespace characters.
1402
1403**Atomic service API**: This API can be used in atomic services since API version 11.
1404
1405**System capability**: SystemCapability.Utils.Lang
1406
1407**Return value**
1408
1409| Type   | Description                                  |
1410| ------- | -------------------------------------- |
1411| boolean | Check result. The value **true** is returned if the text event contains only whitespace characters; otherwise, **false** is returned.|
1412
1413**Example**
1414
1415```ts
1416import { util } from '@kit.ArkTS';
1417
1418let strXml =
1419  '<?xml version="1.0" encoding="utf-8"?>' +
1420  '<note importance="high" logged="true">' +
1421    '<title> </title>' +
1422  '</note>';
1423let textEncoder = new util.TextEncoder();
1424let arrbuffer = textEncoder.encodeInto(strXml);
1425let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1426let str = "";
1427function func(key: xml.EventType, value: xml.ParseInfo) {
1428  str += 'key:' + key + ' value:' + value.isWhitespace() + ' ';
1429  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1430}
1431let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
1432that.parse(options);
1433console.log(str);
1434// key:0 value:true key:2 value:false key:2 value:true key:10 value:true key:3 value:true key:3 value:true key:1 value:true
1435```
1436### getAttributeCount
1437
1438getAttributeCount(): number
1439
1440Obtains the number of attributes for the current start tag.
1441
1442**Atomic service API**: This API can be used in atomic services since API version 11.
1443
1444**System capability**: SystemCapability.Utils.Lang
1445
1446**Return value**
1447| Type  | Description                  |
1448| ------ | ---------------------- |
1449| number | Number of attributes obtained.|
1450
1451**Example**
1452
1453```ts
1454import { util } from '@kit.ArkTS';
1455
1456let strXml = '<?xml version="1.0" encoding="utf-8"?><note importance="high" logged="true"/>';
1457let textEncoder = new util.TextEncoder();
1458let arrbuffer = textEncoder.encodeInto(strXml);
1459let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer);
1460let str = "";
1461function func(key: xml.EventType, value: xml.ParseInfo) {
1462  str += 'key:' + key + ' value:' + value.getAttributeCount() + ' ';
1463  return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
1464}
1465let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
1466that.parse(options);
1467console.log(str);
1468// key:0 value:0 key:2 value:2 key:3 value:2 key:1 value:0
1469```
1470
1471## EventType
1472
1473Enumerates the event types.
1474
1475**Atomic service API**: This API can be used in atomic services since API version 11.
1476
1477**System capability**: SystemCapability.Utils.Lang
1478
1479| Name            | Value  | Description                 |
1480| ---------------- | ---- | --------------------- |
1481| START_DOCUMENT   | 0    | Start document event.       |
1482| END_DOCUMENT     | 1    | End document event.       |
1483| START_TAG        | 2    | Start tag event.       |
1484| END_TAG          | 3    | End tag event.       |
1485| TEXT             | 4    | Text event.           |
1486| CDSECT           | 5    | CDATA section event.          |
1487| COMMENT          | 6    | XML comment event.        |
1488| DOCDECL          | 7    | XML document type declaration event.|
1489| INSTRUCTION      | 8    | XML processing instruction event.|
1490| ENTITY_REFERENCE | 9    | Entity reference event.       |
1491| WHITESPACE       | 10   | Whitespace character event.           |
1492