php - SimpleXml can't access children -


this snippet of xml i'm working with:

<category name="pizzas">     <item name="tomato &amp; cheese">         <price size="small">5.50</price>         <price size="large">9.75</price>     </item>     <item name="onions">         <price size="small">6.85</price>         <price size="large">10.85</price>     </item>     <item name="peppers">         <price size="small">6.85</price>         <price size="large">10.85</price>     </item>     <item name="broccoli">         <price size="small">6.85</price>         <price size="large">10.85</price>     </item> </category> 

this php looks like:

$xml = $this->xml; $result = $xml->xpath('category/@name'); foreach($result $element) {     $this->category[(string)$element] = $element->xpath('item'); } 

everything works ok except $element->xpath('item'); tried using: $element->children(); other xpath queries, return null. why can't access children of category?

it looks you're trying build tree based on categories, keyed category name. that, should change code this:

$xml = $this->xml;  //here, match category tags themselves, not name attribute. $result = $xml->xpath('category');  foreach($result $element) {    //iterate through categories. name attributes     //category array key, , assign item xpath result that.    $this->category[(string)$element['name']] = $element->xpath('item'); } 

with original code here: $result = $xml->xpath('category/@name'); result name attribute nodes, which, attributes, cannot have children.

now if wanted list of items, use $xml->xpath('category/items'), doesn't appear had wanted.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -