0

I'm trying to read in an attribute of an XML object in PHP and cannot get the value.

The XML looks like this:

<TransResult>
  <ResultCode tc="5">Failure</ResultCode>
  <ResultInfo>
    <ResultInfoCode tc="200"/>
    <ResultInfoDesc>Product not available</ResultInfoDesc>

I've tried:

$resultInfoCode=$xml->TransResult->ResultInfo->ResultInfoCode;
$resultInfoCode=$xml->TransResult->ResultInfo->ResultInfoCode['tc'];

And several other variations of those but it either returns null or throws an error.

Any help is greatly appreciated.

6
  • TransResult is the root element and must not be part of the path. Have a look at my example. Commented Jun 21, 2018 at 20:12
  • Again, I've tried to oversimplify. 'TransResult' is not the root element - I've left out several layers because they contain proprietary info. Commented Jun 21, 2018 at 20:20
  • @user3850146 I have updated my answer with your new xml. Commented Jun 21, 2018 at 20:45
  • @The fourth bird Sorry, that did not work. Commented Jun 21, 2018 at 20:56
  • 1
    Please edit your question to include a minimal reproducible example. The general approach you have tried is correct, so if this is not the actual XML and code you are trying, there is no way for us to guess what is wrong. Commented Jun 21, 2018 at 21:33

2 Answers 2

1

If you just have this xml tag inside a SimpleXMLElement the array notation works for attribute access.

$xml = new SimpleXMLElement('<ResultInfoCode tc="200"/>');
echo (string)$xml['tc'];

If ResultInfoCode is nested, just use the object access notation and then the array access.

$xml = new SimpleXMLElement('<data><ResultInfoCode tc="200"/></data>');
echo (string)$xml->ResultInfoCode['tc'];

This should work in all PHP Versions (tested with 5.6 - 7.3). If you get an error or null, you don't have the correct tag selected or there is something else wrong.

In your case, you have to use

$resultInfoCode = $xml->ResultInfo->ResultInfoCode['tc'];
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, it is very nested - I omitted most of it for clarity. That backfired! I edit my post to indicate such. $resultCode = $xml->TransResult->ResultCode; returns "Failure" and $resultInfoDesc = $xml->TransResult->ResultInfo->ResultInfoDesc; returns its value. But ResultInfoCode['tc'] does not.
It does not use SimpleXMLElement - maybe that has something to do with it? It appears to use XMLNS.
@user3850146 what type has $xml? Without that info, it's hard to tell, how you could solve this issue..
I'm new to this and inherited this project. I'm not sure of the answer to that. $xml = $client->processRequest($params); where $client = new CustomSoapClient and $params = new \SoapVar($xml,XSD_ANYXML);
A var_dump of $xml should tell you the var type
|
1

Your value is in the attributes.

Try it like this:

echo $xml->attributes()->tc;

Demo

attributes returns an object of type SimpleXMLElement which has a method __toString so you can use echo to return the string content.

Edit for your updated data:

You might use xpath:

$item = $xml->xpath('//TransResult/ResultInfo/ResultInfoCode')[0]->attributes()->tc;
echo $item;

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.