HarmonyOS 如何解析xml文件

支持xml文件解析吗,最好能提供示例代码。

HarmonyOS
2024-12-23 16:22:13
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

支持的,参考示例:

import { xml, util } from '@kit.ArkTS';

let strXml: string =
  '<?xml version="1.0" encoding="UTF-8"?>' +
    '<book category="COOKING">' +
    '<title lang="en">Everyday</title>' +
    '<string-array name="tune_B_alphabet"><item><sup><small><small>b</small></small></sup>d</item></string-array>'
'</book>';
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 tagFunc(name: string, value: string): boolean {
  str = name + value;
  console.info('tag-' + str);
  return true;
}

function attFunc(name: string, value: string): boolean {
  str = name + ' ' + value;
  console.info('attri-' + str);
  return true;
}

function tokenFunc(name: xml.EventType, value: xml.ParseInfo): boolean {
  str = name + ' ' + value.getDepth();
  console.info('token-' + str);
  return true;
}

let options: xml.ParseOptions = {
  supportDoctype: true,
  ignoreNameSpace: true,
  tagValueCallbackFunction: tagFunc,
  attributeValueCallbackFunction: attFunc,
  tokenValueCallbackFunction: tokenFunc
};
that.parse(options);

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Button('解析').onClick(() => {
      that.parse(options)
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
分享
微博
QQ
微信
回复
2024-12-23 18:33:05
相关问题
HarmonyOS 如何解析zip文件
665浏览 • 1回复 待解决
HarmonyOS 如何解析证书文件
884浏览 • 1回复 待解决
HarmonyOS xml解析失败
618浏览 • 1回复 待解决
HarmonyOS xml解析异常
778浏览 • 1回复 待解决
HarmonyOS xml解析工具
736浏览 • 1回复 待解决
xml文件数据解析 ,都有哪些步骤?
1549浏览 • 1回复 待解决
鸿蒙 | Jar包中解析xml文件为空值。
8368浏览 • 1回复 待解决
HarmonyOS 如何解析HTML文本
1391浏览 • 1回复 待解决
HarmonyOS 如何解析json字符串?
1924浏览 • 1回复 待解决