2012-04-14 51 views
0

我有xml文件,其中包含大量的數據。現在我想選擇一些條件的價格。我在javascript函數中設置了參數,但它並沒有給出想要的結果。我認爲它可以通過childnode做,但我因此未察覺有關使用jQuery的XML與XML

XML文件

<flights updated="2012-03-09T04:38:00.437" type="flights" ob_id="45792117" lastedit="2012-03-09T15:10:01" partner_id="63" activate_date="2012-02-15T00:00:00" page_id="9646" page_pk_id="12597" pos_pk_id="51565" pos="1" module_id="3" pos_name="Flights" product_type_id="4" product_type="flight" headline="Bali" destination="Bali" localised_destination="Denpasar" headline_no_html="Bali" price="199" deals_space_limited="0" deals_sold_out="0" qa_approved="1" tp_available="0" tp_weight="10" partner_eapid="0-25" partner_pid="25" publish_path="\\dubvappdaily01\daily_aus\data\psf\" partner_lang_id="3081" FrTLAs="PER" ToTLAs="DPS" FrDate="2012-04-27T00:00:00" ToDate="2012-05-04T00:00:00" Airline="QZ"/> 

<flights updated="2012-03-09T04:38:00.437" type="flights" ob_id="45792117" lastedit="2012-03-09T15:10:01" partner_id="63" activate_date="2012-02-15T00:00:00" page_id="9646" page_pk_id="12597" pos_pk_id="51565" pos="1" module_id="3" pos_name="Flights" product_type_id="4" product_type="flight" headline="Bali" destination="Bali" localised_destination="Denpasar" headline_no_html="Bali" price="199" deals_space_limited="0" deals_sold_out="0" qa_approved="1" tp_available="0" tp_weight="10" partner_eapid="0-25" partner_pid="25" publish_path="\\dubvappdaily01\daily_aus\data\psf\" partner_lang_id="3081" FrTLAs="SYD" ToTLAs="DPS" FrDate="2012-04-27T00:00:00" ToDate="2012-05-04T00:00:00" Airline="QZ"/> 

HTML頁面

<head> 
    <script type="text/javascript"> 

    function myXml(origin, destination) { 
     var x = xmlDoc.getElementsByTagName("flights"); 

     for(i=0; i<x.length; i++) { 
      if (x[i].getAttribute('FrTLAs') == origin 
      && x[i].getAttribute('destination') == destination) { 
       alert(x[i].getAttribute('price')) 
      } 
     } 
    } 
    </script> 
</head> 

<body> 
    <a href="#" onclick="myXml('SYD','Bali')">click me</a> 
</body> 
+0

有什麼確切的問題嗎? – cfedermann 2012-04-14 09:17:50

回答

1

沒有ü錯過這個?

xmlDoc=loadXMLDoc("flights.xml"); 

CHK此頁面

http://www.w3schools.com/dom/prop_element_attributes.asp 

的例題很清楚如何使用這個

x=xmlDoc.getElementsByTagName("book")[0].attributes; 
    //here its would be getElementsByTagName("flights") in the loop 
    //then .attributes on it 
    // and then this 
frtlas=x.getNamedItem("FrTLAs"); 
desti=x.getNamedItem("destination"); 

//and your code here 

希望這有助於

+0

我添加了xml文件,但是我沒有在這裏粘貼代碼 – Carlos 2012-04-14 09:27:57