6114

rss parsing DOMDocument in PHP

Question:

I'm trying to get all categories and push them into my array, so far I'm doing it this way:

$doc = new DOMDocument(); $doc->load('myxml.xml'); $arr = array(); foreach ($doc->getElementsByTagName('item') as $node) { $items = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('category')->item(0)->nodeValue ); $arr [] = $items ; }

This works if we have only 1 cat, however, my xml has several categories per item. What would be a good way of doing this?

<item> <title>Submit</title> <category>Foo</category> <category>Bar</category> </item>

Thanks

Answer1:

This should help:

$doc = new DOMDocument(); $doc->load('myxml.xml'); $arr = array(); foreach ($doc->getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'date' => array() ); foreach($node->getElementsByTagName('category') as $catNode) { $item['date'][] = $catNode->nodeValue; } $arr[] = $item; } <ul><li>Christian</li> </ul>

Answer2:

You need nested loops:

$doc = new DOMDocument(); $doc->load('myxml.xml'); $arr = array(); foreach ($doc->getElementsByTagName('item') as $itemNode) { $items = array( 'title' => $itemNode->getElementsByTagName('title')->item(0)->nodeValue, 'date' => array() ); foreach ($itemNode->getElementsByTagName('category') as $categoryNode) { $items['date'][] = $categoryNode->nodeValue; } $arr[] = $items; }

Recommend

  • Javascript get anchor href on click
  • Dom Node for PHP find href attribute issue
  • getElementsByClassName and AJAX
  • Runtime error in UVA Online Judge [closed]
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • JSON - slashes not escaping
  • Inversing an interpolation of rotation
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • Simple linked list-C
  • List images(01.png) and descriptions(01.txt) from directory
  • ListItem.Attributes.Add not working
  • CakePHP ACL tutorial initDB function warnings
  • How does this usort cmp function actually work?
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • How to attach a node.js readable stream to a Sendgrid email?
  • Play WS (2.2.1): post/put large request
  • D3 nodes and links from JSON with nested arrays of children
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Display issues when we change from one jquery mobile page to another in firefox
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Can Jackson SerializationFeature be overridden per field or class?
  • Deserializing XML into class C#
  • JSON with duplicate key names losing information when parsed
  • To display the title for the current loaction in map in iphone
  • Rearranging Cells in UITableView Bug & Saving Changes
  • php design question - will a Helper help here?
  • Is there a mandatory requirement to switch app.yaml?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • Benchmarking RAM performance - UWP and C#
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How do I use LINQ to get all the Items that have a particular SubItem?