2012-07-24 71 views
3

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

我想获取节点上的持续时间属性的值,然后获取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打印不确定

回答

4

我遇到的第一个问题是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); 
     } 
    }); 

}; 
+0

感谢拉梅什 - 这非常有助于把我放在正确的轨道上。 – timmackay 2012-07-24 06:10:12

0

对于那些寻找出从外部文件加载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>