中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
ArkTS如何生成xml?
微信扫码分享
import xml from '@ohos.xml'; import util from '@ohos.util'; // 1.基于Arraybuffer构造XmlSerializer对象 let arrayBuffer: ArrayBuffer = new ArrayBuffer(2048); // 创建一个2048字节的缓冲区 let thatSer: xml.XmlSerializer = new xml.XmlSerializer(arrayBuffer); // 基于Arraybuffer构造XmlSerializer对象 @Entry @Component struct Index { build() { Column() { Button("生成xml").onClick(() => { 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(); }) } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) } }