Wuoshi Posted January 18, 2010 Share Posted January 18, 2010 I have the following xml-file (not generated by me and can't choose myself). I'm using simplexml and I have no problems accessing third level elements such as <Category catalogs="false" id="169901" name="Reise" refinements="false"> using the follwing code $xml = simplexml_load_file($xml_url); $CounterObj = count($xml->Category); for($StartPunt=0; $StartPunt < $CounterObj; $StartPunt++) { echo $xml->Category[$StartPunt]['name']; } But I'm trying without success to access the level below too, such as: <Category catalogs="false" id="171701" name="Charter- og Pakketur" refinements="true"/> Here's the xml-file: <CategorySearch format="Tree" id="601"> <Category catalogs="false" id="601" name="Home Page" refinements="false"> <Category catalogs="false" id="169901" name="Reise" refinements="false"> <Category catalogs="false" id="171701" name="Charter- og Pakketur" refinements="true"/> <Category catalogs="false" id="172001" name="Ferje" refinements="false"/> <Category catalogs="false" id="100365023" name="Fly og hotell" refinements="false"/> <Category catalogs="false" id="172201" name="Flybilletter" refinements="false"/> <Category catalogs="false" id="170701" name="Hoteller" refinements="false"/> <Category catalogs="false" id="172301" name="Leiebil" refinements="false"/> <Category catalogs="false" id="100502023" name="Priskalender flybilletter" refinements="false"/> <Category catalogs="false" id="100425123" name="Reiseprodukter" refinements="false"/> </Category> <Category catalogs="false" id="110101" name="Data, Utstyr, Internett" refinements="false"> <Category catalogs="false" id="100324323" name="Grafikk og fremvisning" refinements="false"> <Category catalogs="true" id="127201" name="Projektorer" refinements="false"/> <Category catalogs="true" id="115401" name="Skannere" refinements="false"/> <Category catalogs="true" id="114401" name="Skjermer" refinements="false"/> <Category catalogs="true" id="114901" name="Skrivere" refinements="false"/> <Category catalogs="true" id="100052213" name="Alt-i-ett skrivere" refinements="false"/> <Category catalogs="true" id="116901" name="Nettverk- og Webkameraer" refinements="false"/> </Category> </Category> </CategorySearch> Do I have to loop through it in some way? Or is there another solution? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/188902-extract-xml-via-php/ Share on other sites More sharing options...
dgoosens Posted January 18, 2010 Share Posted January 18, 2010 this is really very strange XML... you should be able to access thoses nodes with $xml->Category->Category; Quote Link to comment https://forums.phpfreaks.com/topic/188902-extract-xml-via-php/#findComment-997363 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 $xml = simplexml_load_file($xml_url); foreach ($xml->Category as $cat) { echo $cat['name'].'<br/>'; foreach ($cat as $cat2) { echo ' - '.$cat2['name'].'<br/>'; foreach ($cat2 as $val) { echo ' - '.$val['name'].'<br/>'; } } } Never used simplexml before but i guess this works Quote Link to comment https://forums.phpfreaks.com/topic/188902-extract-xml-via-php/#findComment-997365 Share on other sites More sharing options...
Wuoshi Posted January 18, 2010 Author Share Posted January 18, 2010 Thanks guys. Buddski, your piece of code is outputing the level above the level I want to output. In other words I want one Category step deeper. Any idea? Quote Link to comment https://forums.phpfreaks.com/topic/188902-extract-xml-via-php/#findComment-997451 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 When I tested that script i was returned with (using your xml) [pre]Home Page - Reise - Charter- og Pakketur - Ferje - Fly og hotell - Flybilletter - Hoteller - Leiebil - Priskalender flybilletter - Reiseprodukter - Data, Utstyr, Internett - Grafikk og fremvisning - Projektorer - Skannere - Skjermer - Skrivere - Alt-i-ett skrivere - Nettverk- og Webkameraer[/pre]Which is as deep as that XML goes.. Quote Link to comment https://forums.phpfreaks.com/topic/188902-extract-xml-via-php/#findComment-997454 Share on other sites More sharing options...
PFMaBiSmAd Posted January 18, 2010 Share Posted January 18, 2010 I generally find that displaying the structure, as simplexml/php sees it, helps determine how to access it - echo "<pre>",print_r($xml,true),"</pre>"; You can put any level into the above to see what it looks like, such as - echo "<pre>",print_r($xml->Category->Category,true),"</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/188902-extract-xml-via-php/#findComment-997457 Share on other sites More sharing options...
Wuoshi Posted January 18, 2010 Author Share Posted January 18, 2010 Hmm...strange. I'll dig in and check for errors. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/188902-extract-xml-via-php/#findComment-997458 Share on other sites More sharing options...
Wuoshi Posted January 18, 2010 Author Share Posted January 18, 2010 Thanks PFMaBiSmAd. Will have a look at it. Quote Link to comment https://forums.phpfreaks.com/topic/188902-extract-xml-via-php/#findComment-997460 Share on other sites More sharing options...
Wuoshi Posted January 18, 2010 Author Share Posted January 18, 2010 Buddski! You're the man! Thank you. I had a small typing error which caused the problem. Quote Link to comment https://forums.phpfreaks.com/topic/188902-extract-xml-via-php/#findComment-997507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.