我怎样才能得到[b7]中的内容?请帮我纠正Xpath

时间:2013-10-21 16:27:51

标签: php xpath simplexml

有人可以帮我从这条XML路径中获取正确的数据吗? 以下数组是名为FIRST的xml路径。我怎样才能得到[b7]中的内容?

Array([0] => SimpleXMLElement Object 
      ( [@attributes] => Array 
          ( 
            [a1] => ENG 
            [a2] => 7F5         
            [a3] => 0 
            [a4] => 0 
          )

        [LINK] => SimpleXMLElement Object 
        ( 
        [@attributes] => Array 
            ( 
                [b1] => bla
                [b2] => bla - bla
                [b3] => 0 
                [b4] => 0 
                [b5] => bla
                [b6] => bla-bla 
                [b7] => 232323
                [b8] => 1 
                [b9] => bla-bla-bla
            )
        ) 
      )
 ) 

我尝试使用$something = $value-> xpath('FIRST')获取FIRST数组 它有效,但是当我正在尝试$bla="{$something['0']['b7']}"时,我什么都没有回来。

任何帮助??

1 个答案:

答案 0 :(得分:2)

请参阅以下示例(Demo):

<?php
/**
 * How can I get what is in [b7]? Please help me get right Xpath
 *
 * @link http://stackoverflow.com/a/19502280/367456
 */

$xml = simplexml_load_string('<CONTENT><LINK b7="hey"/></CONTENT>');
var_dump($xml);

$b7 = (string)$xml->xpath('LINK/@b7')[0];
var_dump($b7);

节目输出:

object(SimpleXMLElement)#1 (1) {
  ["LINK"]=>
  object(SimpleXMLElement)#2 (1) {
    ["@attributes"]=>
    array(1) {
      ["b7"]=>
      string(3) "hey"
    }
  }
}
string(3) "hey"