使用javascript提取XML值

时间:2017-08-18 10:36:16

标签: javascript arrays xml

我有以下XML和 我正在尝试使用java脚本从中提取数据,任何关于使用java脚本转换它的想法? 需要提供此XML中的值。

我的问题与前面提到的问题不同:

这个XML有多个子元素,这些子元素可以增长到' n'号。

除此之外,值必须以数组格式存储。



<MergeFields>
  <MergeItem>
    <Key>TotalPaymentAmountGST </Key>
    <Value>123.56</Value>
  </MergeItem>
  <MergeItem>
    <Key>TotalRefundAmountGST </Key>
    <Value>123.56 </Value>
  </MergeItem>
  <MergeItem>
    <Key>TotalPaymentAmount </Key>
    <Value>123.56 </Value>
  </MergeItem>
  <MergeItem>
    <Key>TotalRefundAmount </Key>
    <Value>123.56 </Value>
  </MergeItem>
  <MergeItem>
    <Key>StatementType </Key>
    <Value>1</Value>
  </MergeItem>

  <MergeItem>
    <Key>Transactions</Key>
    <Children>
      <MergeItem>
        <Key> 0 </Key>
        <Children>
          <MergeItem>
            <Key> PaymentDate </Key>
            <Value> 2017-07-31T03:41:02.537Z </Value>
          </MergeItem>
          <MergeItem>
            <Key> DatePaidTo </Key>
            <Value> 2017-07-31T03:41:02.537Z </Value>
          </MergeItem>
          <MergeItem>
            <Key> PaymentAmount </Key>
            <Value> 123.56 </Value>
          </MergeItem>
          <MergeItem>
            <Key>IsGSTAmount </Key>
            <Value>true</Value>
          </MergeItem>
          <MergeItem>
            <Key>RefundAmount</Key>
            <Value>123.56</Value>
          </MergeItem>
          <MergeItem>
            <Key>DishonouredAmount</Key>
            <Value>123.56</Value>
          </MergeItem>
        </Children>
      </MergeItem>
      <MergeItem>
        <Key> 1 </Key>
        <Children>
          <MergeItem>
            <Key>PaymentDate </Key>
            <Value>2017-07-31T03:41:02.537Z</Value>
          </MergeItem>
          <MergeItem>
            <Key>DatePaidTo </Key>
            <Value>2017-07-31T03:41:02.537Z</Value>
          </MergeItem>
          <MergeItem>
            <Key>PaymentAmount </Key>
            <Value>123.56</Value>
          </MergeItem>
          <MergeItem>
            <Key>IsGSTAmount</Key>
            <Value> true</Value>
          </MergeItem>
          <MergeItem>
            <Key> RefundAmount </Key>
            <Value> 123.56</Value>
          </MergeItem>
          <MergeItem>
            <Key>DishonouredAmount </Key>
            <Value> 123.56 </Value>
          </MergeItem>
        </Children>
      </MergeItem>
    </Children>
  </MergeItem>
</MergeFields>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

XML2js npm包应该可以提供帮助,同时查看this answer