2010-05-05 82 views
3

我沒有做過任何XML項目,所以我不太清楚該怎麼處理這些數據...simplexml幫助我如何解析這個?

我使用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> 
[email protected] 
</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才能正確讀取它。
或其他的東西讓我覺得這是因爲我沒有在頂部。

幫助!

+0

也許在閱讀文件的內容時出現錯誤....是否啓用了E_WARNING?也許你可以看到發生了什麼。 – Cristian 2010-05-05 21:23:02

+0

是的,我將它設置爲E_ALL – bbutle01 2010-05-05 21:38:46

回答

1

由於使用了命名空間(例如soapenv),您可以使用SimpleXML,但它不像簡單。考慮使用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的tools & examples

2

SimpleXML的需求爲XML命名空間(ref.

+0

+1 - 良好的信息! – Josh 2010-05-05 21:43:52

0

對於它的價值了特殊處理,你會發現你有使用PHP SoapClient這個任務更容易的時間。 O'Reilly在PHP SOAP上有一個good tutorial

1

伴侶,

名稱空間通常需要使用你的孩子返回命名空間的元素來撥打電話。我會建議使用肥皂客戶端,如php soapclient,但由於我從來沒有使用過,所以還有另一種可能的選擇。

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

我相信這就是它的工作原理。

0

我嘗試按照salathe的語法。但孩子('soapenv',TRUE) doens't爲我工作,傑森的兒童('http://schemas.xmlsoap.org/soap/envelope/')工作。

因此,閱讀在Salesforce外發郵件的字段值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

Parsing soap responses with SimpleXML具有多命名空間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

對於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);