ArkTS XML解析

直接调用 xml.parseXML 来对数据 XML 解析,特别需要注意的是 listNames 这个参数。代码如下:

import xmlParser from '@ohos.xml';
import util from '@ohos.util';

namespace xml {
  export function test() {
    let strXml =
      '' +
      '' +
      'Everyday' +
      'Giada' +
      'GiadabbbbGiadabbbb' +
      '';
    let result = xml.parseXML(strXml);
    // let listNames: string[] = ["list"];
    // let result = xml.parseXML(strXml, ...listNames);
    let temp;
  }
  /**
   *
   * @param strXml
   * @param listNames 如果有列表,请加上此参数
   * @returns
   */
  export function parseXML(strXml: string, ...listNames: string[]) {
    let textEncoder = new util.TextEncoder();
    let arrBuffer = textEncoder.encodeInto(strXml);
    let that = new xmlParser.XmlPullParser(arrBuffer.buffer, 'UTF-8');
    let defaultAttrName: string = "text";
    let list: Object[] = [];
    let tempListName: string[] = [];
    that.parse({
      supportDoctype: true,
      ignoreNameSpace: true,
      tagValueCallbackFunction: (name: string, value: string) => {
        if (listNames && listNames.indexOf(name) >= 0) {
          tempListName.push(null);
          list.push([]);
        } else {
          tempListName.push(name);
          list.push(new Object());
          if (tempListName.length > 1 && tempListName[tempListName.length-2] == null) {
            (list[tempListName.length-2] as Object[]).push(list[list.length-1]);
          }
        }
        return true;
      },
      attributeValueCallbackFunction: (name: string, value: string) => {
        list[list.length-1][name] = value;
        return true;
      },
      tokenValueCallbackFunction: (eventType: xmlParser.EventType, value: xmlParser.ParseInfo) => {
        if (eventType == xmlParser.EventType.START_DOCUMENT) {
        } else if (eventType == xmlParser.EventType.END_DOCUMENT) {
        } else if (eventType == xmlParser.EventType.START_TAG) {
        } else if (eventType == xmlParser.EventType.END_TAG) {
          if (list.length > 1) {
            if (listNames && listNames.length > 0) {
              let ts = tempListName.splice(list.length - 1, 1);
              let curData = list.splice(list.length - 1, 1);
              if (ts[0] != null) {
                if (!(list[list.length-1] instanceof Array)) {
                  list[list.length-1][value.getName()] = curData[0];
                }
              } else {
                if (!(list[list.length-1] instanceof Array)) {
                  list[list.length-1][value.getName()] = curData[0];
                } else {
                  (list[list.length-1] as Object[]).push(curData[0]);
                }
              }
            } else {
              let curData = list.splice(list.length - 1, 1);
              let tempData = list[list.length-1][value.getName()];
              if (tempData) {
                if (tempData instanceof Array) {
                  (tempData as Object[]).push(curData[0]);
                } else {
                  list[list.length-1][value.getName()] = [tempData, curData[0]];
                }
              } else {
                list[list.length-1][value.getName()] = curData[0];
              }
            }
          }
        } else if (eventType == xmlParser.EventType.TEXT) {
          list[list.length-1][defaultAttrName] = value.getText();
        }
        return true;
      }
    });
    return list[0];
  }
}

export default xml;

你可能感兴趣的:(ArkTS,HarmonyOS,鸿蒙,harmonyos)