Scripta55
Scripta55

Reputation: 49

simplexml_load_string is stripping data out of soap response

So i am hitting a soap service, when i get the data and parse it through simplexml_load_string in order to access the data as an object (or just basically access the data) simplexml_load_string seems to strip it out.

A Raw response from soap service looks like:

enter image description here

A result parsed through simplexml_load_string using the following code:

    $result = simplexml_load_string((string)$result->DisplayCategoriesResult->any);

i get a result of:

enter image description here

this looks correct but at a closer look you will notice its just id's and the names of the categories are left behing simplexml_load_string

how can i manage to get the proper result? if there is another way of getting the raw data into a "usable" form or object that solution is also welcome

Upvotes: 0

Views: 198

Answers (2)

Petko Kostov
Petko Kostov

Reputation: 367

The category names are CDATA. Try something like this to read it.

$doc = new DOMDocument();
$doc->load((string)$result->DisplayCategoriesResult->any);
$categories  = $doc->getElementsByTagName("categories");
foreach ($categories  as $categorie) {
    foreach($categorie->childNodes as $child) {
        if ($child->nodeType == XML_CDATA_SECTION_NODE) {
            echo $child->textContent . "<br/>";
        }
    }
}

Upvotes: 0

iainn
iainn

Reputation: 17434

The text content of XML nodes doesn't show up when using print_r or var_dump, etc. They aren't "traditional" PHP objects, so you can't use the standard debugging options.

To access the text content (whether embedded as CDATA or otherwise), you need to step down into the child elements, and then cast them to strings:

<?php
$xml = <<<XML
<randgo xmlns="">
    <status>0</status>
    <message>Success</message>
    <categories>
        <category id="53"><![CDATA[go eat]]></category>
        <category id="54"><![CDATA[go do]]></category>
        <category id="55"><![CDATA[go out]]></category>
    </categories>
</randgo>
XML;

$sxml = simplexml_load_string($xml);

foreach ($sxml->categories->category as $category)
{
    echo $category['id'] . ": " . (string) $category, PHP_EOL;
}

=

$ php simplexml_categories.php
53: go eat
54: go do
55: go out

See: https://eval.in/590975

(Sorry if there are any typos in the XML, I think I copied from the screenshot correctly...)

Upvotes: 1

Related Questions