【问题标题】:Find xml attribute values with javascript使用 javascript 查找 xml 属性值
【发布时间】:2012-07-22 09:24:53
【问题描述】:

如何使用 Javascript/jQuery 获取 XML 节点的属性值?

我正在尝试获取节点上duration属性的值,然后获取fixedValue。

<loanAmount>
    <interestRates>
        <interestRate allowInterestOnly="true" type="variable" value="5.82"/>
        <interestRate allowFixed="true" allowInterestOnly="true" duration="1" fixedInterestOnlyValue="5.7" fixedValue="5.7" variableInterestOnlyValue="5.82"/>
        <interestRate allowFixed="true" allowInterestOnly="true" duration="3" fixedInterestOnlyValue="5.75" fixedValue="5.75" variableInterestOnlyValue="5.82"/>
        <interestRate allowFixed="true" allowInterestOnly="true" duration="5" fixedInterestOnlyValue="6.64" fixedValue="6.56" variableInterestOnlyValue="5.82"/>
        <interestRate allowFixed="true" allowInterestOnly="true" duration="10" variableInterestOnlyValue="5.82"/>
    </interestRates>
</loanAmount>'

到目前为止,我得到了:

var currentLoanRates = function() {
    var currLoanXml = '<loanAmount><interestRates><interestRate allowInterestOnly="true" type="variable" value="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="1" fixedInterestOnlyValue="5.7" fixedValue="5.7" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="3" fixedInterestOnlyValue="5.75" fixedValue="5.75" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="5" fixedInterestOnlyValue="6.64" fixedValue="6.56" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="10" variableInterestOnlyValue="5.82"/></interestRates></loanAmount>',
    xmlDoc = $.parseXML( currLoanXml ),
    $xml = $( xmlDoc ),
    $intRate = $xml.find("interestRate"),
    $varIntRate = $intRate.attr("fixedValue");

    console.log($intRate);
    console.log($varIntRate);
};

第二个 console.log 打印 undefined

【问题讨论】:

    标签: javascript jquery xml xml-parsing


    【解决方案1】:

    对于那些希望从外部文件加载 XML 的人来说,这可能会有所帮助:

    <script>
        $.ajax({
            url: 'sample.xml',
            dataType: 'xml',
            success: function (data) {
                // Extract relevant data from XML
                var xml_node = $('loanAmount', data);
                $parameter = xml_node.find('interestRate');
                $parameter.each(function (index, element) {
                    if (element.attributes["allowFixed"]) {
                        console.log("Allow Fixed : " + element.attributes["allowFixed"].value);
                    }
    
                    if (element.attributes["duration"]) {
                        console.log("Duration: " + element.attributes["duration"].value);
                    }
                });
            },
            error: function (data) {
                console.log('Error loading XML data');
            }
        });
    </script>
    

    【讨论】:

      【解决方案2】:

      我遇到的第一个问题是 currLoadXml 不是字符串。它需要用单引号括起来。

      试试下面的方法

      var currentLoanRates = function() {
          var currLoanXml = '<loanAmount><interestRates><interestRate allowInterestOnly="true" type="variable" value="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="1" fixedInterestOnlyValue="5.7" fixedValue="5.7" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="3" fixedInterestOnlyValue="5.75" fixedValue="5.75" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="5" fixedInterestOnlyValue="6.64" fixedValue="6.56" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="10" variableInterestOnlyValue="5.82"/></interestRates></loanAmount>',
          xmlDoc = $.parseXML( currLoanXml ),
          $xml = $( xmlDoc ),
          $intRate = $xml.find("interestRate");
          $intRate.each(function(index, element) { 
              if(element.attributes["duration"]) {
                  console.log("Duration :" + element.attributes["duration"].value);
              }
      
              if(element.attributes["fixedValue"]) {
                  console.log("Fixed value:" + element.attributes["fixedValue"].value);
              }
          });
      
      };
      

      【讨论】:

      • 感谢 Ramesh - 这非常有助于我走上正轨。
      猜你喜欢
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 2016-12-28
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      相关资源
      最近更新 更多