Heim  >  Artikel  >  Backend-Entwicklung  >  PHP+XML缓存怎么快速读取指定的值

PHP+XML缓存怎么快速读取指定的值

WBOY
WBOYOriginal
2016-06-13 13:27:06980Durchsuche

PHP+XML缓存如何快速读取指定的值
用XML缓存
结构是

  -
  -
  1
  B007
 

  -
  2
  B001
 

  -
  3
  B000
 

  -
  4
  B004
 

  -
  5
  B003
 

  ..............
 



复制代码
数据比较多

现在我想快速找到
id=4的pd值
或者id=4下面的item的所有值
应该怎么写?

还有一个问题,xml可以支持动态读取值的吗?
比如a.xml?id=4
这类型的
可以吗?谢谢

是PHP的

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

PHP code
$xml=simplexml_load_file('bb.xml');
foreach($xml->item as $v){
      if($v->id==4) echo "id : $v->id<br>pd : $v->pd";
}
<br><font color="#e78608">------解决方案--------------------</font><br>
xpath,还有其他答案吗。<br><br>
PHP code
[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>
  
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;
}
?> <div class="clear">
                 
              
              
        
            </div>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn