2

这是我的 general.xml 文件:-

<?xml version="1.0" encoding="UTF-8"?>
<results>
    <numFound>10</numFound>
    <QTime>4</QTime>
    <result>
        <distance>1071.59873299109</distance>
        <name>Irungattukottai</name>
    </result>
    <result>
            <distance>1892.33578431928</distance>
            <name>Valapuram</name>
       </result>
</results>

region.xml:-

<childrens>
    <child_5068 entity_id="5069" value="Irungattukottai" parent_id="4068"/>
    <child_5068 entity_id="7140" value="Valapuram" parent_id="4068"/>
</childrens>

产品.xml:-

<products>
  <product_id value="1">
    <tab_id value="351">
      <tab_name value="test1"/>
      <dist_region value="5069"/>
      <dist_region value="5069"/>
      <dist_region value="5069"/>
    </tab_id>
  </product_id>
  <product_id value="2">
    <tab_id value="352">
      <tab_name value="test2"/>
      <dist_region value="4457"/>
      <dist_region value="7140"/>
      <dist_region value="5069"/>
    </tab_id>
  </product_id>
</products>

我有这三个 xml 文件,我想尝试这样的事情。
一般来说.xml文件元素<name>Irungattukottai</name>
如果这个名字是exit inregion.xml然后到达那里entity_id
如果entity_id是exit inproduct.xml然后返回那里product_id元素属性值...

我正在尝试这段代码:-

<?php
$g = file_get_contents('general.xml');
$r = file_get_contents('region.xml');
$p = file_get_contents('product.xml');

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

$name = (string)$general->result->name;

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 "City Name:- $name, Entity_id:- $entity, Product_id:- $prid";
?>

这段代码可以正常工作,但只有一个值,例如:-在第一个和第二个根general.xml中有两个根, 但我的代码只能获取第一个元素,而不是尝试获取墙 xml 文件...<name>Irungattukottai</name><name>Valapuram</name>

这是我的正确输出:-

city Name:- Irungattukottai, Entity_id:- 5079, Product_id:- 1

但我想要这种类型的输出: -

city Name:- Irungattukottai, Entity_id:- 5079, Product_id:- 1
city Name:- Valapuram , Entity_id:- 7140, Product_id:- 2
4

4 回答 4

0

您需要迭代 $general 的数组。

试着做 :

echo '<pre>';
print_r($general);
echo '</pre>';

检查您是否获得了一个包含 xml 文件中所有元素的数组。

于 2013-03-28T05:24:39.697 回答
0

您获取 general.xml 提要的代码是可以的。

你可以得到这个名字

$name1 = (string)$general->result[0]->name;
$name2 = (string)$general->result[1]->name;
or you can use loop to get name one by one.

<?php
foreach($general->result as $row){
if(isset($row->name)){
  // do your logic here for region.xml and then for product.xml
}
}
?>
于 2013-03-28T05:37:30.083 回答
0

试试这个:

$xml = simplexml_load_file( 'general.xml' );
$cities = $xml->xpath( '//result/name' );

$cityCount = count( $cities );
if( $cityCount ) {
    $region = simplexml_load_file( 'region.xml' );
    foreach( $cities as $city ) {
        $regions = $region->xpath( '//*[@value="'. ( string ) $city . '"]/@entity_id' );
        if( count ( $regions ) ) {
            $product = simplexml_load_file( 'products.xml' );
            $products = $product->xpath( '//dist_region[@value="' . ( string ) $regions[ 0 ][ 'entity_id' ] . '"]/ancestor::product_id/@value' );
            if( count( $products ) ) {
                foreach( $products as $p ) {
                    echo 'city Name:- ' . ( string ) $city . ', Entity_id:- ' . ( string ) $regions[ 0 ][ 'entity_id' ] . ', Product_id:- ' . $p[ 'value' ] . "\n";
                }
            }
        }
    }
}

输出

city Name:- Irungattukottai, Entity_id:- 5069, Product_id:- 1
city Name:- Irungattukottai, Entity_id:- 5069, Product_id:- 2
city Name:- Valapuram, Entity_id:- 7140, Product_id:- 2
于 2013-03-28T06:02:17.833 回答
0

使用以上两个答案,可以试试这个:-

  <?php
    foreach($general->result as $row){
    if(isset($row->name)){
    //create one variable and store in value.
    $name = $row->name;
     //all you code for region.xml and then for product.xml

    }
    echo "name:- $name,Entity:- $entity, product:- $prid"."<br>";
    }

?>
于 2013-03-28T07:41:49.797 回答