1

It may be simple, but I am kind of stuck. I want to convert an XML string into PHP object. My XML string is:

$a = '<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <soap:Body>
                <VerifyTxnResponse xmlns="http://www.exmlplekhe.com/">
                    <VerifyTxnResult>BID &lt;11467&gt;</VerifyTxnResult>
                </VerifyTxnResponse>
            </soap:Body>
        </soap:Envelope>';

I have tried var_dump(simplexml_load_string($a));, but it returns empty SimpleXMLElement object. I want to get VerifyTxnResult node. I think, &lt;11467&gt; is causing the problem. What may be the possible solution?

Thanks.

6
  • 1
    The function returns a SimpleXMLElement object. What do you mean by "empty result"? And what are you trying to do? Commented Dec 19, 2016 at 10:23
  • What exactly it returns? Commented Dec 19, 2016 at 10:23
  • @ruslan It returns empty SimpleXMLElement object Commented Dec 19, 2016 at 10:25
  • I am trying to get the VerifyTxnResult node. Commented Dec 19, 2016 at 10:30
  • Try print result with var_dump($a); Commented Dec 19, 2016 at 10:33

1 Answer 1

2

I want to get VerifyTxnResult node

The simplexml_load_string function returns an instance of SimpleXMLElement which is actually not empty for the XML you posted.

Register the namespace and fetch the node with xpath method:

$se = simplexml_load_string($a);

$se->registerXPathNamespace('r', 'http://www.nibl.com.np/');

foreach ($se->xpath('//r:VerifyTxnResult') as $result) {
  var_dump((string)$result);
}

Sample Output

string(11) "BID <11467>"
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.