Trouver les valeurs d'attribut xml avec javascript
Comment puis-je obtenir la valeur de l'attribut d'un nœud XML, Javascript /jQuery?
Je vais essayer d'obtenir la valeur de l'attribut de durée sur le nœud, puis obtenir le 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>'
Pour l'instant j'ai:
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);
};
La deuxième console.journal imprime undefined.
OriginalL'auteur timmackay | 2012-07-24
Vous devez vous connecter pour publier un commentaire.
Le premier problème que j'ai rencontré est que currLoadXml n'est pas une chaîne. Il doit être enveloppé à l'intérieur des guillemets simples.
Essayez d'utiliser la méthode ci-dessous
OriginalL'auteur Ramesh
Pour ceux à la recherche pour le chargement du XML à partir d'un fichier externe, ce qui pourrait être de quelque secours:
OriginalL'auteur Ayush Nigam