Simplexml and Print_R() - Why Is This Empty

Empty SimpleXML object?

You shouldn't use print_r() or var_dump() to debug a SimpleXMLElement. It won't output the full XML object. If you want to see the complete XML output, use the asXML() method instead.

And for debugging this, I'd suggest using IMSoP's simplexml_debug() / simplexml_tree() functions. You can get it here: http://github.com/IMSoP/simplexml_debug

Why does SimpleXMLElement returns a empty object?

The problem was with the webservice, it was returning a Soap Object, but I was able to see it just in the source-code. As the XML was showing ok, I wasn't thinking there was a problem with the request. After checking the source-code and using Soap to handle the requests, I could figure it out. SimpleXML works nice.

Thanks to everyone for the help.

simpleXML returns empty element


How can I access all <a:article> in <f:articles> ?

To me simpleXML seems a bit clumsy when working with namespaces (but then again, I'm not a PHP coder). You could use the children method but in my opinion using XPath is simpler.

$feed->registerXPathNamespace('f', 'http://www.bbgo.de/feedxml/feed');
$feed->registerXPathNamespace('a', 'http://www.bbgo.de/feedxml/article');
$article = feed->xpath("/f:feed/f:articles/a:article");

First you need to register the namespaces with some prefixes (prefixes don't need to match the ones used in the XML document but namespace URIs must match) and then use these prefixes in your XPath expression that selects your target. Without the element name prefixes the XPath expression would look for elements that are not in any namespace.

Why $feed array is not built?

It's hard to guess a reason for this. Some things you could test:

  • is the string $xml a well-formed xml document
  • does creating a SimpleXMLElement with explicit string parameter work $feed = new SimpleXMLElement('<root><child/></root>');
  • can you load your document using simplexml_load_file($yourXMLfile); or simplexml_load_string($yourXMLstring);

SimpleXML - echo / print_r return different values


Why are these different

Because these variables are not strings internally, but SimpleXMLElement type objects that get converted into strings when output by echo.

To use the values elsewhere,I usually do an explicit cast:

$bg_color = (string) $image['bgColor'];

A canonical question regarding casting a simplexml element into a string is here:

  • Forcing a SimpleXML Object to a string, regardless of context

simplexml_load_string() returning a empty object

I tried following to list all products from the XML

 $response = '<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Body><ns1:ProductSearchResponse><ns1:TestMode>false</ns1:TestMode><ns1:Page>1</ns1:Page><ns1:ResultsPerPage>1</ns1:ResultsPerPage><ns1:TotalResults>1276</ns1:TotalResults><ns1:Items><ns1:Item><ns1:StockCode>L3720</ns1:StockCode><ns1:Name>David Beckham Instinct Gift Set 30ml EDT + 150ml Shower Gel</ns1:Name><ns1:QuantityAvailable>1</ns1:QuantityAvailable><ns1:UnitPrice Currency="GBP"><ns1:Amount>8.72</ns1:Amount></ns1:UnitPrice><ns1:YourRating xsi:nil="true"/><ns1:YourStockCode></ns1:YourStockCode><ns1:ImageLastUpdated>2016-12-22 18:13:08</ns1:ImageLastUpdated><ns1:ThumbnailImageUrl>https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D</ns1:ThumbnailImageUrl><ns1:HighResImageUrl xsi:nil="true"/></ns1:Item></ns1:Items></ns1:ProductSearchResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>';

$xml = simplexml_load_string(($response));
$posts = $xml->children('SOAP-ENV', true)->Body->children('ns1', true)->ProductSearchResponse;

SimpleXML xpath has empty element

Ok, so I've figured out what was happening, and leaving this here in the hopes that anyone who stumbles across the same issue will find this answer helpful. Thanks to IMSoP for helping me get to the answer.

First, SimpleXML's xpath() method returns an array, not a XML object, which makes life difficult from the outset. Second of all, if you look at the <notifications> tag, you will see it uses the outbound namespace, which in my example I aliased to ob. As such, both Notification and sObject are also considered to be part of the same namespace as they are children of the namespaced element, which means the correct xpath is

//soapenv:Envelope/soapenv:Body/ob:notifications/ob:Notification/ob:sObject

Which, as mentioned above, in SimpleXML will return an array, so grab the first index, and use the provided namespace url for sObject in the children() method call and you will get your data.

So using the above example XML

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<notifications
xmlns="http://soap.sforce.com/2005/09/outbound">
<OrganizationId>00DN0000000XXXXXXX</OrganizationId>
<ActionId>04kN0000000XXXXXXX</ActionId>
<SessionId xsi:nil="true"/>
<EnterpriseUrl>https://cs6.salesforce.com/services/Soap/c/38.0/00DN0000000XXXXXXX</EnterpriseUrl>
<PartnerUrl>https://cs6.salesforce.com/services/Soap/u/38.0/00DN0000000XXXXXXX</PartnerUrl>
<Notification>
<Id>00DN0000000XXXXXXX</Id>
<sObject xsi:type="sf:Account"
xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
<sf:Id>00DN0000000XXXXXXX</sf:Id>
<sf:FirstName>Bill</sf:FirstName>
<sf:LastName>Jack</sf:LastName>
<sf:PersonEmail>abc@xyz.net</sf:PersonEmail>
<sf:PersonMobilePhone>0400000000</sf:PersonMobilePhone>
</sObject>
</Notification>
</notifications>
</soapenv:Body>
</soapenv:Envelope>

We can get the data by doing the following:

$xml = simplexml_load_string($xmlString);

$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('ob', 'http://soap.sforce.com/2005/09/outbound');

$sObject = $xml->xpath('//soapenv:Envelope/soapenv:Body/ob:notifications/ob:Notification/ob:sObject')[0];

$data = $sObject->children('urn:sobject.enterprise.soap.sforce.com');
var_dump($data);


Related Topics



Leave a reply



Submit