Search code examples
phpsimplexml

SimpleXML returns an object instead of a string


I have some XML that looks like this:

<Body>
    <RESULT>
        <SUCCESS>true</SUCCESS>
        <SESSIONID>42</SESSIONID>
        <SESSION_ENCODING>;jsessionid=42</SESSION_ENCODING>
    </RESULT>
</Body>

SimpleXML parses it, in this way:

$obj = simplexml_load_string($xmlStringAsAbove);
$this->sessionId = $obj->Body->RESULT->SESSIONID;

and the result is this:

[sessionId:protected] => SimpleXMLElement Object
    (
        [0] => 42
    )

What I need is this:

[sessionId:protected] => 42

How can I achieve this?


Solution

  • Cast it to a string (or an int) to get the contents of the <SESSIONID> tag:

    $this->sessionId = (string) $obj->Body->RESULT->SESSIONID;