1# XML Generation
2
3
4XML can be used as a data exchange format, which is supported by a wealth of systems and applications. For example, web services can transfer structured data in XML format.
5
6
7XML can also be used as a message passing format for communication between nodes in a distributed system.
8
9
10## Precautions
11
12- XML tags must appear in pairs: one start tag and one end tag.
13
14- XML tags are case sensitive. The start tag and end tag must use the same case.
15
16
17## How to Develop
18
19The **xml** module provides the **XmlSerializer** class to generate XML files. The input is an object of the ArrayBuffer or DataView type with a fixed length, which is used to store the output XML data.
20
21You can call different methods to write different types of content. For example, call **startElement(name: string)** to write a start tag and **setText(text: string)** to write a tag value.
22
23For details about the APIs of the **XML** module, see [@ohos.xml (XML Parsing and Generation)](../reference/apis-arkts/js-apis-xml.md).<br>To generate an XML file, proceed as follows:
24
251. Import the modules.
26
27   ```ts
28   import { xml, util } from '@kit.ArkTS';
29   ```
30
312. Create a buffer and construct an **XmlSerializer** object, either based on an object of the ArrayBuffer or DataView type.
32
33   ```ts
34   // Method 1: Create an XmlSerializer object based on an object of the ArrayBuffer type.
35   let arrayBuffer: ArrayBuffer = new ArrayBuffer(2048); // Create a 2048-byte object of the ArrayBuffer type.
36   let thatSer: xml.XmlSerializer = new xml.XmlSerializer(arrayBuffer); // Create an XmlSerializer object based on the object of the ArrayBuffer type.
37
38   // Method 2: Create an XmlSerializer object based on an object of the DataView type.
39   // let arrayBuffer: ArrayBuffer = new ArrayBuffer(2048);
40   // let dataView: DataView = new DataView(arrayBuffer);
41   // let thatSer: xml.XmlSerializer = new xml.XmlSerializer(dataView);
42   ```
43
443. Call the functions to generate an XML file.
45
46   ```ts
47   thatSer.setDeclaration(); // Write the XML file declaration.
48   thatSer.startElement('bookstore'); // Write the start tag of an element.
49   thatSer.startElement('book'); // Write the start tag of a nested element.
50   thatSer.setAttributes('category', 'COOKING'); // Write the attributes and attribute values.
51   thatSer.startElement('title');
52   thatSer.setAttributes('lang', 'en');
53   thatSer.setText('Everyday'); // Write the tag value.
54   thatSer.endElement(); // Write the end flag.
55   thatSer.startElement('author');
56   thatSer.setText('Giana');
57   thatSer.endElement();
58   thatSer.startElement('year');
59   thatSer.setText('2005');
60   thatSer.endElement();
61   thatSer.endElement();
62   thatSer.endElement();
63   ```
64
654. Use **Uint8Array** to operate the object of the ArrayBuffer type, and use **TextDecoder** to decode the Uint8Array.
66
67   ```ts
68   let view: Uint8Array = new Uint8Array(arrayBuffer); // Use Uint8Array to read data from the object of the ArrayBuffer type.
69   let textDecoder: util.TextDecoder = util.TextDecoder.create(); // Call the TextDecoder class of the util module.
70   let res: string = textDecoder.decodeToString(view); // Decode the view.
71   console.info(res);
72   ```
73
74   The output is as follows:
75
76   ```
77   <?xml version=\"1.0\" encoding=\"utf-8\"?><bookstore>\r\n  <book category=\"COOKING\">\r\n    <title lang=\"en\">Everyday</title>\r\n    <author>Giana</author>\r\n    <year>2005</year>\r\n  </book>\r\n</bookstore>
78   ```
79