【问题标题】:Fatal error:call to undefined method stdClass::xpath()致命错误:调用未定义的方法 stdClass::xpath()
【发布时间】:2015-06-16 11:52:03
【问题描述】:

我有这个 XML:

<JobReference> <Type>STANDARD</Type> <Title>N° ANCIEN DOSSIER</Title> <Reference/>

并且正在运行这个 PHP 代码:

$xmljobreference = simplexml_load_string ($GetJobResult->JobReferences->JobReference->Title );    
$referenceanciendossier = $GetJobResult->JobReferences->xpath( "//JobReference[@Title ='N°  ANCIEN DOSSIER']/Reference" );

但我收到此错误:

致命错误:在第 190 行调用未定义的方法 stdClass::xpath()

如果我运行var_dump($GetJobResult),我会得到:

 public 'JobReferences' => 
    object(stdClass)[149]
      public 'JobReference' => 
        array (size=7)
          0 => 
            object(stdClass)[150]
              ...
          1 => 
            object(stdClass)[151]
              ...
          2 => 
            object(stdClass)[152]

             public 'JobSeq' => int 920179

【问题讨论】:

  • 调试。 $GetJobResult 设置了吗?
  • 好吧 stdClass 对象没有方法...可能你想用$xmljobreference做点什么
  • @Gustaf 我添加 var_dump($GetJobResult) 它是对象
  • @test 我提供了一个答案。试试看
  • @Gustaf 我尝试但我有这个错误调用未定义的 stdclass::xpath() 并且我在你的答案下发表评论

标签: php xml xpath simplexml php-5.4


【解决方案1】:

好吧,正如您的 var_dump 向您展示的那样,JobReferences 是由 JobReference 构建的,它本身是一个包含 7 个项目的数组。

所以假设你想对所有数组项运行 xpath 函数,你需要运行一个foreach 循环。像这样的东西应该可以工作:

$array = $GetJobResult->JobReferences->JobReference;
$results = array();

foreach($array as $reference){
    array_push($results, $reference->xpath( "//JobReference[@Title ='N°  ANCIEN DOSSIER']/Reference" ));
}
var_dump($results);

【讨论】:

  • 在 xml 我有这个 STANDARDN° ANCIEN DOSSIER
  • 你能告诉我如何解决这个问题谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
相关资源
最近更新 更多