0

我有包含大量数据的 xml 文件。现在我想选择一个有条件的价格。我在 javascript 函数中设置了一个参数,但它没有给出期望的结果。我认为可以通过 childnode 完成,但我不知道

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"/>

网页

<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>
4

1 回答 1

1

你错过了吗?

xmlDoc=loadXMLDoc("flights.xml");

查这个页面

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

example2 非常清楚如何使用它

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

希望这可以帮助

于 2012-04-14T09:24:07.410 回答