简单XML帮助:如何解析这个XML?

3

我没有做过任何XML项目,所以不太确定该如何处理这些数据...

我正在使用curl向salesforce发出请求,他们会给我返回一个响应,我需要解析它。我想使用simplexml。以下是响应的一部分:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<loginResponse>
<result>
<metadataServerUrl>
https://na6-api.salesforce.com/services/Soap/m/18.0/
</metadataServerUrl>
<passwordExpired>
false
</passwordExpired>
<sandbox>
false
</sandbox>
<serverUrl>
https://na6-api.salesforce.com/services/Soap/u/18.0/
</serverUrl>
<sessionId>
!AQ4AQLtDIqY.
</sessionId>
<userId>

</userId>
<userInfo>
<accessibilityMode>
false
</accessibilityMode>
<currencySymbol>
$
</currencySymbol>
<orgDefaultCurrencyIsoCode>
USD
</orgDefaultCurrencyIsoCode>
<orgDisallowHtmlAttachments>
false
</orgDisallowHtmlAttachments>
<orgHasPersonAccounts>
false
</orgHasPersonAccounts>
<organizationId>

</organizationId>
<organizationMultiCurrency>
false
</organizationMultiCurrency>
<organizationName>
Ox 
</organizationName>
<profileId>
sdfgsdfg
</profileId>
<roleId>
sdfgsdfg
</roleId>
<userDefaultCurrencyIsoCode xsi:nil="true"/>
<userEmail>
###@gmail.com
</userEmail>
<userFullName>
### ###
</userFullName>
<userId>
asdfasdf
</userId>
<userLanguage>
en_US
</userLanguage>
<userLocale>
en_US
</userLocale>
<userName>
asdfasdf@gmail.com
</userName>
<userTimeZone>
America/Chicago
</userTimeZone>
<userType>
Standard
</userType>
<userUiSkin>
Theme3
</userUiSkin>
</userInfo>
</result>
</loginResponse>
</soapenv:Body>
</soapenv:Envelope>

无论如何,我期望将这些东西(我们称其为数据)输入到

$results = simplexml_load_string($data);
var_dump($results);

这将会给我返回所有的数据...然后要访问特定的部分,它将是 $results->body->loginResponse->blah->blah...

但是它并没有给我任何东西,只是一个空的简单xml对象...

所以有一个网站让我认为我可能需要一个XSLT来正确读取它。
或者还有其他原因让我认为是因为我没有在顶部添加 <?xml version="1.0" encoding="UTF-8"?>。

帮帮我!


也许在读取文件内容时出现了错误... 你启用了 E_WARNING 吗?也许你可以通过这种方式看出发生了什么。 - Cristian
8个回答

2
你可以使用SimpleXML,但由于使用了命名空间(例如“soapenv”),它不像你期望的那么简单。建议尝试使用SimpleXMLElement::children方法,如下所示:
$sxe = new SimpleXMLElement($data);
$login_response = $sxe->children('soapenv', TRUE)->Body->children('', TRUE)->loginResponse->result;
// Now that we have the <loginResponse> lets take a look at an item within it
echo $login_response->userInfo->userEmail;

最后,也很重要的是,你看过salesforce的工具和示例了吗?


2

SimpleXML需要特殊处理来处理命名空间的XML (参考)


1

伙计,

命名空间通常需要使用 children 进行调用才能返回带有命名空间的元素。我建议使用像 PHP SoapClient 这样的 SOAP 客户端,但由于我以前从未使用过它,还有另一种可能的选择。

$results = simplexml_load_string($data);
$xml = $results->children('http://schemas.xmlsoap.org/soap/envelope/');
var_dump($xml);

我相信那就是它的工作方式。


0

对于SAOP请求,我们可以通过用短代码将SOAP-ENV:标签替换为空来轻松解析。

$response = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>';

$response = html_entity_decode($response);
$response = str_replace(['soapenv:', 'ns1:', ':ns1', 'SOAP-ENV:'], ['', '', '', ''], $response);
$objXmlData = simplexml_load_string($response);

0

我尝试按照salathe的语法来编写代码。但是children('soapenv', TRUE)在我的情况下不起作用,而Jason的children('http://schemas.xmlsoap.org/soap/envelope/')可以正常运行。

因此,为了读取Salesforce Outbound Message中的CreatedDate字段值,我需要以下代码:

$rcXML->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://soap.sforce.com/2005/09/outbound')->notifications->Notification->sObject->children('urn:sobject.enterprise.soap.sforce.com')->CreatedDate

为了帮助您理解它的工作原理,我写了一篇带有相同代码和XML的文章,这样应该更容易理解。

http://amigotechnotes.wordpress.com/2013/11/16/parse-xml-with-namespace-by-simplexml-in-php/


0

使用SimpleXML解析SOAP响应提供了一个多命名空间XML解析的精彩而简洁的示例。

对于任何想要获取UPS评级API中的RateResponse的人,以下是方法:

// $client is your SoapClient object

$dom = new DOMDocument;

$dom->loadXML($client->__getLastResponse());

$xml = simplexml_load_string($dom->saveXML(), NULL, NULL, 'http://schemas.xmlsoap.org/soap/envelope/');

$RateResponse = $xml->xpath('/soapenv:Envelope/soapenv:Body')[0]->children('rate', true)->RateResponse;

foreach($RateResponse->RatedShipment as $RatedShipment) {
    print_r((array)$RatedShipment);
}

0

0

同时,还可以查看PHP Toolkit,用于向Salesforce.com发起SOAP调用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接