• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 to write different types of content. For example, call **startElement(name: string)** to write the 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/js-apis-xml.md).
24
25The following steps walk you through on how to generate an XML file.
26
271. Import the modules.
28
29   ```js
30   import xml from '@ohos.xml';
31   import util from '@ohos.util';
32   ```
33
342. Create a buffer and create an **XmlSerializer** object, either based on an object of the ArrayBuffer or DataView type.
35
36   ```js
37   // 1. Create an XmlSerializer object based on an object of the ArrayBuffer type.
38   let arrayBuffer = new ArrayBuffer(2048); // Create a 2048-byte object of the ArrayBuffer type.
39   let thatSer = new xml.XmlSerializer (arrayBuffer); // Create an XmlSerializer object based on the object of the ArrayBuffer type.
40
41   // 2. Create an XmlSerializer object based on an object of the DataView type.
42   let arrayBuffer = new ArrayBuffer(2048); // Create a 2048-byte object of the ArrayBuffer type.
43   let dataView = new DataView(arrayBuffer); // Use an object of the DataView type to operate the object of the ArrayBuffer type.
44   let thatSer = new xml.XmlSerializer (dataView); // Create an XmlSerializer object based on the object of the DataView type.
45   ```
46
473. Call the functions to generate an XML file.
48
49   ```js
50   thatSer.setDeclaration(); // Write the XML file declaration.
51   thatSer.startElement('bookstore'); // Write the start flag.
52   thatSer.startElement('book'); // Write the start tag of a nested element.
53   thatSer.setAttributes('category', 'COOKING'); // Write the attributes and attribute values.
54   thatSer.startElement('title');
55   thatSer.setAttributes('lang', 'en');
56   thatSer.setText('Everyday'); // Write the tag value.
57   thatSer.endElement(); // Write the end flag.
58   thatSer.startElement('author');
59   thatSer.setText('Giada');
60   thatSer.endElement();
61   thatSer.startElement('year');
62   thatSer.setText('2005');
63   thatSer.endElement();
64   thatSer.endElement();
65   thatSer.endElement();
66   ```
67
684. Use **Uint8Array** to operate the object of the ArrayBuffer type, and use **TextDecoder** to decode the Uint8Array.
69
70   ```js
71   let view = new Uint8Array(arrayBuffer); // Use Uint8Array to read data from the object of the ArrayBuffer type.
72   let textDecoder = util.TextDecoder.create(); // Call the TextDecoder class of the util module.
73   let res = textDecoder.decodeWithStream (view); // Decode the view.
74   console.info(res);
75   ```
76
77   The output is as follows:
78
79
80   ```js
81   <?xml version=\"1.0\" encoding=\"utf-8\"?><bookstore>\r\n  <book category=\"COOKING\">\r\n    <title lang=\"en\">Everyday</title>\r\n    <author>Giada</author>\r\n    <year>2005</year>\r\n  </book>\r\n</bookstore>
82   ```
83