1
<?xml version='1.0'?> 
<error>
<error_detail code="0">Successfully</error_detail>
<error_detail code="1">Invalid Username or Password</error_detail>
<error_detail code="2">No username or password</error_detail>
<error_detail code="3">Session has expired</error_detail>
<error_detail code="4">Date of effectivity cannot be less than</error_detail>
</error>

How can I get the "session has expired" using xpath?

I tried:

$xml   = simplexml_load_string($xml_string);
$a     = $xml->xpath("//error_detail[@code='3']");
display_output($a);

I am getting this instead:

array ( 0 => SimpleXMLElement::__set_state(array( '@attributes' => array ( 'code' => '53', ), )), )
1

1 Answer 1

1

The xpath query returns an array even if the query returns just a single element. You need to convert this first element to string in order to obtain the text value:

$xml   = simplexml_load_string($xml_string);
$a     = $xml->xpath("//error_detail[@code=3]");

// convert to string though . ''
var_dump($a[0] . ''); // string(19) "Session has expired"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.