2012-07-24 101 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>