# XML Parsing and Generation
> ![icon-note.gif](public_sys-resources/icon-note.gif) **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 '@ohos.xml';
```
## System Capabilities
SystemCapability.Utils.Lang
## XmlSerializer
### constructor
constructor(buffer: ArrayBuffer | DataView, encoding?: string)
A constructor used to create an **XmlSerializer** instance.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| buffer | ArrayBuffer \| DataView | Yes| **ArrayBuffer** or **DataView** for storing the XML information to write.|
| encoding | string | No| Encoding format.|
**Example**
```js
var arrayBuffer = new ArrayBuffer(1024);
var bufView = new DataView(arrayBuffer);
var thatSer = new xml.XmlSerializer(bufView);
```
### setAttributes
setAttributes(name: string, value: string): void
Sets an attribute.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the attribute.|
| value | string | Yes| Value of the attribute.|
**Example**
```js
var thatSer = new xml.XmlSerializer(bufView);
thatSer.setAttributes("importance", "high");
```
### addEmptyElement
addEmptyElement(name: string): void
Adds an empty element.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the empty element to add.|
**Example**
```js
var thatSer = new xml.XmlSerializer(bufView);
thatSer.addEmptyElement("b"); // =>
```
### setDeclaration
setDeclaration(): void
Sets a declaration.
**Example**
```js
var thatSer = new xml.XmlSerializer(bufView);
thatSer.setDeclaration() // => ;
```
### startElement
startElement(name: string): void
Writes the start tag based on the given element name.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the element.|
**Example**
```js
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.startElement("notel");
thatSer.endElement();// => '';
```
### endElement
endElement(): void
Writes the end tag of the element.
**Example**
```js
var thatSer = new xml.XmlSerializer(bufView);
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("table");
thatSer.setAttributes("importance", "high");
thatSer.setText("Happy");
endElement(); // => Happy
```
### setNamespace
setNamespace(prefix: string, namespace: string): void
Sets the namespace for an element tag.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| prefix | string | Yes| Prefix of the element and its child elements.|
| namespace | string | Yes| Namespace to set.|
**Example**
```js
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setDeclaration();
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("note");
thatSer.endElement();// = >'\r\n';
```
### setComment
setComment(text: string): void
Sets the comment.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Comment to set.|
**Example**
```js
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.startElement("note");
thatSer.setComment("Hi!");
thatSer.endElement(); // => '\r\n \r\n';
```
### setCDATA
setCDATA(text: string): void
Sets CDATA attributes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| CDATA attribute to set.|
**Example**
```js
var arrayBuffer = new ArrayBuffer(1028);
var thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setCDATA('root SYSTEM') // => '';
```
### setText
setText(text: string): void
Sets **Text**.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Content of the **Text** to set.|
**Example**
```js
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.startElement("note");
thatSer.setAttributes("importance", "high");
thatSer.setText("Happy1");
thatSer.endElement(); // => 'Happy1';
```
### setDocType
setDocType(text: string): void
Sets **DocType**.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Content of **DocType** to set.|
**Example**
```js
var arrayBuffer = new ArrayBuffer(1024);
var thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setDocType('root SYSTEM'); // => '';
```
## XmlPullParser
### XmlPullParser
constructor(buffer: ArrayBuffer | DataView, encoding?: string)
Creates 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).
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| buffer | ArrayBuffer \| DataView | Yes| **ArrayBuffer** or **DataView** that contains XML text information.|
| encoding | string | No| Encoding format. Only UTF-8 is supported.|
**Example**
```js
var strXml =
'' +
'' +
' Happy' +
' Work' +
' Play' +
'';
var arrayBuffer = new ArrayBuffer(strXml.length*2);
var bufView = new Uint8Array(arrayBuffer);
var strLen = strXml.length;
for (var i = 0; i < strLen; ++i) {
bufView[i] = strXml.charCodeAt(i);// Set the ArrayBuffer mode.
}
var that = new xml.XmlPullParser(arrayBuffer);
```
### parse
parse(option: ParseOptions): void
Parses XML information.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| option | [ParseOptions](#parseoptions) | Yes| Options for controlling and obtaining the parsed information.|
**Example**
```js
var strXml =
'' +
'' +
' Happy' +
' Work' +
' Play' +
'';
var arrayBuffer = new ArrayBuffer(strXml.length*2);
var bufView = new Uint8Array(arrayBuffer);
var strLen = strXml.length;
for (var i = 0; i < strLen; ++i) {
bufView[i] = strXml.charCodeAt(i);
}
var that = new xml.XmlPullParser(arrayBuffer);
var arrTag = {};
arrTag[0] = '132';
var i = 1;
function func(key, value){
arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
i++;
return true;
}
var options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
```
## ParseOptions
Defines the XML parsing options.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| supportDoctype | boolean | No| Whether to ignore **Doctype**. The default value is **false**.|
| ignoreNameSpace | boolean | No| Whether to ignore **Namespace**. The default value is **false**.|
| tagValueCallbackFunction | (name: string, value: string)=> boolean | No| Callback used to return **tagValue**.|
| attributeValueCallbackFunction | (name: string, value: string)=> boolean | No| Callback used to return **attributeValue**.|
| tokenValueCallbackFunction | (eventType: [EventType](#eventtype), value: [ParseInfo](#parseinfo))=> boolean | No| Callback used to return **tokenValue**.|
## ParseInfo
Provides APIs to manage the parsed XML information.
### getColumnNumber
getColumnNumber(): number
Obtains the column line number, which starts from 1.
**Return value**
| Type| Description|
| -------- | -------- |
| number | Column number obtained.|
### getDepth
getDepth(): number
Obtains the depth of this element.
**Return value**
| Type| Description|
| -------- | -------- |
| number | Depth obtained.|
### getLineNumber
getLineNumber(): number
Obtains the current line number, starting from 1.
**Return value**
| Type| Description|
| -------- | -------- |
| number | Line number obtained.|
### getName
getName(): string
Obtains the name of this element.
**Return value**
| Type| Description|
| -------- | -------- |
| string | Element name obtained.|
### getNamespace
getNamespace(): string
Obtains the namespace of this element.
**Return value**
| Type| Description|
| -------- | -------- |
| string | Namespace obtained.|
### getPrefix
getPrefix(): string
Obtains the prefix of this element.
**Return value**
| Type| Description|
| -------- | -------- |
| string | Element prefix obtained.|
### getText
getText(): string
Obtains the text of the current event.
**Return value**
| Type| Description|
| -------- | -------- |
| string | Text content obtained.|
### isEmptyElementTag
isEmptyElementTag(): boolean
Checks whether the current element is empty.
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the element is empty; returns **false** otherwise.|
### isWhitespace
isWhitespace(): boolean
Checks whether the current text event contains only whitespace characters.
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the text event contains only whitespace characters; returns **false** otherwise.|
### getAttributeCount
getAttributeCount(): number
Obtains the number of attributes for the current start tag.
**Return value**
| Type| Description|
| -------- | -------- |
| number | Number of attributes obtained.|
## EventType
Enumerates the events.
| Name| Value| Description|
| -------- | -------- | -------- |
| START_DOCUMENT | 0 | Indicates a start document event.|
| END_DOCUMENT | 1 | Indicates an end document event.|
| START_TAG | 2 | Indicates a start tag event.|
| END_TAG | 3 | Indicates an end tag event.|
| TEXT | 4 | Indicates a text event.|
| CDSECT | 5 | Indicates a CDATA section event.|
| COMMENT | 6 | Indicates an XML comment event.|
| DOCDECL | 7 | Indicates an XML document type declaration event.|
| INSTRUCTION | 8 | Indicates an XML processing instruction event.|
| ENTITY_REFERENCE | 9 | Indicates an entity reference event.|
| WHITESPACE | 10 | Indicates a whitespace character event.|