简体   繁体   English

使用 javascript 查找 xml 属性值

[英]Find xml attribute values with javascript

How do I get the attribute value of an XML node with Javascript / jQuery?如何使用 Javascript/jQuery 获取 XML 节点的属性值?

I'm trying to get the value of the duration attribute on the node, then get the fixedValue.我正在尝试获取节点上持续时间属性的值,然后获取 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 .第二个 console.log 打印undefined

The first problem I ran into is that currLoadXml is not a string.我遇到的第一个问题是 currLoadXml 不是字符串。 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);
        }
    });

};

For those looking out for loading the XML from an external file, this could be of some help:对于那些希望从外部文件加载 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM