The var_dump() output is correct. $xml->StatusCode is a SimpleXMLElement instance. This is of course needed in case you have to, for example, add a child element to it:

$xml->StatusCode->addChild("test", "value");

If $xml->StatusCode contained only the value of the element rather than an instance of SimpleXMLElement, you wouldn't be able to do any modifications on the loaded XML.

So, what you need to do, is cast the value of StatusCode to a string. There are various ways of doing this:

var_dump($xml->StatusCode); // returns SimpleXMLElement instance
var_dump((string)$xml->StatusCode); // explicitly
var_dump($xml->StatusCode->__toString()); // explicitly, calling the magic function
echo $xml->StatusCode; // implicitly

Some demos

Answer from ishegg on Stack Overflow
🌐
PHP
php.net › manual › en › class.simplexmlelement.php
PHP: SimpleXMLElement - Manual
Figuring out how to access the properties of a SimpleXmlElement object was a little tricky for me. In particular, it took a while to discover that I needed to cast my SimpleXmlElement properties to be of type "string" to print them or do comparisons on them. For instance, assuming you already have a string of xml in $xmlstr...
🌐
Tech Solution Stuff
techsolutionstuff.com › post › how-to-read-xml-file-in-laravel-10
How to Read XML File in Laravel 10
April 22, 2024 - By using simplexml_load_string(), we convert the XML string into a SimpleXMLElement object called $xml. We can then access the elements within the XML using object syntax ($xml->elementName) and retrieve their values. By running the above code, you will see the following output: Title: Harry Potter and the Chamber of Secrets Author: J.K. Rowling ... Start your Laravel development server by running the following command.
🌐
GitHub
github.com › aws › aws-sdk-php › issues › 1598
Class 'SimpleXMLElement' not found · Issue #1598 · aws/aws-sdk-php
August 14, 2018 - Hi, My project is in EC2, upon delete any s3 file I am having this error. (1/1) FatalThrowableError Class 'SimpleXMLElement' not found I have PHP 7.2 and XML and SimpleXML both installed in EC2. my local server does not throw this error ...
Author   koushikSen
Find elsewhere
🌐
CodeGenes
codegenes.net › blog › class-simplexmlelement-not-found
Class 'SimpleXMLElement' Not Found in PHP 7 Laravel: Fixing Digital Ocean Spaces Server Error (Works Locally) — codegenes.net
However, Laravel (and its underlying dependencies like the AWS SDK) relies on PHP’s `SimpleXMLElement` class to parse XML responses from cloud storage services. If this class is missing on your server, interactions with Spaces will fail—even if your local setup works.
🌐
Packagist
packagist.org › search
Packagist
Packagist is the main Composer repository. It aggregates public PHP packages installable with Composer · Maintenance and hosting provided by
🌐
Packagist
packagist.org › packages › fetchleo › laravel-xml
fetchleo/laravel-xml - Packagist
* * @param Model $value * @param SimpleXMLElement $element * @return SimpleXMLElement * @throws CantConvertValueException */ public function convert($value, SimpleXMLElement $element) : SimpleXMLElement; /** * Determine if this converter can convert the given value. * * @param mixed $value * @param $type * @return bool */ public function canConvert($value, $type) : bool; } Then, you can configure the new converter in your config/laravel-xml.php file.
Top answer
1 of 3
1

Converting the XML into JSON that way means loosing data. I suggest keeping the XML if possible.

SimpleXMLElement::asXML() is a method. Do not forget the brackets.

$parent = $this->getParent($keywords);
foreach ($parent->children() as $child) {
    dispatch(new ProcessChild($child->asXML(), true), $this->otherVar);
}

Calling it as a property means that SimpleXML tries to interpret it as a child element node. This means it will be an (empty) SimpleXMLElement.

Here is a small example showing the behavior:

$node = new SimpleXMLElement('<foo/>');
var_dump($node->asXml);
var_dump($node->asXml->getName());
var_dump($node->asXml());

Output:

object(SimpleXMLElement)#2 (0) {
}
string(0) ""
string(29) "<?xml version="1.0"?>
<foo/>
"
2 of 3
0

The SimpleXmlElement can be converted to array as follows:

$xml = <<<'XML'
<root>
  <x a="a1">1</x>
  <y b="b2">2</y>
  <z>3</z>
</root>
XML;

$xe = simplexml_load_string($xml);
$a = $xe->xpath('*');
$a = array_map(function ($e) {
  $item = (array) $e;
  $item['nodeName'] = $e->getName();
  return $item;
}, $a);
// Now `$a` is an array (serializable object)
echo json_encode($a, JSON_PRETTY_PRINT);

Output

[
    {
        "@attributes": {
            "a": "a1"
        },
        "0": "1",
        "nodeName": "x"
    },
    {
        "@attributes": {
            "b": "b2"
        },
        "0": "2",
        "nodeName": "y"
    },
    {
        "0": "3",
        "nodeName": "z"
    }
]

Note, you can get the string value of a SimpleXmlElement by casting it to string:

$item['value'] = (string) $e;

Since xpath method supports relative XPath expressions, the asterisk should work even with namespaced XMLs. Consider using the DOM extension, as it is much more flexible than SimpleXML. In particular, its DOMXPath class allows to register namespaces and use the registered identifiers in the XPath expressions:

$xpath->registerNamespace('myprefix', 'http://example.com/ns');
$xpath->query('/root/myprefix:*');
🌐
Diego
diego.com.es › tutorial-de-simplexml
Tutorial de SimpleXML - Diego Lázaro
Editorial notes on stocks, markets, finance, AI-assisted research, and the software around them.
Top answer
1 of 1
1

I'm not sure if I understand You but in major:

  1. You act on while ($node = $streamer->getNode()) loop what means that after first iteration You'll get this object:

SimpleXMLElement Object ( [identifier] => RCM0635 [datestamp] => 2015-06-09 )

so for the first time it's ok to read it like:

` $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->identifier; echo (string)$simpleXmlNode->datestamp;

`

but in the second iteration You have:

`

SimpleXMLElement Object
(
    [lidoWrap] => SimpleXMLElement Object
    (
        [lido] => SimpleXMLElement Object
            (
                [lidoRecID] => RCM:1748
                [descriptiveMetadata] => SimpleXMLElement Object
                    (
                        [objectClassificationWrap] => SimpleXMLElement Object
                            (
                                [objectWorkTypeWrap] => SimpleXMLElement Object
                                    (
                                        [objectWorkType] => SimpleXMLElement Object
                                            (
                                                [term] => musical instruments
                                            )

                                    )

                                [classificationWrap] => SimpleXMLElement Object
                                    (
                                        [classification] => Array
                                            (
                                                [0] => SimpleXMLElement Object
                                                    (
                                                        [term] => Cornet
                                                    )

                                                [1] => SimpleXMLElement Object
                                                    (
                                                        [conceptID] => SimpleXMLElement Object
                                                            (
                                                                [@attributes] => Array
                                                                    (
                                                                        [type] => SH_Class
                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

`

so the code inside while is wrong.

i sugest to try something like this:

`

while ($node = $streamer->getNode()) {

    $simpleXmlNode = simplexml_load_string($node);

    if (!empty($simpleXmlNode->identifier))
        echo (string)$simpleXmlNode->identifier;
    if (!empty($simpleXmlNode->datestamp))
        echo (string)$simpleXmlNode->datestamp;
    if (!empty($simpleXmlNode->lidoWrap)) {
        $lido = $simpleXmlNode->lidoWrap->lido;
        echo (string)$lido->lidoRecID;
        // and so on as the recursive XML node objects
    }
}

`

🌐
Medium
medium.com › @certosinolab › using-php-and-simplexml-to-parse-xml-93df5bd50868
Using PHP and SimpleXML to parse XML | by CertosinoLab | Medium
September 12, 2021 - basically $restaurantMenu is an object of type SimpleXMLElement which contains within it an array with other objects of type SimpleXMLElement
🌐
Cybersource Developer Community
community.developer.cybersource.com › t5 › Integration-and-Testing › XML-namespace-throwing-500-Error › td-p › 74513
XML namespace throwing 500 Error. - Cybersource Developer Community
January 4, 2021 - It is not a warning in Laravel it is a 500 error. ... Authorize.Net uses a relative namespace path which the SDK's XML library complains about. The solution is don't include the xmlns attribute in your construction of the SimpleXML element object and use SimpleXMLElement::addAttribute after the fact to add it.