Deyaan Posted August 17, 2009 Share Posted August 17, 2009 Hello everybody! I'm new here and also new with PHP, XML and parsing. I'm reading and learning from here, but I haven't had any success with what I want to do. I want to parse this XML with PHP to output something like this: ('game id->date and time', 'description->match', 'description->league', 'alternative odds->where 1', 'alternative odds->where X', 'alternative odds->where 2', 'double chance->alternative odds->where 1X', 'double chance->alternative odds->where X2', 'Over/under->alternative odds->where Under 2.5 goals', 'Over/under->alternative odds->where Over 2.5 goals') Is this even possible? I had no luck, but I'm total beginner here, so help me please! It would help if you could just guide me how to catch those data from XML and I'd (try to) format it myself. Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/ Share on other sites More sharing options...
DarkendSoul Posted August 17, 2009 Share Posted August 17, 2009 I think this is what you want. <?php $doc = new DOMDocument; $doc->Load("http://www.expekt.com/exportServlet?category=SOC%25"); $xpath = new DOMXPath($doc); $entries = $xpath->query("//game"); $game_info = array(); foreach ($entries as $entry) { $game_info[] = array( "id" => $entry->attributes->getNamedItem("id")->textContent, "date" => $entry->attributes->getNamedItem("date")->textContent, "time" => $entry->attributes->getNamedItem("time")->textContent // Keep going! ); } header("Content-Type: text/plain;"); print_r($game_info); ?> Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-899976 Share on other sites More sharing options...
Deyaan Posted August 17, 2009 Author Share Posted August 17, 2009 Thank you for replaying! Sorry, I don't think I understand... what should I input instead of "//game" ? Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-899983 Share on other sites More sharing options...
Deyaan Posted August 17, 2009 Author Share Posted August 17, 2009 Aha, that stays as it is... I thought it is a single-line comment at first. But I don't get anything, just: Array ( ) Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-900004 Share on other sites More sharing options...
RichardRotterdam Posted August 17, 2009 Share Posted August 17, 2009 Maybe you'd like the following tutorial published on the main site of phpfreaks. http://www.phpfreaks.com/tutorial/handling-xml-data Providing a script that does what you want might work on a short term but getting you to know how to work with it will be more valuable in the end. I also think that using simpleXML might be a bit easier to work with even though there is nothing wrong with DOMElement Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-900014 Share on other sites More sharing options...
Deyaan Posted August 17, 2009 Author Share Posted August 17, 2009 That's a nice simple tutorial, thank you for posting that. I follow it but with no success... I already tried with simpleXML <?php // load SimpleXML $games = new SimpleXMLElement('http://www.expekt.com/exportServlet?category=SOC%25', null, true); echo <<<EOF <table> <tr> <th>Date</th> </tr> EOF; foreach($games as $game) // loop through our games { echo <<<EOF <tr> <td>{$game['date']}</td> </tr> EOF; } echo '</table>'; ?> but it doesn't work like that. Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-900047 Share on other sites More sharing options...
RichardRotterdam Posted August 17, 2009 Share Posted August 17, 2009 Using the print_r function you will be able to see what's inside your $game variable. <?php // load SimpleXML $games = new SimpleXMLElement('http://www.expekt.com/exportServlet?category=SOC%25', null, true); echo "<pre>",print_r($games),"<pre>"; Try that code and it should be a piece of cake filtering the data you want edit Hang on your code should work. What php version are you running? Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-900052 Share on other sites More sharing options...
Deyaan Posted August 17, 2009 Author Share Posted August 17, 2009 yes, now that I've uploaded XML file to the server, now it works fine. But I have a lot pf problems with parsing and formatting it the way I want. I'd like to be like that: DateTime, League, Game, alternative[0] (where normal game), alternative[1] (where normal game), alternative[3] (where normal game), alternative[0] (where normal game: double chance), alternative[1] (where normal game: double chance), alternative[0] (where normal game: Over/under), alternative[1] (where normal game: Over/under) I can't isolate this... I don't know how to use something like WHERE in MySQL query or whatever should here be used... I don't know if this is even possible. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-900104 Share on other sites More sharing options...
Deyaan Posted August 17, 2009 Author Share Posted August 17, 2009 I made this: www.infotipovi.com/xml.php with this code: <?php // load SimpleXML $games = new SimpleXMLElement('expekt.xml', null, true); echo <<<EOF <table> <tr> <th>Time</th> <th>Game</th> <th>League</th> <th>Tip 1</th> <th>Tip X</th> <th>Tip 2</th> <th>Tip 1X</th> <th>Tip X2</th> <th>Tip U2.5</th> <th>Tip 02.5</th> </tr> EOF; foreach($games as $game) // loop through our games { echo <<<EOF <tr> <td>('{$game['date']}{$game['time']}00',</td> <td>'{$game->description}',</td> <td>'{$game->description->category}',</td> <td>{$game->alternatives->alternative[0]['odds']},</td> <td>{$game->alternatives->alternative[1]['odds']},</td> <td>{$game->alternatives->alternative[2]['odds']},</td> <td>{$game->alternatives->alternative[0]['odds']},</td> <td>{$game->alternatives->alternative[1]['odds']},</td> <td>{$game->alternatives->alternative[0]['odds']},</td> <td>{$game->alternatives->alternative[1]['odds']}),</td> </tr> EOF; } echo '</table>'; ?> but I want for example the game FK Jablonec - Banik Ostrava only to go once and with this information in line: ('20090817173000',' FK Jablonec - Banik Ostrava', 'Cze. Gambrinus Liga', 2.30, 2.95, 3.25, 1.30, 1.55, 1.65, 2.15,), Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-900217 Share on other sites More sharing options...
Deyaan Posted August 17, 2009 Author Share Posted August 17, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-900362 Share on other sites More sharing options...
Deyaan Posted August 18, 2009 Author Share Posted August 18, 2009 Maybe it's hard to understand, I'll try to make it simpler: I want to loop through my XML structure and only display the first category of a certain type. For instance, I want to display games with the description "FK Jablonec - Banik Ostrava" but not with any of the suffixes in later rows (like ": time of first goal", ": first half goals", etc.) I also want that 4th row of alternatives {$game->alternatives->alternative[0]['odds']} and 5th row of alternatives {$game->alternatives->alternative[1]['odds']} come from where description has a suffix ": double chance" and 6th row of alternatives {$game->alternatives->alternative[0]['odds']} and 7th row of alternatives {$game->alternatives->alternative[1]['odds']} come from where description has a suffix ": Over/under" Please help if tis is possible to accomplish, non of the tutorials online say anything about something like that. Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-900727 Share on other sites More sharing options...
Deyaan Posted August 18, 2009 Author Share Posted August 18, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-900921 Share on other sites More sharing options...
Deyaan Posted August 19, 2009 Author Share Posted August 19, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-901690 Share on other sites More sharing options...
Deyaan Posted August 19, 2009 Author Share Posted August 19, 2009 Anybody? I'm stuck here Quote Link to comment https://forums.phpfreaks.com/topic/170629-xml-parsing-with-php/#findComment-902039 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.