dougu2006 2013-12-11 01:19
浏览 46
已采纳

XML解析儿童()

I'm trying to parse the following xml https://ota.cartrawler.com/cartrawlerota/files/static/ctlocation.EN.xml

I need to parse locations based on countries, this is what I have:

<?php
$xml = simplexml_load_file("ctlocation.ES.xml") 
   or die("Error: Cannot create object");
$name3 = $_GET["nombre"];
foreach($xml->Country->children('Albania') as $loca){
    $name2 = $loca[0]['Name'];
    echo $name2;
    echo "<br />";
    }
?>

It's not retuning any error but it's not parsing anything either. Any idea what's wrong?

Thanks a lot!

  • 写回答

1条回答 默认 最新

  • dongxifu5009 2013-12-11 03:21
    关注

    Observation #1:

    If you have performance problems due to the large size (~35M) of ctlocation.ES.xml, consider using XmlStreamer for parsing large XML files in PHP.

    Observation #2:

    Aside from the size of the input XML, there are correctness issues with the PHP you posted. There is no root level Country element in ctlocation.ES.xml, and 'Albania' only exists as a @name attribute on a /CTLocations/Country element. Also, you have to account for the namespace in which your input XML elements exist.

    Let's use a small sample from the input XML and load it as a string so as to create a small, self-contained example.

    To step through the locations of Albania, try this:

    <?php 
    $xmlstring = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <CTLocations xmlns="http://www.cartrawler.com/">
      <Country code="AL" name="Albania" continent="Europe">
        <Location Id="7188" Name="Tirana Airport" Lat="41.42108838" Lng="19.71271276" CountryCode="AL" Address="Tirana Airport Muhamet Gjollesha Str., Muhamet Gjollesha Str., Tirana" CityName="Tirana" Airport="1" AirportCode="TIA" RailwayStation="0"/>
        <Location Id="30768" Name="Tirana Downtown" Lat="41.332" Lng="19.832" CountryCode="AL" Address="Rruga E Durresit. Nr 61, Tirana" CityName="Tirana" Airport="0" RailwayStation="0"/>
        <Location Id="52400" Name="Sheraton Italia Square" Lat="0.0" Lng="0.0" CountryCode="AL" Address="Square Italia Hotel Sheraton , Tirana," CityName="Tirana" PostalCode="" Airport="0" RailwayStation="0"/>
      </Country>
      <Country code="AD" name="Andorra" continent="Europe">
        <Location Id="4650" Name="Escaldes" Lat="42.511" Lng="1.548" CountryCode="AD" Address="Avda. Miquel Mateu, 25, Escaldes-engordany , Andorra, Ad700" CityName="Andorra" PostalCode="AD700" Airport="0" RailwayStation="0"/>
        <Location Id="152576" Name="Andorra" Lat="42.508" Lng="1.522" CountryCode="AD" Address="Avda D Enclar, 142, Edifici Becier, Andorra La Vella, Ad500" CityName="Andorra La Vella" PostalCode="AD500" Airport="0" RailwayStation="0"/>
      </Country>
    </CTLocations>
    XML;
    $xml = simplexml_load_string($xmlstring)
       or die("Error: Cannot create object");
    $xml->registerXPathNamespace('ct', 'http://www.cartrawler.com/');
    echo "The locations in Albania:<br/>";
    foreach($xml->xpath("/ct:CTLocations/ct:Country[@name='Albania']/ct:Location") as $loca) {
        echo $loca['Name'];
        echo "<br/>";
    }
    echo "Done."
    ?>
    

    The above script will yield this output:

    The locations in Albania:
    Tirana Airport
    Tirana Downtown
    Sheraton Italia Square
    Done.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败