• 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.                                      |
31
32**Example**
33
34```js
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
52Sets an attribute.
53
54**System capability**: SystemCapability.Utils.Lang
55
56**Parameters**
57
58| Name| Type  | Mandatory| Description           |
59| ------ | ------ | ---- | --------------- |
60| name   | string | Yes  | Key of the attribute.  |
61| value  | string | Yes  | Value of the attribute.|
62
63**Example**
64
65```js
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```js
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
117Sets a declaration.
118
119**System capability**: SystemCapability.Utils.Lang
120
121**Example**
122
123```js
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```js
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```js
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```js
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
240Sets the comment.
241
242**System capability**: SystemCapability.Utils.Lang
243
244**Parameters**
245
246| Name| Type  | Mandatory| Description                |
247| ------ | ------ | ---- | -------------------- |
248| text   | string | Yes  | Comment to set.|
249
250**Example**
251
252```js
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
271Sets CDATA attributes.
272
273**System capability**: SystemCapability.Utils.Lang
274
275**Parameters**
276
277| Name| Type  | Mandatory| Description             |
278| ------ | ------ | ---- | ----------------- |
279| text   | string | Yes  | CDATA attribute to set.|
280
281**Example**
282
283```js
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
302Sets **Text**.
303
304**System capability**: SystemCapability.Utils.Lang
305
306**Parameters**
307
308| Name| Type  | Mandatory| Description            |
309| ------ | ------ | ---- | ---------------- |
310| text   | string | Yes  | Content of the **Text** to set.|
311
312**Example**
313
314```js
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
336Sets **DocType**.
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 set.|
345
346**Example**
347
348```js
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### XmlPullParser
367
368constructor(buffer: ArrayBuffer | DataView, encoding?: string)
369
370Creates and returns an **XmlPullParser** object. The **XmlPullParser** object passes two parameters. The first parameter is the memory of the **ArrayBuffer** or **DataView** type, and the second parameter is the file format (UTF-8 by default).
371
372**System capability**: SystemCapability.Utils.Lang
373
374**Parameters**
375
376| Name  | Type                             | Mandatory| Description                                      |
377| -------- | --------------------------------- | ---- | ------------------------------------------ |
378| buffer   | ArrayBuffer \| DataView | Yes  | **ArrayBuffer** or **DataView** that contains XML text information.|
379| encoding | string                            | No  | Encoding format. Only UTF-8 is supported.                 |
380
381**Example**
382
383```js
384let strXml =
385    '<?xml version="1.0" encoding="utf-8"?>' +
386    '<!DOCTYPE note [\n<!ENTITY foo "baa">]>' +
387    '<note importance="high" logged="true">' +
388    '    <![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>' +
389    '    <!--Hello, World!-->' +
390    '    <company>John &amp; Hans</company>' +
391    '    <title>Happy</title>' +
392    '    <title>Happy</title>' +
393    '    <lens>Work</lens>' +
394    '    <lens>Play</lens>' +
395    '    <?go there?>' +
396    '    <a><b/></a>' +
397    '    <h:table xmlns:h="http://www.w3.org/TR/html4/">' +
398    '        <h:tr>' +
399    '            <h:td>Apples</h:td>' +
400    '            <h:td>Bananas</h:td>' +
401    '        </h:tr>' +
402    '    </h:table>' +
403    '</note>';
404let arrayBuffer = new ArrayBuffer(strXml.length);
405let bufView = new Uint8Array(arrayBuffer);
406let strLen = strXml.length;
407for (let i = 0; i < strLen; ++i) {
408    bufView[i] = strXml.charCodeAt(i);
409}
410let that = new xml.XmlPullParser(arrayBuffer, 'UTF-8');
411let str1 = '';
412function func1(name, value){
413    str1 += name+':'+value;
414    return true;
415}
416let options = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func1}
417that.parse(options);
418console.log(str1) //'note:company:title:title:lens:lens:a:b:h:table:h:tr:h:td:h:td:'
419```
420
421
422### parse
423
424parse(option: ParseOptions): void
425
426Parses XML information.
427
428**System capability**: SystemCapability.Utils.Lang
429
430**Parameters**
431
432| Name| Type                         | Mandatory| Description                            |
433| ------ | ----------------------------- | ---- | -------------------------------- |
434| option | [ParseOptions](#parseoptions) | Yes  | Options for controlling and obtaining the parsed information.|
435
436**Example**
437
438```js
439let strXml =
440            '<?xml version="1.0" encoding="utf-8"?>' +
441            '<note importance="high" logged="true">' +
442            '    <title>Happy</title>' +
443            '    <todo>Work</todo>' +
444            '    <todo>Play</todo>' +
445            '</note>';
446let arrayBuffer = new ArrayBuffer(strXml.length);
447let bufView = new Uint8Array(arrayBuffer);
448let strLen = strXml.length;
449for (let tmp = 0; tmp < strLen; ++tmp) {
450    bufView[tmp] = strXml.charCodeAt(tmp);
451}
452let that = new xml.XmlPullParser(arrayBuffer);
453let arrTag = {};
454let str = "";
455let i = 0;
456function func(key, value){
457    arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
458    str += arrTag[i];
459    i++;
460    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
461}
462let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
463that.parse(options);
464console.log(str);
465// Output:
466// key:0 value:0key:2 value:1key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:3 value:1key:1 value:0
467// Note:
468// 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:
469// 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), ...
470```
471
472
473## ParseOptions
474
475Defines the XML parsing options.
476
477**System capability**: SystemCapability.Utils.Lang
478
479
480| Name                          | Type                                                        | Mandatory| Description                                   |
481| ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- |
482| supportDoctype                 | boolean                                                      | No  | Whether to ignore **Doctype**. The default value is **false**.|
483| ignoreNameSpace                | boolean                                                      | No  | Whether to ignore **Namespace**. The default value is **false**.         |
484| tagValueCallbackFunction       | (name: string, value: string) =&gt; boolean | No  | Callback used to return **tagValue**.                 |
485| attributeValueCallbackFunction | (name: string, value: string) =&gt; boolean | No  | Callback used to return **attributeValue**.           |
486| tokenValueCallbackFunction     | (eventType: [EventType](#eventtype), value: [ParseInfo](#parseinfo)) =&gt; boolean | No  | Callback used to return **tokenValue**.               |
487
488## ParseInfo
489
490Provides APIs to manage the parsed XML information.
491
492
493### getColumnNumber
494
495getColumnNumber(): number
496
497Obtains the column line number, starting from 1.
498
499**System capability**: SystemCapability.Utils.Lang
500
501**Return value**
502
503| Type  | Description          |
504| ------ | -------------- |
505| number | Column number obtained.|
506
507**Example**
508
509```js
510let strXml =
511            '<?xml version="1.0" encoding="utf-8"?>' +
512            '<note importance="high" logged="true">' +
513            '    <title>Happy</title>' +
514            '    <todo>Work</todo>' +
515            '    <todo>Play</todo>' +
516            '</note>';
517let arrayBuffer = new ArrayBuffer(strXml.length);
518let bufView = new Uint8Array(arrayBuffer);
519let strLen = strXml.length;
520for (let tmp = 0; tmp < strLen; ++tmp) {
521    bufView[tmp] = strXml.charCodeAt(tmp);
522}
523let that = new xml.XmlPullParser(arrayBuffer);
524let arrTag = {};
525let str = "";
526let i = 0;
527function func(key, value){
528    arrTag[i] = 'key:'+key+' value:'+ value.getColumnNumber();
529    str += arrTag[i];
530    i++;
531    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
532}
533let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
534that.parse(options);
535console.log(str);
536// Output:
537// key:0 value:1key:2 value:77key:10 value:81key:2 value:88key:4 value:93key:3 value:101key:10 value:105key:2 value:111key:4 value:115key:3 value:122key:10 value:126key:2 value:132key:4 value:136key:3 value:143key:3 value:150key:1 value:299
538```
539
540### getDepth
541
542getDepth(): number
543
544Obtains the depth of this element.
545
546**System capability**: SystemCapability.Utils.Lang
547
548**Return value**
549
550| Type  | Description                |
551| ------ | -------------------- |
552| number | Depth obtained.|
553
554**Example**
555
556```js
557let strXml =
558            '<?xml version="1.0" encoding="utf-8"?>' +
559            '<note importance="high" logged="true">' +
560            '    <title>Happy</title>' +
561            '    <todo>Work</todo>' +
562            '    <todo>Play</todo>' +
563            '</note>';
564let arrayBuffer = new ArrayBuffer(strXml.length);
565let bufView = new Uint8Array(arrayBuffer);
566let strLen = strXml.length;
567for (let tmp = 0; tmp < strLen; ++tmp) {
568    bufView[tmp] = strXml.charCodeAt(tmp);
569}
570let that = new xml.XmlPullParser(arrayBuffer);
571let arrTag = {};
572let str = "";
573let i = 0;
574function func(key, value){
575    arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
576    str += arrTag[i];
577    i++;
578    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
579}
580let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
581that.parse(options);
582console.log(str);
583// Output:
584// key:0 value:0key:2 value:1key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:3 value:1key:1 value:0
585// Note:
586// 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:
587// 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), ...
588```
589
590### getLineNumber
591
592getLineNumber(): number
593
594Obtains the current line number, starting from 1.
595
596**System capability**: SystemCapability.Utils.Lang
597
598**Return value**
599
600| Type  | Description          |
601| ------ | -------------- |
602| number | Line number obtained.|
603
604**Example**
605
606```js
607let strXml =
608            '<?xml version="1.0" encoding="utf-8"?>' +
609            '<note importance="high" logged="true">' +
610            '    <title>Happy</title>' +
611            '    <todo>Work</todo>' +
612            '    <todo>Play</todo>' +
613            '</note>';
614let arrayBuffer = new ArrayBuffer(strXml.length);
615let bufView = new Uint8Array(arrayBuffer);
616let strLen = strXml.length;
617for (let tmp = 0; tmp < strLen; ++tmp) {
618    bufView[tmp] = strXml.charCodeAt(tmp);
619}
620let that = new xml.XmlPullParser(arrayBuffer);
621let arrTag = {};
622let str = "";
623let i = 0;
624function func(key, value){
625    arrTag[i] = 'key:'+key+' value:'+ value.getLineNumber();
626    str += arrTag[i];
627    i++;
628    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
629}
630let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
631that.parse(options);
632console.log(str);
633// Output:
634// key:0 value:1key:2 value:1key:10 value:1key:2 value:1key:4 value:1key:3 value:1key:10 value:1key:2 value:1key:4 value:1key:3 value:1key:10 value:1key:2 value:1key:4 value:1key:3 value:1key:3 value:1key:1 value:1
635```
636
637### getName
638
639getName(): string
640
641Obtains the name of this element.
642
643**System capability**: SystemCapability.Utils.Lang
644
645**Return value**
646
647| Type  | Description              |
648| ------ | ------------------ |
649| string | Element name obtained.|
650
651**Example**
652
653```js
654let strXml =
655            '<?xml version="1.0" encoding="utf-8"?>' +
656            '<note importance="high" logged="true">' +
657            '    <title>Happy</title>' +
658            '    <todo>Work</todo>' +
659            '    <todo>Play</todo>' +
660            '</note>';
661let arrayBuffer = new ArrayBuffer(strXml.length);
662let bufView = new Uint8Array(arrayBuffer);
663let strLen = strXml.length;
664for (let tmp = 0; tmp < strLen; ++tmp) {
665    bufView[tmp] = strXml.charCodeAt(tmp);
666}
667let that = new xml.XmlPullParser(arrayBuffer);
668let arrTag = {};
669let str = "";
670let i = 0;
671function func(key, value){
672    arrTag[i] = 'key:'+key+' value:'+ value.getName();
673    str += arrTag[i];
674    i++;
675    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
676}
677let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
678that.parse(options);
679console.log(str);
680// Output:
681// key:0 value:key:2 value:notekey:10 value:key:2 value:titlekey:4 value:key:3 value:titlekey:10 value:key:2 value:todokey:4 value:key:3 value:todokey:10 value:key:2 value:todokey:4 value:key:3 value:todokey:3 value:notekey:1 value:
682```
683### getNamespace
684
685getNamespace(): string
686
687Obtains the namespace of this element.
688
689**System capability**: SystemCapability.Utils.Lang
690
691**Return value**
692
693| Type  | Description                    |
694| ------ | ------------------------ |
695| string | Namespace obtained.|
696
697**Example**
698
699```js
700let strXml =
701            '<?xml version="1.0" encoding="utf-8"?>' +
702            '<note importance="high" logged="true">' +
703            '    <title>Happy</title>' +
704            '    <todo>Work</todo>' +
705            '    <todo>Play</todo>' +
706            '</note>';
707let arrayBuffer = new ArrayBuffer(strXml.length);
708let bufView = new Uint8Array(arrayBuffer);
709let strLen = strXml.length;
710for (let tmp = 0; tmp < strLen; ++tmp) {
711    bufView[tmp] = strXml.charCodeAt(tmp);
712}
713let that = new xml.XmlPullParser(arrayBuffer);
714let arrTag = {};
715let str = "";
716let i = 0;
717function func(key, value){
718    arrTag[i] = 'key:'+key+' value:'+ value.getNamespace();
719    str += arrTag[i];
720    i++;
721    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
722}
723let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
724that.parse(options);
725console.log(str);
726// Output:
727// 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:
728```
729### getPrefix
730
731getPrefix(): string
732
733Obtains the prefix of this element.
734
735**System capability**: SystemCapability.Utils.Lang
736
737**Return value**
738
739| Type  | Description              |
740| ------ | ------------------ |
741| string | Element prefix obtained.|
742
743**Example**
744
745```js
746let strXml =
747            '<?xml version="1.0" encoding="utf-8"?>' +
748            '<note importance="high" logged="true">' +
749            '    <title>Happy</title>' +
750            '    <todo>Work</todo>' +
751            '    <todo>Play</todo>' +
752            '</note>';
753let arrayBuffer = new ArrayBuffer(strXml.length);
754let bufView = new Uint8Array(arrayBuffer);
755let strLen = strXml.length;
756for (let tmp = 0; tmp < strLen; ++tmp) {
757    bufView[tmp] = strXml.charCodeAt(tmp);
758}
759let that = new xml.XmlPullParser(arrayBuffer);
760let arrTag = {};
761let str = "";
762let i = 0;
763function func(key, value){
764    arrTag[i] = 'key:'+key+' value:'+ value.getPrefix();
765    str += arrTag[i];
766    i++;
767    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
768}
769let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
770that.parse(options);
771console.log(str);
772// Output:
773// 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:
774```
775
776### getText
777
778getText(): string
779
780Obtains the text of the current event.
781
782**System capability**: SystemCapability.Utils.Lang
783
784**Return value**
785
786| Type  | Description                    |
787| ------ | ------------------------ |
788| string | Text content obtained.|
789
790**Example**
791
792```js
793let strXml =
794            '<?xml version="1.0" encoding="utf-8"?>' +
795            '<note importance="high" logged="true">' +
796            '    <title>Happy</title>' +
797            '    <todo>Work</todo>' +
798            '    <todo>Play</todo>' +
799            '</note>';
800let arrayBuffer = new ArrayBuffer(strXml.length);
801let bufView = new Uint8Array(arrayBuffer);
802let strLen = strXml.length;
803for (let tmp = 0; tmp < strLen; ++tmp) {
804    bufView[tmp] = strXml.charCodeAt(tmp);
805}
806let that = new xml.XmlPullParser(arrayBuffer);
807let arrTag = {};
808let str = "";
809let i = 0;
810function func(key, value){
811    arrTag[i] = 'key:'+key+' value:'+ value.getText();
812    str += arrTag[i];
813    i++;
814    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
815}
816let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
817that.parse(options);
818console.log(str);
819// Output:
820// key:0 value:key:2 value:key:10 value:    key:2 value:key:4 value:Happykey:3 value:key:10 value:    key:2 value:key:4 value:Workkey:3 value:key:10 value:    key:2 value:key:4 value:Playkey:3 value:key:3 value:key:1 value:
821```
822### isEmptyElementTag
823
824isEmptyElementTag(): boolean
825
826Checks whether the current element is empty.
827
828**System capability**: SystemCapability.Utils.Lang
829
830**Return value**
831
832| Type   | Description                        |
833| ------- | ---------------------------- |
834| boolean | Returns **true** if the element is empty; returns **false** otherwise.|
835
836**Example**
837
838```js
839let strXml =
840            '<?xml version="1.0" encoding="utf-8"?>' +
841            '<note importance="high" logged="true">' +
842            '    <title>Happy</title>' +
843            '    <todo>Work</todo>' +
844            '    <todo>Play</todo>' +
845            '</note>';
846let arrayBuffer = new ArrayBuffer(strXml.length);
847let bufView = new Uint8Array(arrayBuffer);
848let strLen = strXml.length;
849for (let tmp = 0; tmp < strLen; ++tmp) {
850    bufView[tmp] = strXml.charCodeAt(tmp);
851}
852let that = new xml.XmlPullParser(arrayBuffer);
853let arrTag = {};
854let str = "";
855let i = 0;
856function func(key, value){
857    arrTag[i] = 'key:'+key+' value:'+ value.isEmptyElementTag();
858    str += arrTag[i];
859    i++;
860    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
861}
862let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
863that.parse(options);
864console.log(str);
865// Output:
866// key:0 value:falsekey:2 value:falsekey:10 value:falsekey:2 value:falsekey:4 value:falsekey:3 value:falsekey:10 value:falsekey:2 value:falsekey:4 value:falsekey:3 value:falsekey:10 value:falsekey:2 value:falsekey:4 value:falsekey:3 value:falsekey:3 value:falsekey:1 value:false
867```
868### isWhitespace
869
870isWhitespace(): boolean
871
872Checks whether the current text event contains only whitespace characters.
873
874**System capability**: SystemCapability.Utils.Lang
875
876**Return value**
877
878| Type   | Description                                  |
879| ------- | -------------------------------------- |
880| boolean | Returns **true** if the text event contains only whitespace characters; returns **false** otherwise.|
881
882**Example**
883
884```js
885let strXml =
886            '<?xml version="1.0" encoding="utf-8"?>' +
887            '<note importance="high" logged="true">' +
888            '    <title>Happy</title>' +
889            '    <todo>Work</todo>' +
890            '    <todo>Play</todo>' +
891            '</note>';
892let arrayBuffer = new ArrayBuffer(strXml.length);
893let bufView = new Uint8Array(arrayBuffer);
894let strLen = strXml.length;
895for (let tmp = 0; tmp < strLen; ++tmp) {
896    bufView[tmp] = strXml.charCodeAt(tmp);
897}
898let that = new xml.XmlPullParser(arrayBuffer);
899let arrTag = {};
900let str = "";
901let i = 0;
902function func(key, value){
903    arrTag[i] = 'key:'+key+' value:'+ value.isWhitespace();
904    str += arrTag[i];
905    i++;
906    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
907}
908let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
909that.parse(options);
910console.log(str);
911// Output:
912// key:0 value:truekey:2 value:falsekey:10 value:truekey:2 value:truekey:4 value:falsekey:3 value:truekey:10 value:truekey:2 value:truekey:4 value:falsekey:3 value:truekey:10 value:truekey:2 value:truekey:4 value:falsekey:3 value:truekey:3 value:truekey:1 value:true
913```
914### getAttributeCount
915
916getAttributeCount(): number
917
918Obtains the number of attributes for the current start tag.
919
920**System capability**: SystemCapability.Utils.Lang
921
922**Return value**
923| Type  | Description                  |
924| ------ | ---------------------- |
925| number | Number of attributes obtained.|
926
927**Example**
928
929```js
930let strXml =
931            '<?xml version="1.0" encoding="utf-8"?>' +
932            '<note importance="high" logged="true">' +
933            '    <title>Happy</title>' +
934            '    <todo>Work</todo>' +
935            '    <todo>Play</todo>' +
936            '</note>';
937let arrayBuffer = new ArrayBuffer(strXml.length);
938let bufView = new Uint8Array(arrayBuffer);
939let strLen = strXml.length;
940for (let tmp = 0; tmp < strLen; ++tmp) {
941    bufView[tmp] = strXml.charCodeAt(tmp);
942}
943let that = new xml.XmlPullParser(arrayBuffer);
944let arrTag = {};
945let str = "";
946let i = 0;
947function func(key, value){
948    arrTag[i] = 'key:'+key+' value:'+ value.getAttributeCount();
949    str += arrTag[i];
950    i++;
951    return true; // Determines whether to continuely parse, which is used to continue or terminate parsing.
952}
953let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
954that.parse(options);
955console.log(str);
956// Output:
957// key:0 value:0key:2 value:2key:10 value:0key:2 value:0key:4 value:0key:3 value:0key:10 value:0key:2 value:0key:4 value:0key:3 value:0key:10 value:0key:2 value:0key:4 value:0key:3 value:0key:3 value:0key:1 value:0
958```
959
960## EventType
961
962Enumerates the events.
963
964**System capability**: SystemCapability.Utils.Lang
965
966| Name            | Value  | Description                 |
967| ---------------- | ---- | --------------------- |
968| START_DOCUMENT   | 0    | Indicates a start document event.       |
969| END_DOCUMENT     | 1    | Indicates an end document event.       |
970| START_TAG        | 2    | Indicates a start tag event.       |
971| END_TAG          | 3    | Indicates an end tag event.       |
972| TEXT             | 4    | Indicates a text event.           |
973| CDSECT           | 5    | Indicates a CDATA section event.          |
974| COMMENT          | 6    | Indicates an XML comment event.        |
975| DOCDECL          | 7    | Indicates an XML document type declaration event.|
976| INSTRUCTION      | 8    | Indicates an XML processing instruction event.|
977| ENTITY_REFERENCE | 9    | Indicates an entity reference event.       |
978| WHITESPACE       | 10   | Indicates a whitespace character event.           |
979