[Mid-Autumn Festival and National Day constantly updated] XML generation, parsing and conversion in HarmonyOS (Part 1)

1. XML Overview

XML (Extensible Markup Language) is a markup language for describing data, designed to provide a common way to transmit and store data, especially data frequently used in web applications. XML does not have predefined tags. Therefore, XML is more flexible and can be applied to a wide range of applications.

XML documents are composed of elements, attributes and content.

  • Elements are pairs of tags that contain text, attributes, or other elements.
  • Properties provide additional information about the element.
  • Content is the data or sub-elements contained by the element.

XML can also define the document structure by using XML Schema or DTD (Document Type Definition). These mechanisms allow developers to create custom rules to verify that XML documents conform to their expected format.

XML also supports features such as namespaces, entity references, annotations, and processing instructions, allowing it to flexibly adapt to various data needs.

The language basic class library provides basic capabilities related to XML, including: XML generation , XML parsing and XML conversion .

XML can be used as a data exchange format and is supported by various systems and applications. For example, Web services can transmit structured data in XML format.

XML can also be used as a message passing format for communication and interaction between different nodes in distributed systems.

2. XML generation

Precautions

  • XML tags must appear in pairs. To generate a start tag, you must generate an end tag.
  • XML tags are case-sensitive, and the start tag and end tag must have the same case.

Development steps

The XML module provides the XmlSerializer class to generate XML files. The input is a fixed-length Arraybuffer or DataView object, which is used to store the output XML data.

Write different contents by calling different methods, such as startElement(name: string) to write the element start tag, and setText(text: string) to write the tag value.

For the API interface of the XML module, please refer to the detailed description of @ohos.xml . A complete XML file can be generated by calling the corresponding function as required.

  1. Import modules.
import xml from '@ohos.xml'; 
import util from '@ohos.util';

       2. Create a buffer and construct an XmlSerializer object (an XmlSerializer object can be constructed based on Arraybuffer or an XmlSerializer object based on DataView).

// 1.基于Arraybuffer构造XmlSerializer对象
let arrayBuffer = new ArrayBuffer(2048); // 创建一个2048字节的缓冲区
let thatSer = new xml.XmlSerializer(arrayBuffer); // 基于Arraybuffer构造XmlSerializer对象

// 2.基于DataView构造XmlSerializer对象
let arrayBuffer = new ArrayBuffer(2048); // 创建一个2048字节的缓冲区
let dataView = new DataView(arrayBuffer); // 使用DataView对象操作ArrayBuffer对象
let thatSer = new xml.XmlSerializer(dataView); // 基于DataView构造XmlSerializer对象

    4. Call the XML element generation function.

thatSer.setDeclaration(); // 写入xml的声明
thatSer.startElement('bookstore'); // 写入元素开始标记
thatSer.startElement('book'); // 嵌套元素开始标记
thatSer.setAttributes('category', 'COOKING'); // 写入属性及属性值
thatSer.startElement('title');
thatSer.setAttributes('lang', 'en');
thatSer.setText('Everyday'); // 写入标签值
thatSer.endElement(); // 写入结束标记
thatSer.startElement('author');
thatSer.setText('Giada');
thatSer.endElement();
thatSer.startElement('year');
thatSer.setText('2005');
thatSer.endElement();
thatSer.endElement();
thatSer.endElement();

    5. Use Uint8Array to operate the Arraybuffer, call TextDecoder to decode the Uint8Array and output it.

let view = new Uint8Array(arrayBuffer); // 使用Uint8Array读取arrayBuffer的数据
let textDecoder = util.TextDecoder.create(); // 调用util模块的TextDecoder类
let res = textDecoder.decodeWithStream(view); // 对view解码
console.info(res);

The output is as follows:

<?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>

Guess you like

Origin blog.csdn.net/HarmonyOSDev/article/details/133312710