views:

35

answers:

1

Just trying to figure a shorter way to do this:

I'm using simpleXMLElement to parse an xml file and it's aggravating to have to call two lines to process an array when I know what node I want.

Current code:

$xml = new SimpleXMLElement($data);
$r = $xml->xpath('///givexNumber');
$result["cardNum"] = $r[0];

What I would like to do would be something like I can do with DomX

$result["cardNum"] = $xml->xpath('///givexNumber')->item(0)->nodeValue;

Any ideas?

I didn't really see that simplexmlelement can do this, but thought someone might know a trick or two.

+1  A: 

I don't know php too well, but shouldn't:

$result["cardNum"] = new SimpleXMLElement($data)->xpath('///givexNumber')[0]

be the same as

$xml = new SimpleXMLElement($data);
$r = $xml->xpath('///givexNumber');
$result["cardNum"] = $r[0];
Maxem
One would think. But $r = $xml->xpath('///givexNumber')[0] does not work.
Senica Gonzalez