Jump to content

XML parsing with PHP


Deyaan

Recommended Posts

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!  :confused:

 

It would help if you could just guide me how to catch those data from XML and I'd (try to) format it myself.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.