javascript - 使用 javascript 查找 xml 属性值

标签 javascript jquery xml xml-parsing

如何使用 Javascript/jQuery 获取 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 打印undefined

最佳答案

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

};

关于javascript - 使用 javascript 查找 xml 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11624448/

相关文章:

javascript - 由正则表达式文字创建的 RegExp 对象共享一个实例?

javascript - 我的 AJAX 调用需要多长时间?

javascript - Bootstrap 可折叠面板 - 从打开的部分开始

android - 如何在 Material 按钮上写多行文本?

javascript - 将远程 XML 加载到网页中并通过 JavaScript 访问其内容

javascript - Angular ui路由器不区分大小写的查询参数

javascript - 异步等待和可共享的 MongoDB 连接

javascript - 截图 Ionic 框架

jquery - 代码在 jQuery 2.1.4 中失败,但在旧版本中运行良好

wpf - 将多重绑定(bind)放在 xaml 中的一行上