0

I am trying to use php to work with several xml files. I have read through the explanations and some examples on php simpleXML website but I am having trouble getting what I want out of the xml.
I have no control over the xml. Here is a snippet of the xml file:

<end-user-emails>
    <user email="test@testemails.com"/>
</end-user-emails>

A snippet of code I currently have:

$result = $xml->xpath("end-user-emails/user[@email]");
print_r($result[0][email]);

Which outputs:

SimpleXMLElement Object ( [0] => test@testemails.com ) 

I cannot find a way to simply return attribute value.
I have tried casting it to a string and get errors. I have tried several variations of:

$result = $xml->end-user-emails[0]->user[0]->attributes();

and it tells me, despite the previous output, that I cannot call attributes() because it is not being called on an object.

So, if anyone could let me know how to grab both the attribute name and value from the xml it would be much appreciated. The attribute name is not nessasary but I would like to use it so I can check that I am in fact grabbing an email, something like:

if attributeName = "email" then $email = attributevalue
Jake Sellers
  • 2,350
  • 2
  • 21
  • 40
  • 1
    Why don't you use `$result[0]['email'][0]`? – Ties Apr 03 '13 at 19:56
  • Tried it, no change in output. – Jake Sellers Apr 03 '13 at 20:00
  • Not sure, and may be completely irrelevant, but are dashes allowed in element-names in XML? **update** never mind, dashes are allowed, just not as first character. – thaJeztah Apr 03 '13 at 20:06
  • The dashes are not part of the element names, they are closing the elements. It works exactly like html: for example. – Jake Sellers Apr 03 '13 at 20:08
  • If was talking about dashes (`-`), not slashes (`/`), but looked it up, it is a valid character for a tagName. **However** they may cause problems when accessing them as property via php. This question answers that situation: http://stackoverflow.com/questions/758449/how-do-i-access-this-object-property – thaJeztah Apr 03 '13 at 20:11
  • oops lol, ya dashes are fine – Jake Sellers Apr 03 '13 at 20:15

2 Answers2

2

The attributes() method will return an array like object so this should do what you want, but only gonna work with php 5.4+

$str = '
<end-user-emails>
    <user email="test@testemails.com"/>
</end-user-emails>';
$xml = simplexml_load_string($str);
// grab users with email
$user = $xml->xpath('//end-user-emails/user[@email]');
// print the first one's email attribute
var_dump((string)$user[0]->attributes()['email']);

If you are on php5.3, you will have to loop over the attributes() like this:

foreach ($user[0]->attributes() as $attr_name => $attr_value) {
    if ($attr_name == 'email') {
        var_dump($attr_name, (string)$attr_value);
    }
}

You could just assign the return value of ->attributes() and use ['email'] on that variable too. The looping also useful if you dont know the attribute names beforehand.

complex857
  • 20,425
  • 6
  • 51
  • 54
1

To get the user's email address (in your example) load the xml into an object and then parse over each item. I hope this helps.

//load the data into a simple xml object
$xml = simplexml_load_file($file, null, LIBXML_NOCDATA);
//parse over the data and manipulate
foreach ($xml->action as $item) {
    $email = $item->end-user-emails['email'];
    echo $email;

}//end for

For more information see http://php.net/manual/en/function.simplexml-load-file.php

ringocub
  • 427
  • 1
  • 4
  • 7