I am learning PHP SimpleXML and I have some questions.
I have been playing to get code from a web in the intranet of my work. I need generic code whenever its possible, since the code could change at any time.
In my example I select a div tag and all its children.
... <div class="cabTabs"> <ul> <li><a href="/link1">Info1</a></li> <li><a href="/link2">Info2</a></li> <li><a href="/link3">Info3</a></li> </ul> </div> ... //Get all web content: $b = new sfWebBrowser(); //using symfony 1.4.17 sfWebBrower to get a SimpleXML object. $b->get('http://intranetwebexample'); //returns a sfWebBrower object. $xml = $b->getResponseXML(); //returns a SimpleXMLElement //[Eclipse xdebug Watch - $xml] "$xml" SimpleXMLElement @attributes Array [3] head SimpleXMLElement body SimpleXMLElement //Get the div class="cabTabs". $result = $xml->xpath('//descendant::div[@class="cabTabs"]'); //[Eclipse xdebug Watch - $result] "$result" Array [1] 0 SimpleXMLElement @attributes Array [1] class cabTabs ul SimpleXMLElement li Array [6]
Questions:
- The use of descendant:: prefix:
I have read in other stackoverflow topics that descendant:: prefix is not recommended.
In order to select a tag, and all its content, what should be the right way to do it?
Im using the above code, but dont know if its the right way to do it. - Some questions checking the Eclipse xdebug variable Watch:
2.1 Some times I cant expand the SimpleXML tree more than one or levels. In the example above, I cant access/see the below "li" node, and see its children.
Could it be a limitation of xdebug debugger with SimpleXML objects or maybe a limitation of the Eclipse Watch?
I can perfectly expand/see the "li" node when I access its parent with the usual loop: foreach($ul->li as $li).
However its not a critical bug, I think it would be perfect to see it directly and report it in the proper forum.
2.2 I dont understant at all the result code of the $xml->xpath:
If we take a look at the Eclipse Watch, the "div" tag has been converted to a 0 index key, but the "ul" and "li" tags had their original names, why?
2.3 How to access/loop xpath content with a generic code:
Im using the following Non generic code to access it:
foreach ($result as $record) { foreach($record->ul as $ul) { foreach($ul->li as $li) { foreach($li->a as $a) { echo ' ' . $a->name; } } } }
The above code works but only if we write the right tag names. (->ul, ->li, ->a..)
What is the generic way to loop through all its content without having to specify the children name each time? (->ul, ->li, ->a..)
Also I would prefer not having to convert it to an array, unless its the right way.
I have been trying with children() property, but it doesnt work, it stops and crashes in that line: foreach ($result->children() as $ul)
Thank you a lot in advance for taking your time to read my questions. Any help is really welcome 🙂
System info:
symfony 1.4.17 with sfWebBrowserPlugin, cURL dadapter.
PHP 5.4.0 with cURL support enabled, cURL Information 7.24.0
I dont know I’ve never used it myself
dont know i usually use Zend Debug – but i dont understand your question anyway… i think you left out some words 🙂
2.1 PRobably xdebug/eclipse. Id check preferences theres probably a setting to limit the amount of recursion to help manage memory.
2.2
SimpleXML::xpathAlways returns an array of matched Nodes. Thats why you have integer index array as your result. So if you do//someelementyou get an array of allsomeelementtags. You can then access their descendents in the normal fashion like$someelement->itschildelement.2.3
$result->children()is a good way to get at things in a generic sense. If Xdebug is crashing thats just xdebug. Either turn it off, ignore it, or find a different debugger 🙂 Xdebug is jsut a tool but shouldnt dictate how you implement things.