kungmats Posted December 13, 2012 Share Posted December 13, 2012 Hey Guys! I relly need your help! I have this xml-file that I need data from.. I can get the data I want, so that's not the problem. My problem is to get all the data print out at the right place. The structure is this: <Events> <ListEvent> <BookingUrl></BookingUrl> <ImageUrl></ImageUrl> <Name></Name> // The hole event name <Occasions> <Occasion> <Arena> <Id></Id> <Name></Name> // THis dates event name <Url></Url> </Arena> <End></End> <Start></Start> </Occasion> </ListEvent> </Events> I want this printout for every Occasion - BookingUrl - ImageUrl - Name - Id - Name - Url - End - Start Quote Link to comment https://forums.phpfreaks.com/topic/271955-can-not-loop-xml-file-the-way-i-want/ Share on other sites More sharing options...
Christian F. Posted December 13, 2012 Share Posted December 13, 2012 We'll need to see your code in order to be able to help you. Quote Link to comment https://forums.phpfreaks.com/topic/271955-can-not-loop-xml-file-the-way-i-want/#findComment-1399168 Share on other sites More sharing options...
kungmats Posted December 13, 2012 Author Share Posted December 13, 2012 (edited) We'll need to see your code in order to be able to help you. Oups! ... Sorry! This is my "not working" code. The preg_match stuff works, but not my loops! preg_match_all('/<BookingUrl>(.*?)<\/BookingUrl>/', $contents, $bookingurl); preg_match_all('/<ImageUrl>(.*?)<\/ImageUrl>/', $contents, $imageurl); preg_match_all('/Url><Name>(.*?)<\/Name>/', $contents, $artistnamn); preg_match_all('/Id><Name>(.*?)<\/Name>/', $contents, $arenanamn); preg_match_all('/<Occasion>(.*?)<\/Occasion>/', $contents, $occasion); preg_match_all('/<Start>(.*?)<\/Start>/', $contents, $start); preg_match_all('/<Arena>(.*?)<\/Arena>/', $contents, $Arena); $i =0; foreach ($listevent[1] as $row ) { echo "<font color=blue>".$i."</font><br>"; $i2 =0; foreach ($occasion[1] as $row2 ) { echo $bookingurl[1][$i]."<br>"; echo $imageurl[1][$i]."<br>"; echo $artistnamn[1][$i]."<br>"; $i2++; echo "<p>"; } $i++; } And a bit xml <EventResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <Count>20</Count> <Events> <ListEvent> <BookingUrl> http:// </BookingUrl> <CbisProductId>302703</CbisProductId> <ImageUrl> http:// </ImageUrl> <Name>Sven Ingvars</Name> <NextOccasion> <Arena> <Id>16739</Id> <Name>Ladan i Båstad</Name> <Url> http:// </Url> </Arena> <End>2012-12-14T21:00:00</End> <Start>2012-12-14T21:00:00</Start> </NextOccasion> <Occasions> <Occasion> <Arena> <Id>16739</Id> <Name>Ladan i Båstad</Name> <Url> http:// </Url> </Arena> <End>2012-12-14T21:00:00</End> <Start>2012-12-14T21:00:00</Start> </Occasion> <Occasion> <Arena> <Id>16739</Id> <Name>Ladan i Båstad</Name> <Url> http:// </Url> </Arena> <End>2012-12-15T21:00:00</End> <Start>2012-12-15T21:00:00</Start> </Occasion> </Occasions> <Url> http:// </Url> </ListEvent> <ListEvent> <BookingUrl> http://b </BookingUrl> <CbisProductId>277426</CbisProductId> <ImageUrl> http:// </ImageUrl> <Name>Dirty Dancing</Name> <NextOccasion> <Arena> <Id>279700</Id> <Name>China Teatern</Name> <Url> http:// </Url> </Arena> <End>2012-12-13T19:30:00</End> <Start>2012-12-13T19:30:00</Start> </NextOccasion> <Occasions> <Occasion> <Arena> <Id>279700</Id> <Name>China Teatern</Name> <Url> http:// </Url> </Arena> <End>2012-12-13T19:30:00</End> <Start>2012-12-13T19:30:00</Start> </Occasion> <Occasion> <Arena> <Id>279700</Id> <Name>China Teatern</Name> <Url> http:// </Arena> <End>2012-12-14T19:30:00</End> <Start>2012-12-14T19:30:00</Start> </Occasion> <Occasion> <Arena> <Id>279700</Id> <Name>China Teatern</Name> <Url> http:// </Url> </Arena> <End>2012-12-15T19:30:00</End> <Start>2012-12-15T19:30:00</Start> </Occasion> <Occasion> <Arena> <Id>279700</Id> <Name>China Teatern</Name> <Url> http:// </Url> </Arena> <End>2012-12-15T15:00:00</End> <Start>2012-12-15T15:00:00</Start> </Occasion> </Occasions> <Url> http:// </Url> </ListEvent> </Events> </EventResult> Edited December 13, 2012 by kungmats Quote Link to comment https://forums.phpfreaks.com/topic/271955-can-not-loop-xml-file-the-way-i-want/#findComment-1399229 Share on other sites More sharing options...
requinix Posted December 13, 2012 Share Posted December 13, 2012 Don't use regular expressions. Try SimpleXML instead. Example: $xml = new SimpleXMLElement(...); foreach ($xml->Events->ListEvent as $event) { echo "Booking URL: ", (string)$event->BookingUrl, "\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/271955-can-not-loop-xml-file-the-way-i-want/#findComment-1399235 Share on other sites More sharing options...
kungmats Posted December 13, 2012 Author Share Posted December 13, 2012 (edited) Don't use regular expressions. Try SimpleXML instead. Example: $xml = new SimpleXMLElement(...); foreach ($xml->Events->ListEvent as $event) { echo "Booking URL: ", (string)$event->BookingUrl, "\n"; } I've tryed that for two days now but it won't work with our swedish letters åäö ÅÄÖ so I cant use it Edited December 13, 2012 by kungmats Quote Link to comment https://forums.phpfreaks.com/topic/271955-can-not-loop-xml-file-the-way-i-want/#findComment-1399238 Share on other sites More sharing options...
requinix Posted December 13, 2012 Share Posted December 13, 2012 If the XML doesn't have the $xmlstring = file_get_contents("wherever"); $xml = new SimpleXMLElement("<?xml version='1.0' encoding='whatever encoding'?>\n{$xmlstring}", 0, false); Quote Link to comment https://forums.phpfreaks.com/topic/271955-can-not-loop-xml-file-the-way-i-want/#findComment-1399255 Share on other sites More sharing options...
kungmats Posted December 13, 2012 Author Share Posted December 13, 2012 If the XML doesn't have the <?xml instruction (which includes the text encoding) then you can tack that onto the string. $xmlstring = file_get_contents("wherever"); $xml = new SimpleXMLElement("<?xml version='1.0' encoding='whatever encoding'?>\n{$xmlstring}", 0, false); OK, thank you! I'll try Quote Link to comment https://forums.phpfreaks.com/topic/271955-can-not-loop-xml-file-the-way-i-want/#findComment-1399276 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.