|
|||||||
| PHP Discussion and technical support for using and deploying PHP based websites. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
weird named array key causing problems
hi all
Im using the simplexml_load_file function here. There is an attribute in the xml named @attributes which but in using the code below: $stat = $xml->@attributes->stat; throws me an error, of course, unexpected '@'. How can i get around this? |
|
#2
|
||||
|
||||
|
What about replacing @attribute with another before parsing..
$xml = str_replace('@attributes','a_t_attributes',$xml); $stat = $xml->a_t_attributes->stat; |
|
#3
|
||||
|
||||
|
I believe the "@" is an artifact of simpleXML, not part of the actual XML data being parsed. You should be able to access an attribute directly from the element, e.g.: $xml->elementName['attributeName']. See "Example #5" on the SimpleXML Examples page.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett freelancer.internet.com Email me |
|
#4
|
|||
|
|||
|
I think your right that the '@' is from the simple xml..
However im still none the wiser to how i can access the data. A print_r() of the whole simpleXML read is as folllows... Code:
SimpleXMLElement Object ( [@attributes] => Array ( [stat] => ok ) [auth] => SimpleXMLElement Object ( [token] => xxx [perms] => x [user] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => x [username] => x [fullname] => xx ) ) ) ) edit: Heres the actual xml i get back.. Code:
<?xml version="1.0" encoding="UTF-8"?> <rsp stat="ok"><auth><token>xxx</token><perms>x</perms><user id="x" username="x" fullname="xx"/></auth></rsp> Last edited by benbramz; 11-07-2009 at 08:40 PM. Reason: adding xml |
|
#5
|
|||
|
|||
|
Quote:
I didnt get at first what you meant, but now the penny just dropped. Thanks a lot! |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|