Jump to content

Can Not Loop Xml-File The Way I Want


kungmats

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/271955-can-not-loop-xml-file-the-way-i-want/
Share on other sites

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>

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 :(

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.