1e41f4b71Sopenharmony_ci# XML Generation
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciXML 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.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciXML can also be used as a message passing format for communication between nodes in a distributed system.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## Precautions
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci- XML tags must appear in pairs: one start tag and one end tag.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci- XML tags are case sensitive. The start tag and end tag must use the same case.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## How to Develop
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciThe **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.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciYou 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. 
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciFor details about the APIs of the **XML** module, see [@ohos.xml (XML Parsing and Generation)](../reference/apis-arkts/js-apis-xml.md).
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ciTo generate an XML file, proceed as follows:
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci1. Import the modules.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci   ```ts
30e41f4b71Sopenharmony_ci   import { xml, util } from '@kit.ArkTS';
31e41f4b71Sopenharmony_ci   ```
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci2. Create a buffer and create an **XmlSerializer** object, either based on an object of the ArrayBuffer or DataView type.
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci   ```ts
36e41f4b71Sopenharmony_ci   // 1. Create an XmlSerializer object based on an object of the ArrayBuffer type.
37e41f4b71Sopenharmony_ci   let arrayBuffer: ArrayBuffer = new ArrayBuffer(2048); // Create a 2048-byte object of the ArrayBuffer type.
38e41f4b71Sopenharmony_ci   let thatSer: xml.XmlSerializer = new xml.XmlSerializer(arrayBuffer); // Create an XmlSerializer object based on the object of the ArrayBuffer type.
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci   // 2. Create an XmlSerializer object based on an object of the DataView type.
41e41f4b71Sopenharmony_ci   let arrayBuffer: ArrayBuffer = new ArrayBuffer(2048); // Create a 2048-byte object of the ArrayBuffer type.
42e41f4b71Sopenharmony_ci   let dataView: DataView = new DataView(arrayBuffer); // Use an object of the DataView type to operate the object of the ArrayBuffer type.
43e41f4b71Sopenharmony_ci   let thatSer: xml.XmlSerializer = new xml.XmlSerializer(dataView); // Create an XmlSerializer object based on the object of the DataView type.
44e41f4b71Sopenharmony_ci   ```
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci3. Call the functions to generate an XML file.
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci   ```ts
49e41f4b71Sopenharmony_ci   thatSer.setDeclaration(); // Write the XML file declaration.
50e41f4b71Sopenharmony_ci   thatSer.startElement('bookstore'); // Write the start tag of an element.
51e41f4b71Sopenharmony_ci   thatSer.startElement('book'); // Write the start tag of a nested element.
52e41f4b71Sopenharmony_ci   thatSer.setAttributes('category', 'COOKING'); // Write the attributes and attribute values.
53e41f4b71Sopenharmony_ci   thatSer.startElement('title');
54e41f4b71Sopenharmony_ci   thatSer.setAttributes('lang', 'en');
55e41f4b71Sopenharmony_ci   thatSer.setText('Everyday'); // Write the tag value.
56e41f4b71Sopenharmony_ci   thatSer.endElement(); // Write the end flag.
57e41f4b71Sopenharmony_ci   thatSer.startElement('author');
58e41f4b71Sopenharmony_ci   thatSer.setText('Giana');
59e41f4b71Sopenharmony_ci   thatSer.endElement();
60e41f4b71Sopenharmony_ci   thatSer.startElement('year');
61e41f4b71Sopenharmony_ci   thatSer.setText('2005');
62e41f4b71Sopenharmony_ci   thatSer.endElement();
63e41f4b71Sopenharmony_ci   thatSer.endElement();
64e41f4b71Sopenharmony_ci   thatSer.endElement();
65e41f4b71Sopenharmony_ci   ```
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci4. Use **Uint8Array** to operate the object of the ArrayBuffer type, and use **TextDecoder** to decode the Uint8Array.
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci   ```ts
70e41f4b71Sopenharmony_ci   let view: Uint8Array = new Uint8Array(arrayBuffer); // Use Uint8Array to read data from the object of the ArrayBuffer type.
71e41f4b71Sopenharmony_ci   let textDecoder: util.TextDecoder = util.TextDecoder.create(); // Call the TextDecoder class of the util module.
72e41f4b71Sopenharmony_ci   let res: string = textDecoder.decodeToString(view); // Decode the view.
73e41f4b71Sopenharmony_ci   console.info(res);
74e41f4b71Sopenharmony_ci   ```
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci   The output is as follows:
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci   ```
79e41f4b71Sopenharmony_ci   <?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>
80e41f4b71Sopenharmony_ci   ```
81