简体   繁体   English

使用xpath在PHP中解析XML

[英]Parsing XML in PHP using xpath

Simple php code to consume SOAP web-service: 使用SOAP Web服务的简单php代码:

<?php

$url = "http://localhost:9090/TaskListService/TaskListWS?wsdl";
$client = new SoapClient($url);
$fcs = $client->__getFunctions();
var_dump($fcs);
$xml = $client->getTaskListForUser(array('userToken' => 'X:123:123:woo:John:Shoulders:mgr'));
print 'xml' . PHP_EOL;
print_r($xml);
$result = $xml->xpath('/doc/tasks/task');
print_r($result);
?>

Run in terminal to get: 在终端中运行以获取:

[woo:/Library/WebServer/Documents] 255 > php TestTaskList.php
array(8) {
  [0]=>
  string(47) "toStringResponse toString(toString $parameters)"
  [1]=>
  string(77) "getTaskListForUserResponse getTaskListForUser(getTaskListForUser $parameters)"
  [2]=>
  string(104) "updateWaitingEntriesServiceResponse updateWaitingEntriesService(updateWaitingEntriesService $parameters)"
  [3]=>
  string(53) "queueEntryResponse queueEntry(queueEntry $parameters)"
  [4]=>
  string(77) "getAllQueueEntriesResponse getAllQueueEntries(getAllQueueEntries $parameters)"
  [5]=>
  string(71) "deleteQueueEntryResponse deleteQueueEntry(deleteQueueEntry $parameters)"
  [6]=>
  string(56) "updateEntryResponse updateEntry(updateEntry $parameters)"
  [7]=>
  string(107) "getAllQueueEntriesWithStatusResponse getAllQueueEntriesWithStatus(getAllQueueEntriesWithStatus $parameters)"
}
xml
stdClass Object
(
    [return] => <?xml version='1.0'?><doc><tasks><task><OWNER>woo</OWNER><OID>1448826671442</OID><QUEUE_NAME>HR</QUEUE_NAME><ENTRY_NUMBER>5020</ENTRY_NUMBER><PRIORITY></PRIORITY><CREATION_DATE>11-29-2015 14:51:11</CREATION_DATE><JOB_ID>5000</JOB_ID><JOB_STEP_ID>createReporting</JOB_STEP_ID><BUSINESS_PROCESS>NewEmployee</BUSINESS_PROCESS><DATA>test_data.xml</DATA><ENTRY_STATUS></ENTRY_STATUS><PRIORITY></PRIORITY><HANDLE_BY_DATE></HANDLE_BY_DATE><ESCALATE_TO_USER></ESCALATE_TO_USER><ESCALATE_TO_ROLE></ESCALATE_TO_ROLE><QUEUED_FOR_ROLE></QUEUED_FOR_ROLE><QUEUED_FOR_USERID></QUEUED_FOR_USERID><MSG></MSG><COMMENT></COMMENT><RESULT_OBJ>1448826383597.xml</RESULT_OBJ></task><task><OWNER>woo</OWNER><OID>1448397478803</OID><QUEUE_NAME>HR</QUEUE_NAME><ENTRY_NUMBER>4710</ENTRY_NUMBER><PRIORITY></PRIORITY><CREATION_DATE>11-24-2015 15:37:58</CREATION_DATE><JOB_ID>4700</JOB_ID><JOB_STEP_ID>createDependents</JOB_STEP_ID><BUSINESS_PROCESS>NewEmployee</BUSINESS_PROCESS><DATA>dependents.xml</DATA><ENTRY_STATUS></ENTRY_STATUS><PRIORITY></PRIORITY><HANDLE_BY_DATE></HANDLE_BY_DATE><ESCALATE_TO_USER></ESCALATE_TO_USER><ESCALATE_TO_ROLE></ESCALATE_TO_ROLE><QUEUED_FOR_ROLE></QUEUED_FOR_ROLE><QUEUED_FOR_USERID></QUEUED_FOR_USERID><MSG></MSG><COMMENT></COMMENT><RESULT_OBJ>1448397449614.xml</RESULT_OBJ></task></tasks><errors><error></error></errors></doc>
)

Fatal error: Call to undefined method stdClass::xpath() in /Library/WebServer/Documents/TestTaskList.php on line 10

How should I get the list of 'task' entries? 我应该如何获取“任务”条目列表? Note that I tried to do 请注意,我尝试做

$result = $xml->xpath('/doc/tasks/task');

and get an error I cannot figure out. 并得到我无法弄清楚的错误。

When you try: 当您尝试:

$xml->xpath('/doc/tasks/task');

in $xml you have an object with type stdClass, which hasn't xpath() method. 在$ xml中,您有一个类型为stdClass的对象,该对象没有xpath()方法。

You neen to create SimpleXMLElement object first, like this: 您首先需要创建SimpleXMLElement对象,如下所示:

$simpleXml = SimpleXMLElement($xml->return);
// or $simpleXml = simplexml_load_string($xml->return);

And then you can use xpath(): 然后可以使用xpath():

$result = $simpleXml->xpath('/doc/tasks/task');
var_dump($result);

After that you will see, that you have an array with two SimpleXMLElement objects with 'task' entries and iterate them. 之后,您将看到一个数组,其中包含两个带有“任务”条目的SimpleXMLElement对象,并对其进行迭代。

Read more here 在这里阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM