php – 在多个XML文件中搜索匹配

这是我的general.xml文件:
general.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<results xmlns="http://gisgraphy.com">
    <numFound>1</numFound>
    <QTime>67</QTime>
    <result>
        <distance>1139.81967842778</distance>
        <name>Rājkot</name>
        <adm1Code>09</adm1Code>
        <adm1Name>State of Gujarāt</adm1Name>
        <asciiName>Rajkot</asciiName>
        <countryCode>IN</countryCode>
        <featureClass>P</featureClass>
        <featureCode>PPL</featureCode>
        <featureId>1258847</featureId>
        <gtopo30>139</gtopo30>
        <population>1177362</population>
        <timezone>Asia/Kolkata</timezone>
        <lat>22.299999237060547</lat>
        <lng>70.78333282470703</lng>
        <placeType>City</placeType>
        <oneWay>false</oneWay>
        <length>0.0</length>
        <google_map_url>http://maps.google.com/maps?f=q&amp;amp;ie=UTF-8&amp;amp;iwloc=addr&amp;amp;om=1&amp;amp;z=12&amp;amp;q=R%C4%81jkot&amp;amp;ll=22.329999237060548,70.78333282470703</google_map_url>
        <yahoo_map_url>http://maps.yahoo.com/broadband?mag=6&amp;amp;mvt=m&amp;amp;lon=70.78333282470703&amp;amp;lat=22.299999237060547</yahoo_map_url>
        <country_flag_url>/images/flags/IN.png</country_flag_url>
    </result>
</results>

这是我的region.xml文件:

<childrens>
    <child_4893 entity_id="4893" value="Gujarat" parent_id="4823">
        <child_4894 entity_id="4894" value="Ahmedabad" parent_id="4893"/>
        <child_4895 entity_id="4895" value="Anand" parent_id="4893"/>
        <child_4896 entity_id="4896" value="Bharuch (Broach)" parent_id="4893"/>
        <child_4897 entity_id="4897" value="Bhavnagar" parent_id="4893"/>
        <child_4898 entity_id="4898" value="Bhuj" parent_id="4893"/>
        <child_4899 entity_id="4899" value="Gandhidham" parent_id="4893"/>
        <child_4900 entity_id="4900" value="Gandhinagar" parent_id="4893"/>
        <child_4901 entity_id="4901" value="Godhra" parent_id="4893"/>
        <child_4902 entity_id="4902" value="Jamnagar" parent_id="4893"/>
        <child_4903 entity_id="4903" value="Junagadh" parent_id="4893"/>
        <child_4904 entity_id="4904" value="Morvi" parent_id="4893"/>
        <child_4905 entity_id="4905" value="Nadiad" parent_id="4893"/>
        <child_4906 entity_id="4906" value="Navsari" parent_id="4893"/>
        <child_4907 entity_id="4907" value="Patan" parent_id="4893"/>
        <child_4908 entity_id="4908" value="Porbandar" parent_id="4893"/>
        <child_4909 entity_id="4909" value="Rajkot" parent_id="4893"/>
        <child_4910 entity_id="4910" value="Surat" parent_id="4893"/>
        <child_4911 entity_id="4911" value="Surendranagar" parent_id="4893"/>
        <child_4912 entity_id="4912" value="Vadodara (Baroda)" parent_id="4893"/>
        <child_4913 entity_id="4913" value="Vejalpur" parent_id="4893"/>
        <child_4914 entity_id="4914" value="Veraval" parent_id="4893"/>
    </child_4893>
</childrens>

这是我的product.xml文件:

<products>
    <product_id value="1">
        <tab_id>
            <tab_name value="test1" />
            <dist_region value="4909"/>
            <dist_region value="4909"/>
            <dist_region value="4909"/>
        </tab_id>
    </product_id>
</products>

说明

>在general.xml文件中,有一个名为< name>的节点元素,它是商店城市名称.
>检查region.xml文件中是否存在此城市名称,复制相应的entity_id.
>检查product.xml文件中是否存在此entity_id,如果存在则返回相应的product_id.

例如:

> take< name> Rajkot< / name>来自general.xml
> Rajkot存在于region.xml中,对应的entity_id是4909
> entity_id值4909存在于product.xml中,相应的product_id属性值为1
> return product_id值1

最佳答案 使用PHP simplexml执行此操作: – >看现场演示:
http://codepad.viper-7.com/ax2LtQ

// $g holds general.xml, $r region.xml, $p product.xml 

$general = simplexml_load_string($g);
$region = simplexml_load_string($r);
$product = simplexml_load_string($p);

$name = (string)$general->result->name;
$name = 'Rajkot' // <--- see explanation below!

if (strlen(trim($name))==0) exit('name not found');

list($entity) = $region->xpath("//*[@value='$name']/@entity_id");
$entity=(string)$entity;

if (strlen(trim($entity))==0) exit('entity_id not found');

list($prid) = $product->xpath("//dist_region[@value='$entity']/ancestor::product_id/@value");
$prid=(string)$prid;

echo "name: $name, entity_id: $entity, product_id: $prid";

说明:在general.xml中,< name> =Rājkot,在region.xml中它是Rajkot,没有特殊的a字符.为了使代码正常工作,我不得不将$name设置为Rajkot.
你需要找到一个解决方案.

点赞