HarmonyOS xml解析异常

以下xml数据,调用convertToJSObject方法解析出来的数据是undefined。

<XMLBODY>
  <Map id="opdata">
  <Field id="CloudTipsResponseCode">0</Field>
  <Map id="cloudKey">
  <Field id="CLOUDID">8888202108174408</Field>
  <Field id="URL"><![CDATA[1801]]></Field>
  <Field id="URLTYPE">XLT1801</Field>
  <Field id="ALERTTEXT"><![CDATA[9]]></Field>
  <Field id="PRODTYPE">理财¥5m1&8R6e¥理财</Field>
  <Map id="ACTION">
  <Field id="forceLoginFlag"></Field>
  <Field id="targetMenuID"></Field>
  <Field id="targetParams"><![CDATA[]]></Field>
  </Map>
  <Field id="ACTIONTYPE">native_returnToTargetMenu</Field>
  </Map>
  </Map>
  </XMLBODY>

xml解析代码如下:

let conv = new convertXml.ConvertXML()
let obj: object = conv.convertToJSObject(xmlStr, convertOptions)
let elements: Element[] = obj['_elements_'] as Element[]

完整xml解析代码如下:

import convertXml from '@ohos.convertxml';

interface Attributes {
  id: string
}

interface Element {
  _type_: string;
  _name_: string;
  _text_?: string;
  _cdata_?: string;
  _attributes_?: Attributes;
  _elements_: Element[]
}

let convertOptions: convertXml.ConvertOptions = {
  trim: true,
  declarationKey: "_declaration_",
  instructionKey: "_instruction_",
  attributesKey: "_attributes_",
  textKey: "_text_",
  cdataKey: "_cdata_",
  doctypeKey: "_doctype_",
  commentKey: "_comment_",
  parentKey: "_parent_",
  typeKey: "_type_",
  nameKey: "_name_",
  elementsKey: "_elements_"
}

function depXml(elements: Element[], rootName?: string): string | object {
  let resultXml: string | object = new Object();
  if (!elements) {
    return '';
  }

  // 解析xml中数组,只能解决数组中元素相同,没有attributes的场景
  if (elements.length > 1 && elements.every(item => item._type_ === 'element'
    && item._name_ === elements[0]._name_ && !item._attributes_)) {
    let resultArray: object[] = new Array()
    elements.forEach(item => {
      let itemObj = depXml(item._elements_) as object
      resultArray.push(itemObj)
    });
    return resultArray
  }

  elements.forEach(item => {
    if (rootName && item._name_ === rootName) {
      resultXml = depXml(item._elements_)
      return;
    }
    if (item._type_ === 'element' && item._attributes_) {
      resultXml[item._attributes_.id] = depXml(item._elements_)
    } else if(item._type_ === 'element' && item._name_ && item._elements_) {
      resultXml[item._name_] = depXml(item._elements_)
    } else if (item._type_ === 'text') {
      if (item._text_ != undefined) {
        resultXml = item._text_
      }
    } else if (item._type_ === 'cdata') {
      try {
        if (item._cdata_ != undefined) {
          if (!isNumeric(item._cdata_)) {
            resultXml = JSON.parse(item._cdata_)
          }else {
            resultXml = item._cdata_
          }
        }
      } catch (e) {
        if (item._cdata_ != undefined) {
          resultXml = item._cdata_
        }
      }
    }
  })
  return resultXml;
}

export default class ParseXML {
  static jsonFromXML(xmlStr: string): string {
    try {
      let conv = new convertXml.ConvertXML()
      let obj: object = conv.convertToJSObject(xmlStr, convertOptions)
      let elements: Element[] = obj['_elements_'] as Element[]
      let rootElementName = elements[0]._name_
      // 解析xml
      let res = depXml(elements, rootElementName)
      let responseData = JSON.stringify(res)
      return responseData
    } catch (e) {
      throw e as Error
    }
  }
}

// 判断字符串是否纯数字,由于大数转json精度丢失,纯数字时不转json
function isNumeric(str: string): boolean {
  return /^\d+$/.test(str)
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

根据提供的xml发现存在需要替换的实体引用&,具体文档参考文档的实体部分:https://developer.mozilla.org/zh-CN/docs/Web/XML/XML_introduction

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 如何解析xml文件
69浏览 • 1回复 待解决
HarmonyOS json解析异常
540浏览 • 1回复 待解决
xml文件数据解析 ,都有哪些步骤?
799浏览 • 1回复 待解决
怎样实现XML标签和标签值的解析
346浏览 • 1回复 待解决
鸿蒙 | Jar包中解析xml文件为空值。
7470浏览 • 1回复 待解决
HarmonyOS 有没有xml描述的Shape?
51浏览 • 1回复 待解决