Hybride Posted February 10, 2010 Share Posted February 10, 2010 I know this is very simple, but alas, I can't figure it out. I need to grab the CData from an XML file using SimpleXML, loop through every item in the XML file, pick out specific products and then put them in a different file. Using echo "<pre>",print_r($xml,true),"</pre>"; the structure goes as - id - title [items] - item - model - title - category or basically $xml->items->item then whatever node I need. It's the title that has the <![CDATA[whatever]]> and I can't figure out how to extract that information as well. It just shows up as SimpleXMLElement Object. I tried to do something along the lines of: $xml = simplexml_load_file('products.xml'); if($xml->items->item->category == ";whatever;") { print $xml->items->item; } Where assuming the category is what I need, then only the items are printed (but I realised this is more of a Perl thing and I became confused.) I tried looking at these pages: Reading XML with elements and attributes and Extract XML via PHP which helped, but this is the first am actually doing XML->PHP work and not sure how to actually do it. Any help would be sincerely appreciated. Link to comment https://forums.phpfreaks.com/topic/191647-extract-cdata-from-xml-php-simplexml/ Share on other sites More sharing options...
Hybride Posted February 10, 2010 Author Share Posted February 10, 2010 Actually, I managed to figure it out (somehow that keeps happening after I post on this forum)... To those interested, $xml = simplexml_load_file('products.xml');foreach ($xml->items->item as $cat) { preg_match('#whatever#i',$cat->category,$matches); if($matches[0]) { echo $cat->title.'<br/>'; echo $cat->model.'<br/>'; echo $cat->price.'<br/>'; }} It loops through the entire document, finding only whatever you're trying to match, printing the respected title/model/price, though that can easily be changed to fit your needs. Link to comment https://forums.phpfreaks.com/topic/191647-extract-cdata-from-xml-php-simplexml/#findComment-1010219 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.