# @ohos.xml (XML Parsing and Generation) The xml module provides APIs for generating and parsing XML files. The module offers two methods for generating XML files: * [XmlSerializer](#xmlserializer): suitable for scenarios where the size of the XML text is known in advance. * [XmlDynamicSerializer20+](#xmldynamicserializer20): suitable for scenarios where the size of the XML text is not known in advance. > **NOTE** > > 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. ## Modules to Import ``` import { xml } from '@kit.ArkTS'; ``` ## XmlSerializer **XmlSerializer** provides APIs to generate an XML file. ### constructor constructor(buffer: ArrayBuffer | DataView, encoding?: string) A constructor used to create an **XmlSerializer** instance. > **NOTE** > > 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. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------- | ---- | ------------------------------------------------ | | buffer | ArrayBuffer \| DataView | Yes | ArrayBuffer or DataView for storing the XML information to set.| | encoding | string | No | Encoding format. The default value is **'utf-8'** (the only format currently supported). | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer, "utf-8"); ``` ### setAttributes setAttributes(name: string, value: string): void Sets an attribute. > **NOTE** > > 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. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | --------------- | | name | string | Yes | Key of the attribute. | | value | string | Yes | Value of the attribute.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.startElement("note"); thatSer.setAttributes("importance", "high"); thatSer.endElement(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // ``` ### addEmptyElement addEmptyElement(name: string): void Adds an empty element. > **NOTE** > > 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. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------ | | name | string | Yes | Name of the element.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.addEmptyElement("d"); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // ``` ### setDeclaration setDeclaration(): void Sets a file declaration with encoding. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.setDeclaration(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // ``` ### startElement startElement(name: string): void Adds the start tag based on the given element name. > **NOTE** > >- After calling this API, you must call [endElement](#endelement) to write the end flag to ensure that the node is closed correctly. > >- 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. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------ | | name | string | Yes | Name of the element.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.startElement("note"); thatSer.setText("Happy"); thatSer.endElement(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // Happy ``` ### endElement endElement(): void Adds the end tag of the element. > **NOTE** > > Before calling this API, you must call [startElement](#startelement) to write the start flag. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.startElement("note"); thatSer.setText("Happy"); thatSer.endElement(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // Happy ``` ### setNamespace setNamespace(prefix: string, namespace: string): void Sets the namespace for an element tag. > **NOTE** > > 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. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | --------- | ------ | ---- | ------------------------------ | | prefix | string | Yes | Prefix of the element and its child elements. | | namespace | string | Yes | Namespace to set.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.setNamespace("h", "http://www.w3.org/TR/html4/"); thatSer.startElement("note"); thatSer.endElement(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // ``` ### setComment setComment(text: string): void Sets a comment. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------- | | text | string | Yes | Comment to set.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.setComment("Hello, World!"); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // ``` ### setCDATA setCDATA(text: string): void Adds data to the CDATA tag. The structure of the generated CDATA tag is "\". > **NOTE** > > 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. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ----------------- | | text | string | Yes | CDATA data to set.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.setCDATA('root SYSTEM') let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // ``` ### setText setText(text: string): void Sets a tag value. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ---------------- | | text | string | Yes | Tag value to set, which is the content of the **text** attribute.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.startElement("note"); thatSer.setAttributes("importance", "high"); thatSer.setText("Happy"); thatSer.endElement(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // Happy ``` ### setDocType setDocType(text: string): void Sets a document type. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------- | | text | string | Yes | Content of **DocType** to set.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts import { util } from '@kit.ArkTS'; let arrayBuffer = new ArrayBuffer(2048); let thatSer = new xml.XmlSerializer(arrayBuffer); thatSer.setDocType('root SYSTEM "http://www.test.org/test.dtd"'); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.log(result); // ``` ## XmlDynamicSerializer20+ The **XmlDynamicSerializer** class is used to generate XML strings. It is recommended when the length of the XML content cannot be determined in advance. > **NOTE** > > 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. ### constructor20+ constructor(encoding?: string) A constructor used to create an **XmlDynamicSerializer** instance. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------- | ---- | ------------------------------------------------ | | encoding | string | No | Encoding format. The default value is **'utf-8'** (the only format currently supported). | **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200066 | Incorrect encoding format, only support utf-8. | **Example** ```ts let serializer = new xml.XmlDynamicSerializer('utf-8'); ``` ### getOutput20+ getOutput(): ArrayBuffer Obtains the ArrayBuffer of the XML string. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | -------------------- | | ArrayBuffer | ArrayBuffer for storing the XML information to set.| **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.startElement("note"); serializer.setText("Happy"); serializer.endElement(); let arr = serializer.getOutput(); let uint8 = new Uint8Array(arr); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // Happy ``` ### setAttributes20+ setAttributes(name: string, value: string): void Sets an attribute. > **NOTE** > > 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. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | --------------- | | name | string | Yes | Key of the attribute. The total length of the XML cannot exceed 100,000 characters.| | value | string | Yes | Value of the attribute. The total length of the XML cannot exceed 100,000 characters.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200063 | Illegal position for xml. | | 10200064 | Cannot be an empty string. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.startElement("note"); serializer.setAttributes("importance", "high"); serializer.endElement(); let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // ``` ### addEmptyElement20+ addEmptyElement(name: string): void Adds an empty element. > **NOTE** > > 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. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------ | | name | string | Yes | Name of the empty element. The total length of the XML cannot exceed 100,000 characters.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200064 | Cannot be an empty string. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.addEmptyElement("d"); let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // ``` ### setDeclaration20+ setDeclaration(): void Sets a file declaration with encoding. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200063 | Illegal position for xml. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.setDeclaration(); let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // ``` ### startElement20+ startElement(name: string): void Writes the start tag of the element. > **NOTE** > >- After calling this API, you must call [endElement](#endelement) to write the end flag to ensure that the node is closed correctly. > >- 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. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------ | | name | string | Yes | Name of the element. The total length of the XML cannot exceed 100,000 characters.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200064 | Cannot be an empty string. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.startElement("note"); serializer.setText("Happy"); serializer.endElement(); let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // Happy ``` ### endElement20+ endElement(): void Writes the end tag of the element. > **NOTE** > > Before calling this API, you must call [startElement](#startelement) to write the start flag. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200065 | There is no match between the startElement and the endElement. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.startElement("note"); serializer.setText("Happy"); serializer.endElement(); let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // Happy ``` ### setNamespace20+ setNamespace(prefix: string, namespace: string): void Sets the namespace for an element tag. > **NOTE** > > 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. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | --------- | ------ | ---- | ------------------------------ | | prefix | string | Yes | Prefix of the element and its child elements. The total length of the XML cannot exceed 100,000 characters.| | namespace | string | Yes | Namespace to set. The total length of the XML cannot exceed 100,000 characters.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200064 | Cannot be an empty string. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.setNamespace("h", "http://www.w3.org/TR/html4/"); serializer.startElement("note"); serializer.endElement(); let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // ``` ### setComment20+ setComment(text: string): void Sets a comment. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------- | | text | string | Yes | Comment to set. The total length of the XML cannot exceed 100,000 characters.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200064 | Cannot be an empty string. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.setComment("Hello, World!"); let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // ``` ### setCdata20+ setCdata(text: string): void Adds data to the CDATA tag. The structure of the generated CDATA tag is "\". > **NOTE** > > 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. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ----------------- | | text | string | Yes | CDATA data to set. The total length of the XML cannot exceed 100,000 characters.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200064 | Cannot be an empty string. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.setCdata('root SYSTEM') let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // ``` ### setText20+ setText(text: string): void Sets a tag value. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ---------------- | | text | string | Yes | Tag value. The total length of the XML cannot exceed 100,000 characters.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200064 | Cannot be an empty string. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.startElement("note"); serializer.setAttributes("importance", "high"); serializer.setText("Happy"); serializer.endElement(); let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // Happy ``` ### setDocType20+ setDocType(text: string): void Sets a document type. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------- | | text | string | Yes | Content of **DocType** to set. The total length of the XML cannot exceed 100,000 characters.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200062 | The cumulative length of xml has exceeded the upper limit 100000. | | 10200064 | Cannot be an empty string. | **Example** ```ts import { util } from '@kit.ArkTS'; let serializer = new xml.XmlDynamicSerializer('utf-8'); serializer.setDocType('root SYSTEM "http://www.test.org/test.dtd"'); let arrayBuffer = serializer.getOutput(); let uint8 = new Uint8Array(arrayBuffer); let result = util.TextDecoder.create().decodeToString(uint8); console.info(result); // ``` ## XmlPullParser Implements XML file parsing. ### constructor constructor(buffer: ArrayBuffer | DataView, encoding?: string) Creates and returns an **XmlPullParser** object. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------- | ---- | ------------------------------------------ | | buffer | ArrayBuffer \| DataView | Yes | XML text information to be parsed.| | encoding | string | No | Encoding format. The default value is **'utf-8'** (the only format currently supported). | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = 'Happy' let textEncoder = new util.TextEncoder(); let uint8Array = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(uint8Array.buffer as object as ArrayBuffer, 'UTF-8'); ``` ### parseXml14+ parseXml(option: ParseOptions): void Parses XML information. **Atomic service API**: This API can be used in atomic services since API version 14. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ----------------------------- | ---- | ------------- | | option | [ParseOptions](#parseoptions) | Yes | XML parsing options.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | **Example** ```ts import { xml, util } from '@kit.ArkTS'; let strxml = '' + '' + ' <![CDATA[Test\nTest]]>' + ''; let textEncoder = new util.TextEncoder(); let uint8 = textEncoder.encodeInto(strxml); function func(key: xml.EventType, value: xml.ParseInfo) { if (key == xml.EventType.CDSECT) { console.log(JSON.stringify(value.getText())); } return true; } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} let pullParser = new xml.XmlPullParser(uint8.buffer as object as ArrayBuffer); pullParser.parseXml(options); // "Test\nTest" ``` ### parse(deprecated) parse(option: ParseOptions): void Parses XML information. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 14. You are advised to use [parseXml14+](#parsexml14) instead. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ----------------------------- | ---- | -------------------------------- | | option | [ParseOptions](#parseoptions) | Yes | XML parsing options.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = '' + '' + 'John & Hans' + 'Happy' + ''; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer, 'UTF-8'); let str = ''; function func(name: string, value: string) { str = name + value; console.log(str); return true; } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func} that.parse(options); // note // company // John & Hans // company // title // Happy // title // note ``` ## ParseOptions Defines the XML parsing options. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang | Name | Type | Mandatory| Description | | ------------------------------ | ------------------------------------------------------------ | ---- | --------------------------------------- | | 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**.| | 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**.| | tagValueCallbackFunction | (name: string, value: string) => boolean | No | Start tag, tag value, and end tag of parsing. The default value is **undefined**, indicating no parsing.| | attributeValueCallbackFunction | (name: string, value: string) => boolean | No | Parsing attribute and attribute value. The default value is **undefined**, indicating no parsing.| | tokenValueCallbackFunction | (eventType: [EventType](#eventtype), value: [ParseInfo](#parseinfo)) => boolean | No | Parsing element's [EventType](#eventtype) and [ParseInfo](#parseinfo). The default value is **undefined**, indicating no parsing.| ## ParseInfo Provides APIs to manage the parsed XML information. ### getColumnNumber getColumnNumber(): number Obtains the current column number, starting from 1. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | -------------- | | number | Column number obtained.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = 'Happy'; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.getColumnNumber() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // key:0 value:1 key:2 value:45 key:4 value:50 key:3 value:57 key:1 value:57 ``` ### getDepth getDepth(): number Obtains the depth of this element. > **NOTE** > > The depth of the whitespace character event in the tag is the same as the depth of the tag. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | -------------------- | | number | Depth obtained.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = '' + '' + 'Happy' + ''; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.getDepth() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // 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 ``` ### getLineNumber getLineNumber(): number Obtains the current line number, starting from 1. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | -------------- | | number | Line number obtained.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = 'Work'; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.getLineNumber() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // key:0 value:1 key:2 value:1 key:4 value:1 key:3 value:1 key:1 value:1 ``` ### getName getName(): string Obtains the name of this element. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | ------------------ | | string | Element name obtained.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = 'Happy'; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.getName() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // key:0 value: key:2 value:note key:4 value: key:3 value:note key:1 value: ``` ### getNamespace getNamespace(): string Obtains the namespace of this element. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | ------------------------ | | string | Namespace obtained.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = '' + '' + 'Happy' + ''; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.getNamespace() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:false, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // 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: ``` ### getPrefix getPrefix(): string Obtains the prefix of this element. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | ------------------ | | string | Element prefix obtained.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = '' + '' + 'Happy' + ''; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.getPrefix() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:false, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // key:0 value: key:2 value: key:2 value:h key:4 value: key:3 value:h key:3 value: key:1 value: ``` ### getText getText(): string Obtains the text of the current event. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | ------------------------ | | string | Text content obtained.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = 'Happy'; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.getText() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // key:0 value: key:2 value: key:4 value:Happy key:3 value: key:1 value: ``` ### isEmptyElementTag isEmptyElementTag(): boolean Checks whether the current element is empty. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------- | ---------------------------- | | boolean | Check result. The value **true** is returned if the element is empty; otherwise, **false** is returned.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = '' + '' + '' + '</note>'; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.isEmptyElementTag() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // key:0 value:false key:2 value:false key:2 value:true key:3 value:false key:3 value:false key:1 value:false ``` ### isWhitespace isWhitespace(): boolean Checks whether the current event contains only whitespace characters. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------- | -------------------------------------- | | boolean | Check result. The value **true** is returned if the text event contains only whitespace characters; otherwise, **false** is returned.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = '<?xml version="1.0" encoding="utf-8"?>' + '<note importance="high" logged="true">' + '<title> ' + ''; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.isWhitespace() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // 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 ``` ### getAttributeCount getAttributeCount(): number Obtains the number of attributes for the current start tag. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | ---------------------- | | number | Number of attributes obtained.| **Example** ```ts import { util } from '@kit.ArkTS'; let strXml = ''; let textEncoder = new util.TextEncoder(); let arrbuffer = textEncoder.encodeInto(strXml); let that = new xml.XmlPullParser(arrbuffer.buffer as object as ArrayBuffer); let str = ""; function func(key: xml.EventType, value: xml.ParseInfo) { str += 'key:' + key + ' value:' + value.getAttributeCount() + ' '; return true; // Determines whether to continually parse, which is used to continue or terminate parsing. } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func} that.parse(options); console.log(str); // key:0 value:0 key:2 value:2 key:3 value:2 key:1 value:0 ``` ## EventType Enumerates the event types. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Utils.Lang | Name | Value | Description | | ---------------- | ---- | --------------------- | | START_DOCUMENT | 0 | Start document event. | | END_DOCUMENT | 1 | End document event. | | START_TAG | 2 | Start tag event. | | END_TAG | 3 | End tag event. | | TEXT | 4 | Text event. | | CDSECT | 5 | CDATA section event. | | COMMENT | 6 | XML comment event. | | DOCDECL | 7 | XML document type declaration event.| | INSTRUCTION | 8 | XML processing instruction event.| | ENTITY_REFERENCE | 9 | Entity reference event. | | WHITESPACE | 10 | Whitespace character event. |