php處理xpath

  1 <?php

  2 $doc = new DOMDocument('1.0', 'utf-8');

  3 $doc->load('./info.xml');

  4 $xpath = new DOMXPath($doc);

  5 $name = $_GET['name'];

  6 $password = $_GET['password'];

  7 $query = "/addressBook/address[name/text()='$name' and password/text()='$password']/card";

  8 echo $query."<br>";

  9 $arts = $xpath->query($query);

 10 foreach ($arts as $art)

 11 {

 12     echo $art->nodeValue." ";

 13 }

 14 ?>

 

 

其中,articles.xml的內容如下:

 

  1 <?xml version="1.0" encoding="UTF-8"?>

  2 <articles>

  3     <article id="1">

  4         <tags>

  5             <tag>php</tag>

  6             <tag>xpath</tag>

  7         </tags>

  8         <title>PHP XPath Example</title>

  9     </article>

 10     <article id="2">

 11         <tags>

 12             <tag>dom</tag>

 13             <tag>dodocument</tag>

 14         </tags>

 15         <title>DomDocument Tutorial</title>

 16     </article>

 17 </articles>

 

 

發佈了53 篇原創文章 · 獲贊 2 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章