Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  118] [ 1]  / answers: 1 / hits: 25679  / 12 Years ago, tue, july 24, 2012, 12:00:00

How do I get the attribute value of an XML node with Javascript / jQuery?



I'm trying to get the value of the duration attribute on the node, then get the 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>'


So far I've got:



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);
};


The second console.log prints undefined.


More From » jquery

 Answers
13

The first problem I ran into is that currLoadXml is not a string. It needs to be wrapped inside single quotes.



Try using the below method



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);
}
});

};

[#84073] Sunday, July 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ezequiel

Total Points: 67
Total Questions: 96
Total Answers: 119

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
;