I've run into this before. There is a workaround. The data is there,
it just isn't accessible via normal methods.
Use the ->attributes() function to access them.
<?php
$xmlstr = <<<XML
<user>
<name first_name="John" last_name="White">John White</name>
<phone area_code="801" prefix="555" subscriber="1234"></phone>
</user>
XML;
$xml = simplexml_load_string($xmlstr);
print_r($xml);
print_r($xml->name[0]->attributes());
# Normal way
print $xml->phone[0]['prefix'] . "\n";
# Odd way
foreach ($xml->name[0]->attributes() as $k => $v) {
print "$k = $v\n";
}
On Fri, Jul 12, 2013 at 4:17 PM, James Noble <[email protected]> wrote:
> I am having issues with some XML I receive from a third party. In the XML
> they populate both the content and the attributes of the XML. When I use
> simplexml_load_file I loose all the attributes for any tags that have data
> in the content area. Below is a very simple example and yes I know in this
> instance I can just combine the data.
>
> <user>
> <name first_name="John" last_name="White">John White</name>
> <phone area_code="801" prefix="555" subscriber="1234"></phone>
> </user>
>
> When I dump this file to the screen I get
> [0]=>SimpleXMLElement Object
> (
> [Name]=>John White
> [phone]
> (
> [@attributes]=>Array
> (
> [area_code]=> 801
> [prefix]=> 555
> [subscriber]=> 1234
> )
> )
> )
>
> Is there a way to get the attributes first_name and last_name as well as
> the content from simplexml_load_file?
>
> _______________________________________________
>
> UPHPU mailing list
> [email protected]
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
_______________________________________________
UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net