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). 24 25To generate an XML file, proceed as follows: 26 271. Import the modules. 28 29 ```ts 30 import { xml, util } from '@kit.ArkTS'; 31 ``` 32 332. Create a buffer and create an **XmlSerializer** object, either based on an object of the ArrayBuffer or DataView type. 34 35 ```ts 36 // 1. Create an XmlSerializer object based on an object of the ArrayBuffer type. 37 let arrayBuffer: ArrayBuffer = new ArrayBuffer(2048); // Create a 2048-byte object of the ArrayBuffer type. 38 let thatSer: xml.XmlSerializer = new xml.XmlSerializer(arrayBuffer); // Create an XmlSerializer object based on the object of the ArrayBuffer type. 39 40 // 2. Create an XmlSerializer object based on an object of the DataView type. 41 let arrayBuffer: ArrayBuffer = new ArrayBuffer(2048); // Create a 2048-byte object of the ArrayBuffer type. 42 let dataView: DataView = new DataView(arrayBuffer); // Use an object of the DataView type to operate the object of the ArrayBuffer type. 43 let thatSer: xml.XmlSerializer = new xml.XmlSerializer(dataView); // Create an XmlSerializer object based on the object of the DataView type. 44 ``` 45 463. Call the functions to generate an XML file. 47 48 ```ts 49 thatSer.setDeclaration(); // Write the XML file declaration. 50 thatSer.startElement('bookstore'); // Write the start tag of an element. 51 thatSer.startElement('book'); // Write the start tag of a nested element. 52 thatSer.setAttributes('category', 'COOKING'); // Write the attributes and attribute values. 53 thatSer.startElement('title'); 54 thatSer.setAttributes('lang', 'en'); 55 thatSer.setText('Everyday'); // Write the tag value. 56 thatSer.endElement(); // Write the end flag. 57 thatSer.startElement('author'); 58 thatSer.setText('Giana'); 59 thatSer.endElement(); 60 thatSer.startElement('year'); 61 thatSer.setText('2005'); 62 thatSer.endElement(); 63 thatSer.endElement(); 64 thatSer.endElement(); 65 ``` 66 674. Use **Uint8Array** to operate the object of the ArrayBuffer type, and use **TextDecoder** to decode the Uint8Array. 68 69 ```ts 70 let view: Uint8Array = new Uint8Array(arrayBuffer); // Use Uint8Array to read data from the object of the ArrayBuffer type. 71 let textDecoder: util.TextDecoder = util.TextDecoder.create(); // Call the TextDecoder class of the util module. 72 let res: string = textDecoder.decodeToString(view); // Decode the view. 73 console.info(res); 74 ``` 75 76 The output is as follows: 77 78 ``` 79 <?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> 80 ``` 81