中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
怎样实现XML标签和标签值的解析?
微信扫码分享
import { xml, util } from '@kit.ArkTS'; // 需要使用util模块函数对文件编码 let strXml: string = '<?xml version="1.0" encoding="utf-8"?>' + '<note importance="high" logged="true">' + '<title>Play</title>' + '<lens>Work</lens>' + '</note>'; let textEncoder: util.TextEncoder = new util.TextEncoder(); let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer as object as ArrayBuffer, 'UTF-8'); let str: string = ''; function func(name: string, value: string): boolean { str = name + value; console.info(str); return true; //true:继续解析 false:停止解析 } let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func}; that.parse(options);