1

这是我迄今为止尝试过的,它给了我一堆我不想要的东西(浏览器输出见底部):

// transform to Simple XML
$xml = new SimpleXMLElement($results);
// find category names
$xml = $xml->xpath('params/param/value/array/data/value/struct/member/value/string');

print_r($xml);

这是 XML,我需要获取所有 categoryName 节点:

<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value>
      <array><data>
  <value><struct>
  <member><name>categoryId</name><value><string>7</string></value></member>
  <member><name>parentId</name><value><string>0</string></value></member>
  <member><name>description</name><value><string>@MyCat000egory</string></value></member>
  <member><name>categoryDescription</name><value><string></string></value></member>
  <member><name>categoryName</name><value><string>@MyCat000egory</string></value></member>
  <member><name>htmlUrl</name><value><string>http://en.webacceleration.net/?cat=7</string></value></member>
  <member><name>rssUrl</name><value><string>http://en.webacceleration.net/?feed=rss2&amp;amp;cat=7</string></value></member>
</struct></value>
  <value><struct>
  <member><name>categoryId</name><value><string>6</string></value></member>
  <member><name>parentId</name><value><string>0</string></value></member>
  <member><name>description</name><value><string>this is the category</string></value></member>
  <member><name>categoryDescription</name><value><string></string></value></member>
  <member><name>categoryName</name><value><string>this is the category</string></value></member>
  <member><name>htmlUrl</name><value><string>http://en.webacceleration.net/?cat=6</string> </value></member>
  <member><name>rssUrl</name><value><string>http://en.webacceleration.net/?feed=rss2&amp;amp;cat=6</string></value></member>
</struct></value>
  <value><struct>
  <member><name>categoryId</name><value><string>1</string></value></member>
  <member><name>parentId</name><value><string>0</string></value></member>
  <member><name>description</name><value><string>Uncategorized</string></value></member>
  <member><name>categoryDescription</name><value><string></string></value></member>
  <member><name>categoryName</name><value><string>Uncategorized</string></value></member>
  <member><name>htmlUrl</name><value><string>http://en.webacceleration.net/?cat=1</string></value></member>
  <member><name>rssUrl</name><value><string>http://en.webacceleration.net/?feed=rss2&amp;amp;cat=1</string></value></member>
</struct></value>
</data></array>
      </value>
    </param>
  </params>
</methodResponse>

这是浏览器输出:

Array ( [0] => SimpleXMLElement Object ( [0] => 7 ) [1] => SimpleXMLElement Object ( [0] => 0 ) [2] => SimpleXMLElement Object ( [0] => @MyCat000egory ) [3] => SimpleXMLElement Object ( ) [4] => SimpleXMLElement Object ( [0] => @MyCat000egory ) [5] => SimpleXMLElement Object ( [0] => http://en.webacceleration.net/?cat=7 ) [6] => SimpleXMLElement Object ( [0] => http://en.webacceleration.net/?feed=rss2&cat=7 ) [7] => SimpleXMLElement Object ( [0] => 6 ) [8] => SimpleXMLElement Object ( [0] => 0 ) [9] => SimpleXMLElement Object ( [0] => this is the category ) [10] => SimpleXMLElement Object ( ) [11] => SimpleXMLElement Object ( [0] => this is the category ) [12] => SimpleXMLElement Object ( [0] => http://en.webacceleration.net/?cat=6 ) [13] => SimpleXMLElement Object ( [0] => http://en.webacceleration.net/?feed=rss2&cat=6 ) [14] => SimpleXMLElement Object ( [0] => 1 ) [15] => SimpleXMLElement Object ( [0] => 0 ) [16] => SimpleXMLElement Object ( [0] => Uncategorized ) [17] => SimpleXMLElement Object ( ) [18] => SimpleXMLElement Object ( [0] => Uncategorized ) [19] => SimpleXMLElement Object ( [0] => http://en.webacceleration.net/?cat=1 ) [20] => SimpleXMLElement Object ( [0] => http://en.webacceleration.net/?feed=rss2&cat=1 ) ) 
4

2 回答 2

1
$categories = $xml->xpath('//name[text()="categoryName"]/../value/string');

用于text()="categoryName"定位<name>/../检索其兄弟节点<value>

全面分解:

  • //name[text()="categoryName"]<name>文本值为“categoryName”的 所有节点
  • /../上一场比赛的父节点
  • /value/string <string><value>匹配的父节点的子节点/../

.

   var_dump($categories);
    array(3) {
      [0]=>
      object(SimpleXMLElement)#5 (1) {
        [0]=>
        string(14) "@MyCat000egory"
      }
      [1]=>
      object(SimpleXMLElement)#6 (1) {
        [0]=>
        string(20) "this is the category"
      }
      [2]=>
      object(SimpleXMLElement)#7 (1) {
        [0]=>
        string(13) "Uncategorized"
      }
    }
于 2012-11-16T01:26:28.063 回答
1

使用而不是简单地使用member这个:member[name="categoryName"]

$xml = $xml->xpath('params/param/value/array/data/value/struct/member[name="categoryName"]/value/string');
于 2012-11-16T01:26:37.587 回答