fast-xml-parse解析xml-丢失大整数精度

程吕恭
2023-12-01

解决

改用xml2js解析xml。

import * as xml2js from 'xml2js';
export const xmlToJson = (xml: string) => {
  let xmlJson = '';
  xml2js.parseString(xml, { explicitArray: false }, function (err, json) {
    xmlJson = json;
  });
  return xmlJson;
};

注意,xml2js解析出来的是键值对是数组

explicitArray (default: true): Always put child nodes in an array if true; otherwise an array is created only if there is more than one.

添加 { explicitArray: false }

此时解析出来的全是对象,数组格式被破坏.还需要自己写方法转换。

其他

js丢失精度思考:把整数转为string。

 类似资料: