按特定属性值循环XML元素

时间:2015-05-22 19:49:50

标签: javascript xml

XML代码:

<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="29.0000" PlanId="1" RoundDown="" TermMax="5" TierId="1" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="27.0000" PlanId="1" RoundDown="" TermMax="5" TierId="2" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="1" RoundDown="" TermMax="5" TierId="3" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.0000" PlanId="1" RoundDown="" TermMax="5" TierId="4" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="15.0000" PlanId="1" RoundDown="" TermMax="5" TierId="5" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="2" RoundDown="50" TermMax="5" TierId="6" TierMultiplier="60.61"/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.4000" PlanId="2" RoundDown="100" TermMax="5" TierId="7" TierMultiplier="71.52"/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="16.2000" PlanId="2" RoundDown="100" TermMax="5" TierId="8" TierMultiplier="92.61"/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="13.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="9" TierMultiplier="112.84"/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="10.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="10" TierMultiplier="143.30"/>
    <Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="24.0000" PlanId="3" RoundDown="50" TermMax="6" TierId="11" TierMultiplier="60.61"/>
    <Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="3" RoundDown="100" TermMax="7" TierId="12" TierMultiplier="71.52"/>
    <Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="3" RoundDown="100" TermMax="9" TierId="13" TierMultiplier="92.61"/>
    <Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="3" RoundDown="100" TermMax="11" TierId="14" TierMultiplier="112.84"/>
    <Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="3" RoundDown="100" TermMax="14" TierId="15" TierMultiplier="143.30"/>
    <Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="19.2000" PlanId="5" RoundDown="50" TermMax="6" TierId="17" TierMultiplier="68.06"/>
    <Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="5" RoundDown="100" TermMax="7" TierId="18" TierMultiplier="71.94"/>
    <Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="5" RoundDown="100" TermMax="9" TierId="19" TierMultiplier="92.61"/>
    <Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="5" RoundDown="100" TermMax="11" TierId="20" TierMultiplier="112.84"/>
    <Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="5" RoundDown="100" TermMax="14" TierId="21" TierMultiplier="143.30"/>

在我的javascript中,我试图遍历具有PlanId =&#34; 5&#34;属性的XML元素。只有然后将每个Plan元素的变量分配给其他属性,如下面的for循环代码中所示,但我不能得到PlanId =&#34; 5&#34;我上面的XML中的元素(最后5行)。任何帮助/方向将不胜感激。这是我尝试实现此目的的循环:

planId = 5;

for (var i = 1; i <= planIdCount; i++) {

    // Get Plan attributes for affordability calculations
    tierID = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='TierId']");
    prov.log(0, "tierID: " + tierID);
    tierMultiplier = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='TierMultiplier']");            
    prov.log(0, "tierMultiplier: " + tierMultiplier);
    config.Global_Variables.CalculatedAffordabilityLines[0].TierMultiplier.setValue(tierMultiplier);
    monthlyRate = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='MonthlyRate']");
    prov.log(0, "monthlyRate: " + monthlyRate);
    config.Global_Variables.CalculatedAffordabilityLines[0].MonthlyRate.setValue(monthlyRate);
    roundDown = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='RoundDown']");
    config.Global_Variables.CalculatedAffordabilityLines[0].RoundDown.setValue(roundDown);
    prov.log(0, "roundDown: " + roundDown); 
    maxDrawAmount = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='MaxDrawAmount']");
    config.Global_Variables.CalculatedAffordabilityLines[0].MaxDrawAmount.setValue(maxDrawAmount);
    prov.log(0, "maxDrawAmount: " + maxDrawAmount);
    termMax = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='TermMax']");
    config.Global_Variables.CalculatedAffordabilityLines[0].setColumn("gbeb23b1191604f5eb919d11192d5caf6", termMax);
    prov.log(0, "termMax: " + termMax);         

    // Affordability calculations
    maxLoan = config.Business_Logic.Scripts.Calculations.CalculateMaxLoan(mdi, termMax, monthlyRate);
    // ***** 23012015 - MS - Per Robert, round to 2 decimal places. *****
    maxLoan = Number(Math.round(maxLoan + "e+2") + "e-2").toFixed(2);
    prov.log(0, "maxLoan: " + maxLoan);
    config.Global_Variables.CalculatedAffordabilityLines[0].MaxLoan.setValue(maxLoan);
    affordabilityLine = Math.floor(maxLoan/roundDown)*roundDown;
    prov.log(0, "affordabilityLine: " + affordabilityLine);
    config.Global_Variables.CalculatedAffordabilityLines[0].AffordabilityLine.setValue(affordabilityLine);
}

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var plans = '<Plans><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="29.0000" PlanId="1" RoundDown="" TermMax="5" TierId="1" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="27.0000" PlanId="1" RoundDown="" TermMax="5" TierId="2" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="1" RoundDown="" TermMax="5" TierId="3" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.0000" PlanId="1" RoundDown="" TermMax="5" TierId="4" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="15.0000" PlanId="1" RoundDown="" TermMax="5" TierId="5" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="2" RoundDown="50" TermMax="5" TierId="6" TierMultiplier="60.61"/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.4000" PlanId="2" RoundDown="100" TermMax="5" TierId="7" TierMultiplier="71.52"/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="16.2000" PlanId="2" RoundDown="100" TermMax="5" TierId="8" TierMultiplier="92.61"/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="13.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="9" TierMultiplier="112.84"/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="10.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="10" TierMultiplier="143.30"/><Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="24.0000" PlanId="3" RoundDown="50" TermMax="6" TierId="11" TierMultiplier="60.61"/><Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="3" RoundDown="100" TermMax="7" TierId="12" TierMultiplier="71.52"/><Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="3" RoundDown="100" TermMax="9" TierId="13" TierMultiplier="92.61"/><Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="3" RoundDown="100" TermMax="11" TierId="14" TierMultiplier="112.84"/><Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="3" RoundDown="100" TermMax="14" TierId="15" TierMultiplier="143.30"/><Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="19.2000" PlanId="5" RoundDown="50" TermMax="6" TierId="17" TierMultiplier="68.06"/><Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="5" RoundDown="100" TermMax="7" TierId="18" TierMultiplier="71.94"/><Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="5" RoundDown="100" TermMax="9" TierId="19" TierMultiplier="92.61"/><Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="5" RoundDown="100" TermMax="11" TierId="20" TierMultiplier="112.84"/><Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="5" RoundDown="100" TermMax="14" TierId="21" TierMultiplier="143.30"/></Plans>';
    
// parse the XML into a DOM
var oParser = new DOMParser();
var oDOM = oParser.parseFromString(plans, "text/xml");

// query for the items we care about.
var items = oDOM.querySelectorAll('[PlanId="5"]');

// Do whatever you need to do with those elements.
// Here (for the sake of example) we're emitting each item's MonthlyRate attribute.
var list = document.getElementById('list');
for(var i = 0; i < items.length; i++){
  var el = document.createElement('li');
  el.innerHTML = 'Monthly Rate: ' + items[i].getAttribute('MonthlyRate');
  list.appendChild(el);
}
&#13;
<ol id="list"></ol>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试此代码

<html>
<body>
<head>
<title>ok</title>
</head>

<body>

    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="29.0000" PlanId="1" RoundDown="" TermMax="5" TierId="1" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="27.0000" PlanId="1" RoundDown="" TermMax="5" TierId="2" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="1" RoundDown="" TermMax="5" TierId="3" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.0000" PlanId="1" RoundDown="" TermMax="5" TierId="4" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="15.0000" PlanId="1" RoundDown="" TermMax="5" TierId="5" TierMultiplier=""/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="2" RoundDown="50" TermMax="5" TierId="6" TierMultiplier="60.61"/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.4000" PlanId="2" RoundDown="100" TermMax="5" TierId="7" TierMultiplier="71.52"/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="16.2000" PlanId="2" RoundDown="100" TermMax="5" TierId="8" TierMultiplier="92.61"/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="13.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="9" TierMultiplier="112.84"/>
    <Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="10.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="10" TierMultiplier="143.30"/>
    <Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="24.0000" PlanId="3" RoundDown="50" TermMax="6" TierId="11" TierMultiplier="60.61"/>
    <Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="3" RoundDown="100" TermMax="7" TierId="12" TierMultiplier="71.52"/>
    <Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="3" RoundDown="100" TermMax="9" TierId="13" TierMultiplier="92.61"/>
    <Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="3" RoundDown="100" TermMax="11" TierId="14" TierMultiplier="112.84"/>
    <Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="3" RoundDown="100" TermMax="14" TierId="15" TierMultiplier="143.30"/>
    <Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="19.2000" PlanId="5" RoundDown="50" TermMax="6" TierId="17" TierMultiplier="68.06"/>
    <Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="5" RoundDown="100" TermMax="7" TierId="18" TierMultiplier="71.94"/>
    <Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="5" RoundDown="100" TermMax="9" TierId="19" TierMultiplier="92.61"/>
    <Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="5" RoundDown="100" TermMax="11" TierId="20" TierMultiplier="112.84"/>
    <Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="5" RoundDown="100" TermMax="14" TierId="21" TierMultiplier="143.30"/>

<script>


    function forEach(nodes,fnc){   
        console.log(nodes.length);
            for(idx = 0;idx<nodes.length;idx++){
               fnc(nodes[idx].attributes);
            }
    }

    function find(id,attributeName){
        var nodes = document.getElementsByTagName('Plan');
        var results = document.getElementById('results');

        results.innerHTML = '';//===>Clear results

        forEach(nodes,function(attributes){

                console.log(attributes[attributeName]);
               if(attributes[attributeName].value == id){               
               var lblspan = document.createElement('span');
               lblspan.innerHTML = 
                    'TierMultiplier : '+attributes['TierMultiplier'].value+'<br>';

                results.appendChild(lblspan);
               }
        });

    }


  </script>
  <input onclick="find(5,'PlanId');" type="button" value="Find"/><br>
  <span id="results">
    {Results}
  </span>

 </body>

<html>