• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.xml (XML Parsing and Generation)
2
3The **XML** module provides a series of APIs for converting XML text into JavaScript objects and generating and parsing XML files.
4
5> **NOTE**
6>
7> 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.
8
9
10## Modules to Import
11
12```
13import xml from '@ohos.xml';
14```
15
16## XmlSerializer
17
18**XmlSerializer** provides APIs to generate an XML file.
19
20### constructor
21
22constructor(buffer: ArrayBuffer | DataView, encoding?: string)
23
24A constructor used to create an **XmlSerializer** instance.
25
26**System capability**: SystemCapability.Utils.Lang
27
28**Parameters**
29
30| Name  | Type                             | Mandatory| Description                                            |
31| -------- | --------------------------------- | ---- | ------------------------------------------------ |
32| buffer   | ArrayBuffer \| DataView | Yes  | **ArrayBuffer** or **DataView** for storing the XML information to set.|
33| encoding | string                            | No  | Encoding format. The default value is **'utf-8'** (the only format currently supported).              |
34
35**Example**
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
55Sets an attribute.
56
57**System capability**: SystemCapability.Utils.Lang
58
59**Parameters**
60
61| Name| Type  | Mandatory| Description           |
62| ------ | ------ | ---- | --------------- |
63| name   | string | Yes  | Key of the attribute.  |
64| value  | string | Yes  | Value of the attribute.|
65
66**Example**
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
89Adds an empty element.
90
91**System capability**: SystemCapability.Utils.Lang
92
93**Parameters**
94
95| Name| Type  | Mandatory| Description              |
96| ------ | ------ | ---- | ------------------ |
97| name   | string | Yes  | Name of the empty element to add.|
98
99**Example**
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
120Sets a file declaration with encoding.
121
122**System capability**: SystemCapability.Utils.Lang
123
124**Example**
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
150Writes the start tag based on the given element name.
151
152**System capability**: SystemCapability.Utils.Lang
153
154**Parameters**
155
156| Name| Type  | Mandatory| Description              |
157| ------ | ------ | ---- | ------------------ |
158| name   | string | Yes  | Name of the element.|
159
160**Example**
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
183Writes the end tag of the element.
184
185**System capability**: SystemCapability.Utils.Lang
186
187**Example**
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
211Sets the namespace for an element tag.
212
213**System capability**: SystemCapability.Utils.Lang
214
215**Parameters**
216
217| Name   | Type  | Mandatory| Description                          |
218| --------- | ------ | ---- | ------------------------------ |
219| prefix    | string | Yes  | Prefix of the element and its child elements.    |
220| namespace | string | Yes  | Namespace to set.|
221
222**Example**
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
245Sets a comment.
246
247**System capability**: SystemCapability.Utils.Lang
248
249**Parameters**
250
251| Name| Type  | Mandatory| Description                |
252| ------ | ------ | ---- | -------------------- |
253| text   | string | Yes  | Comment to set.|
254
255**Example**
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
276Sets CDATA data.
277
278**System capability**: SystemCapability.Utils.Lang
279
280**Parameters**
281
282| Name| Type  | Mandatory| Description             |
283| ------ | ------ | ---- | ----------------- |
284| text   | string | Yes  | CDATA data to set.|
285
286**Example**
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
307Sets a tag value.
308
309**System capability**: SystemCapability.Utils.Lang
310
311**Parameters**
312
313| Name| Type  | Mandatory| Description            |
314| ------ | ------ | ---- | ---------------- |
315| text   | string | Yes  | Tag value to set, which is the content of the **text** attribute.|
316
317**Example**
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
341Sets a document type.
342
343**System capability**: SystemCapability.Utils.Lang
344
345**Parameters**
346
347| Name| Type  | Mandatory| Description               |
348| ------ | ------ | ---- | ------------------- |
349| text   | string | Yes  | Content of **DocType** to set.|
350
351**Example**
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
370Implements XML file parsing.
371
372### constructor
373
374constructor(buffer: ArrayBuffer | DataView, encoding?: string)
375
376Creates and returns an **XmlPullParser** object.
377
378**System capability**: SystemCapability.Utils.Lang
379
380**Parameters**
381
382| Name  | Type                             | Mandatory| Description                                      |
383| -------- | --------------------------------- | ---- | ------------------------------------------ |
384| buffer   | ArrayBuffer \| DataView | Yes  | XML text information to be parsed.|
385| encoding | string                            | No  | Encoding format. The default value is **'utf-8'** (the only format currently supported).        |
386
387**Example**
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
431Parses XML information.
432
433**System capability**: SystemCapability.Utils.Lang
434
435**Parameters**
436
437| Name| Type                         | Mandatory| Description                            |
438| ------ | ----------------------------- | ---- | -------------------------------- |
439| option | [ParseOptions](#parseoptions) | Yes  | Options for controlling and obtaining the parsed information.|
440
441**Example**
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// Output:
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// Note:
467// 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:
468// 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), ...
469```
470
471
472## ParseOptions
473
474Defines the XML parsing options.
475
476**System capability**: SystemCapability.Utils.Lang
477
478
479| Name                          | Type                                                        | Mandatory| Description                                   |
480| ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- |
481| supportDoctype                 | boolean                                                      | No  | Whether to ignore the document type. The default value is **false**, indicating that the document type is parsed.|
482| ignoreNameSpace                | boolean                                                      | No  | Whether to ignore the namespace. The default value is **false**, indicating that the namespace is parsed.|
483| 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. |
484| 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.|
485| 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.|
486
487## ParseInfo
488
489Provides APIs to manage the parsed XML information.
490
491
492### getColumnNumber
493
494getColumnNumber(): number
495
496Obtains the column line number, starting from 1.
497
498**System capability**: SystemCapability.Utils.Lang
499
500**Return value**
501
502| Type  | Description          |
503| ------ | -------------- |
504| number | Column number obtained.|
505
506**Example**
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// Output:
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
537Obtains the depth of this element.
538
539**System capability**: SystemCapability.Utils.Lang
540
541**Return value**
542
543| Type  | Description                |
544| ------ | -------------------- |
545| number | Depth obtained.|
546
547**Example**
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// Output:
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// Note:
573// 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:
574// 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), ...
575```
576
577### getLineNumber
578
579getLineNumber(): number
580
581Obtains the current line number, starting from 1.
582
583**System capability**: SystemCapability.Utils.Lang
584
585**Return value**
586
587| Type  | Description          |
588| ------ | -------------- |
589| number | Line number obtained.|
590
591**Example**
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// Output:
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
622Obtains the name of this element.
623
624**System capability**: SystemCapability.Utils.Lang
625
626**Return value**
627
628| Type  | Description              |
629| ------ | ------------------ |
630| string | Element name obtained.|
631
632**Example**
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// Output:
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
662Obtains the namespace of this element.
663
664**System capability**: SystemCapability.Utils.Lang
665
666**Return value**
667
668| Type  | Description                    |
669| ------ | ------------------------ |
670| string | Namespace obtained.|
671
672**Example**
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// Output:
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
702Obtains the prefix of this element.
703
704**System capability**: SystemCapability.Utils.Lang
705
706**Return value**
707
708| Type  | Description              |
709| ------ | ------------------ |
710| string | Element prefix obtained.|
711
712**Example**
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// Output:
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
743Obtains the text of the current event.
744
745**System capability**: SystemCapability.Utils.Lang
746
747**Return value**
748
749| Type  | Description                    |
750| ------ | ------------------------ |
751| string | Text content obtained.|
752
753**Example**
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// Output:
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
783Checks whether the current element is empty.
784
785**System capability**: SystemCapability.Utils.Lang
786
787**Return value**
788
789| Type   | Description                        |
790| ------- | ---------------------------- |
791| boolean | Returns **true** if the element is empty; returns **false** otherwise.|
792
793**Example**
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// Output:
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
823Checks whether the current text event contains only whitespace characters.
824
825**System capability**: SystemCapability.Utils.Lang
826
827**Return value**
828
829| Type   | Description                                  |
830| ------- | -------------------------------------- |
831| boolean | Returns **true** if the text event contains only whitespace characters; returns **false** otherwise.|
832
833**Example**
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// Output:
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
863Obtains the number of attributes for the current start tag.
864
865**System capability**: SystemCapability.Utils.Lang
866
867**Return value**
868| Type  | Description                  |
869| ------ | ---------------------- |
870| number | Number of attributes obtained.|
871
872**Example**
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// Output:
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
901Enumerates the event types.
902
903**System capability**: SystemCapability.Utils.Lang
904
905| Name            | Value  | Description                 |
906| ---------------- | ---- | --------------------- |
907| START_DOCUMENT   | 0    | Start document event.       |
908| END_DOCUMENT     | 1    | End document event.       |
909| START_TAG        | 2    | Start tag event.       |
910| END_TAG          | 3    | End tag event.       |
911| TEXT             | 4    | Text event.           |
912| CDSECT           | 5    | CDATA section event.          |
913| COMMENT          | 6    | XML comment event.        |
914| DOCDECL          | 7    | XML document type declaration event.|
915| INSTRUCTION      | 8    | XML processing instruction event.|
916| ENTITY_REFERENCE | 9    | Entity reference event.       |
917| WHITESPACE       | 10   | Whitespace character event.           |
918