0

继承了需要一些维护的 Node.js 应用程序,这不是我的强项。

我们正在使用 fast-xml-parser 解析 XML,它对我们的大多数输入都非常有效。然而,我们有一些输入是额外的深度,我们需要将输出扁平化为相同的水平。

输入:其中价格值是额外的深度

<products capture-installed="true">
<script/>
<script/>
<script/>
<product>
<pid>8</pid>
<modelno>6273033</modelno>
<name>
<![CDATA[ Big Red Truck ]]>
</name>
<category>
<![CDATA[ Toys]]>
</category>
<currency>USD</currency>
<price>
  <actualprice>19.20</actualprice>
</price>
</product>

当我们用现有代码将其展平时,我们得到:

   "product": {
      "pid": "8",
      "modelno": "6273033",
      "name": "Big Red Truck",
      "category": "Toys",
      "currency": "USD",
      "price": {
         "actualprice": "19.20"
      }

但我们需要的是这样的:

   "product": {
      "pid": "8",
      "modelno": "6273033",
      "name": "Big Red Truck",
      "category": "Toys",
      "currency": "USD",
      "price-actualprice": "19.20"
      }

当前代码:

const parse = require("fast-xml-parser");

const options = {
  ignoreAttributes : true,
  ignoreNameSpace : false,
  parseNodeValue : false,
  tagValueProcessor : a => {
    if(Array.isArray(a)){
      return a.join(',');
    }
    return a;
  }
};

const flatten = (data) => {
  return data.map(row => {
    const fieldNames = Object.keys(row);
    for (const fieldName of fieldNames) {
      if(Array.isArray(row[fieldName])){
        row[fieldName] = row[fieldName].join(',');
      }

      if(typeof row[fieldName] === 'object'){
        row[fieldName] = JSON.stringify(row[fieldName]);
      }
    }
    return row;
  });
};


function findTheArray(o) {
  if(Array.isArray(o)){
    return o;
  }
  var result, p; 
  for (p in o) {
      if( o.hasOwnProperty(p) && typeof o[p] === 'object' ) {
          result = findTheArray(o[p]);
          if(result){
              return result;
          }
      }
  }
  return result;
}


module.exports = function parseData(data) {
  return new Promise((resolve, reject) => {
    try {
      const isValid = parse.validate(data);
      if (isValid === true) {
        const pData = parse.parse(data, options);
        const array = findTheArray(pData);
        if(array){
          resolve(flatten(array));
        } else {
          reject('Can\'t find any goodies!');
        }
      } else {
        reject(isValid.err);
      }
    } catch (err) {
      reject(err);
    }
  });
};

我一直在代码的这个领域工作,但没有取得任何成功:

if(typeof row[fieldName] === 'object'){
        row[fieldName] = JSON.stringify(row[fieldName])

想法?谢谢

4

1 回答 1

0

在最新版本的 FXP 中,您可以这样做;

const options = {
    ignoreAttributes: true,
    stopNodes: [
        "products.product.price"
    ],
    tagValueProcessor: (tagName, tagValue, jPath, hasAttributes, isLeafNode) => {
        if (jPath === 'products.product.price') {
            return /([0-9]+\.[0-9]+)/.exec(tagValue)[1]
        }
    },
    // preserveOrder: true,
};
const parser = new XMLParser(options);
let result = parser.parse(XMLdata);

输出

"product": {
    "pid": "8",
    "modelno": "6273033",
    "name": "Big Red Truck",
    "category": "Toys",
    "currency": "USD",
    "price": "19.20"
}

但是,标签名称不能更改。

于 2021-12-13T01:31:53.460 回答