如何解析这个特定的XML?

时间:2012-11-16 01:16:35

标签: php xml simplexml

这是我到目前为止所尝试的内容,它给了我一些我不想要的东西(请参阅底部的浏览器输出):

// 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 ) ) 

2 个答案:

答案 0 :(得分: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"
      }
    }

答案 1 :(得分:1)

使用而不仅仅是member这个:member[name="categoryName"]

$xml = $xml->xpath('params/param/value/array/data/value/struct/member[name="categoryName"]/value/string');