用XML缓存
结构是    - <channel>
    - <item>
      <id>1</id>
      <pd>B007</pd>
      </item>
    - <item>
      <id>2</id>
      <pd>B001</pd>
      </item>
    - <item>
      <id>3</id>
      <pd>B000</pd>
      </item>
    - <item>
      <id>4</id>
      <pd>B004</pd>
      </item>
    - <item>
      <id>5</id>
      <pd>B003</pd>
      </item>
    ..............
    </channel>
复制代码
数据比较多现在我想快速找到
id=4的pd值
或者id=4下面的item的所有值
应该怎么写?还有一个问题,xml可以支持动态读取值的吗?
比如a.xml?id=4
这类型的
可以吗?谢谢是PHP的

解决方案 »

  1.   

    参考:http://topic.csdn.net/u/20120521/03/527bc4d2-b4e0-4d6e-9ae4-021a675cfff5.html
      

  2.   

    $xml=simplexml_load_file('bb.xml');
    foreach($xml->item as $v){
          if($v->id==4) echo "id : $v->id<br>pd : $v->pd";
    }
      

  3.   

    xpath,还有其他答案吗。[User:root Time:10:28:18 Path:/home/liangdong/php]$ php xpath.php 
    pd B004
    [User:root Time:10:28:19 Path:/home/liangdong/php]$ cat xpath.php 
    <?php
    $str = <<<EOF
    <channel>
      <item>
      <id>1</id>
      <pd>B007</pd>
      </item>
      <item>
      <id>2</id>
      <pd>B001</pd>
      </item>
      <item>
      <id>3</id>
      <pd>B000</pd>
      </item>
      <item>
      <id>4</id>
      <pd>B004</pd>
      </item>
      <item>
      <id>5</id>
      <pd>B003</pd>
      </item>
      </channel>
    EOF;$xml = simplexml_load_string($str, "SimpleXMLElement", LIBXML_NOBLANKS);
    $res = $xml->xpath("/channel/item/id[text()=4]/parent::item/pd");
    foreach ($res as $node) {
            echo $node->getName() . " " . $node->{0} . PHP_EOL;
    }
    ?>
      

  4.   


    谢谢了,看了一下,还要用到插件,所以没有去试,不过已经用xpath解决了