Am trying to access the attribute of the simple xml
SimpleXMLElement Object
(
[#attributes] => Array
(
[Index] => 21
)
[Data] => Hello world
)
i want to access Index attribute. I tried the following code but its not
working for me
$xml->attributes()->Index
There you go my friend:
$xml->attributes()['Index']
Sometimes I noticed I had to cast the result when it's a string. Like:
(string)$xml->attributes()['Index']
Related
I have a RSS object $rssObject created using the PHP simplexml_load_file function, the goal is to get the value of [href].
var_dump($rssObject) returns the following:
Array
(
[0] => SimpleXMLElement Object
(
[link] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => https://www.example.com
)
)
I have tried unsuccessfully to get the contents of [href] using this notation which returns null
$rssObject[0]->link[0]->{'#attributes'}['href'];
Not sure why?
Any help would be appreciated!
In SimpleXML, attributes are accessed using array notation:
$xml = simplexml_load_file();
$url = $xml[0]->link[0]['href'];
See "Example #5 Using attributes" in the PHP manual.
I'm trying to access values from a PHP object returned from an API. I am trying to get the 'total' atribute.
stdClass Object
(
[#attributes] => stdClass Object
(
[status] => ok
)
[invoices] => stdClass Object
(
[#attributes] => stdClass Object
(
[page] => 1
[per_page] => 25
[pages] => 1
[total] => 5
)
My returned object is stored in a variable called $list.
$list->invoices->attributes->total
I'm trying to echo / print_r that, but getting nothing?
Any help is appreciated!
The # is a part of the property name, you can't just ignore it.
echo $list->invoices->{'#attributes'}->total;
$total = $list->invoices->attributes()->total;
As it turns out, you don't need to even specify #attributes to read this data. The key trick to get to it is to cast the result as a string.
So, I know it seems strange, but this will work in this case:
echo (string)$list->invoices['total'];
In my case i used:
$att_side = $xml->item->attributes()->side;
This is driving me crazy, I am trying to get to a specific part of this object and it is driving me crazy, here is the object contents:
XMLHandler Object
(
[doc:XMLHandler:private] => SimpleXMLElement Object
(
[#attributes] => Array
(
[state] => Live
)
[newsListItem] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://api.contentplus.co.uk/6cb5ea15-d6b1-4c40-9db7-cb2a3315080b/news/800773226/
)
[id] => 800773226
[publishDate] => 2011-10-24T10:04:49
[lastModifiedDate] => 2011-10-24T11:20:40
[headline] => Relationships matter on social media
)
)
)
[format] => html
)
I want to get the value of [id] I am trying to access it like this:
echo $niList->doc->newsListItem[0]->id;
but this is giving me nothing, I know I am close (well I hope I am) but I just cant quite get it right, could anyone help please.
Thanks all.
Your object dump says
doc:XMLHandler:private
which means doc is a private property of XMLHandler. As such, you can only access it from within that object via $this. But you are trying to access it from outside the object when you do
echo $niList->newsListItem[0]->id;
This wont work. Add a method to that XMLHandler object that does what you want to do with that newslistitem id. Also see the chapter on Visibility in the PHP Manual:
http://docs.php.net/manual/en/language.oop5.visibility.php
I've got a response form the solr query in php. below is the response form apache solr in drupal6, i need to access the id field inside the Apache_Solr_Document, can some bosy help me with this.
i was able to print this using print_r($result);
Array ( [type] => Bookmarks [node] => Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 )))
if i do print_r($result[node]); i am getting
Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 ))
from here i can't figure out how to access the id.
Have you tried reading through IBM's article on Solr? The end of the article deals specifically with PHP.
Edit: After finding a source class to read through, it looks like you can do:
$result['node']->getField("id");
or using the magic __get:
$result['node']->id;
You can try using the {} brackets if the object name is not a valid object property name (like SimpleXML stuff).
$object->{'strang&$*#nmame'}->value;
I have some xml, lets say <names number="12"></names>
When I run the following:
$simpleXMLElement = new SimpleXMLElement($xml);
pr($simpleXMLElement);
I get the following:
SimpleXMLElement Object
(
[#attributes] => Array
(
[number] => 12
)
[0] =>
)
It throws in that 0 entry. This is weird. I don't know what it's supposed to represent. If I do this instead:
<names number="12"><name first="oliver" /></names>
I get the following output:
SimpleXMLElement Object
(
[#attributes] => Array
(
[number] => 12
)
[name] => SimpleXMLElement Object
(
[#attributes] => Array
(
[first] => oliver
)
)
)
This is as expected (for me at least). Any thoughts/direction?
First: if you don't correctly format your post, the XML will not be displayed. Indent any code with at least 4 spaces.
Secondly, do not expect print_r() or var_dump() to give you an exact representation of a SimpleXMLElement because SimpleXML uses lots of magic, so children and attributes won't necessarily show up in the output.
It seems to be just SimpleXML doing a quick-and-dirty job of parsing the element: Since you have <names></names>, it adds an array inside the element, as expecting elements within it, and when it doesn't find any elements inside the names tags, it leaves an empty array, with the key 0, since it doesn't know what name to give it.
A short tag (<names />) shouldn't generate the empty content. (As weird as that sounds.)