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 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在