使用javascript查找xml属性值
如何使用Javascript/jQuery获取XML节点的属性值 我试图获取节点上duration属性的值,然后获取fixedValue使用javascript查找xml属性值,javascript,jquery,xml,xml-parsing,Javascript,Jquery,Xml,Xml Parsing,如何使用Javascript/jQuery获取XML节点的属性值 我试图获取节点上duration属性的值,然后获取fixedValue <loanAmount> <interestRates> <interestRate allowInterestOnly="true" type="variable" value="5.82"/> <interestRate allowFixed="true" allowInte
<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);
};
var currentLoanRates=函数(){
var currLoanXml=“”,
xmlDoc=$.parseXML(currLoanXml),
$xml=$(xmlDoc),
$intRate=$xml.find(“interestRate”),
$Varientrate=$intRate.attr(“固定值”);
控制台日志($intRate);
控制台日志($varIntRate);
};
第二个console.log打印未定义。我遇到的第一个问题是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);
}
});
};
var currentLoanRates=函数(){
var currLoanXml=“”,
xmlDoc=$.parseXML(currLoanXml),
$xml=$(xmlDoc),
$intRate=$xml.find(“interestRate”);
$intRate.each(函数(索引,元素){
if(element.attributes[“duration”]){
log(“持续时间:+element.attributes[“持续时间”].value);
}
if(element.attributes[“fixedValue”]){
log(“固定值:”+element.attributes[“fixedValue”].value);
}
});
};
对于那些希望从外部文件加载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>
$.ajax({
url:'sample.xml',
数据类型:“xml”,
成功:功能(数据){
//从XML中提取相关数据
var xml_node=$('loanAmount',数据);
$parameter=xml_node.find('interestRate');
$parameter.each(函数(索引、元素){
if(element.attributes[“allowFixed”]){
log(“允许固定:”+element.attributes[“allowFixed”].value);
}
if(element.attributes[“duration”]){
log(“持续时间:+element.attributes[“持续时间”].value);
}
});
},
错误:函数(数据){
log(“加载XML数据时出错”);
}
});
谢谢Ramesh-这非常有助于我走上正轨。