0

Im attempting to echo/assign a variable to the contents of the node "code" which is inside status;

I can get request-id just fine...

Any ideas people?

<?
$responseXML = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<payment xmlns="http://www.example.com" self="http://www.example.com">
  <merchant-account-id ref="http://www.example.com">0000</merchant-account-id>
  <transaction-id>0000</transaction-id>
  <request-id>0000</request-id>
  <transaction-type>auth</transaction-type>
  <transaction-state>success</transaction-state>
  <completion-time-stamp>2015-12-28T17:39:25.000Z</completion-time-stamp>
  <statuses>
    <status code="201.0000" description="3d-acquirer:The resource was successfully created." severity="information"/>
  </statuses>
  <avs-code>P</avs-code>
  <requested-amount currency="GBP">0.01</requested-amount>
  <account-holder>
    <first-name>test</first-name>
    <last-name>test</last-name>
    <email>[email protected]</email>
    <phone>00000000000</phone>
    <address>
    <street1>test</street1>
    <city>test test</city>
    <state>test</state>
    <country>GB</country>
    </address>
  </account-holder>
  <card-token>
    <token-id>000</token-id>
    <masked-account-number>000000******0000</masked-account-number>
  </card-token>
  <ip-address>192.168.0.1</ip-address>
  <descriptor></descriptor>
  <authorization-code>000000</authorization-code>
  <api-id>000-000</api-id>
</payment>';

$doc = new DOMDocument;
$doc->loadXML($responseXML);

echo $doc->getElementsByTagName('request-id')->item(0)->nodeValue;
echo $doc->getElementsByTagName('status code')->item(0)->nodeValue;



?>

I've tried simplexml looad string, but pulling hair out with this one, can anybody shed some light, speed of getting this info out in one process is quite important so not to stress the webserver out!

Many thanks.

3
  • code is an attribute of status Commented Dec 28, 2015 at 18:50
  • so how do i grab that? Commented Dec 28, 2015 at 19:00
  • echo $doc->getElementsByTagName('status')->item(0)->getAttribute('code'); Commented Dec 28, 2015 at 19:05

3 Answers 3

1

Using DOM is a good idea, but the API methods are a little cumbersome. Using Xpath makes it a lot easier.

Xpath allows you to use expressions to fetch node lists or scalar values from a DOM:

$document = new DOMDocument;
$document->loadXML($responseXML);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('example', 'http://www.example.com');

echo $xpath->evaluate('string(//example:request-id)'), "\n";
echo $xpath->evaluate('string(//example:status/@code)');

Output:

0000
201.0000

Xpath does not have a default namespace so if you XML has a namespace (like your example) you need to register a prefix for it and use it.

Sign up to request clarification or add additional context in comments.

Comments

0

As code is an attribute of xml tag status, doing

getElementsByTagName('status code')

is wrong.

There's a special method for getting attribute value getAttribute:

echo $doc->getElementsByTagName('status')->item(0)->getAttribute('code');

Comments

0

Using XPath allows to access the status node very precisely.

DOMDocument + XPath:

$responseXML = '...';

$doc = new DOMDocument();
$doc->loadXML($responseXML);

$xp = new DOMXpath($doc);
$xp->registerNamespace('example', 'http://www.example.com');

// Every status node.
$statusNodes = $xp->query('//example:status');
// or a very specific one.
$statusNodes = $xp->query('/example:payment/example:statuses/example:status');

$statusNode = $statusNodes[0];

$code = $statusNode->getAttribute('code');
// $code is '201.0000'.

// To change the 'code' value.
$statusNode->setAttribute('code', '302.0000');

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.