Jump to content


Photo

Parsing XML


  • Please log in to reply
6 replies to this topic

#1 dreado

dreado
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationUK

Posted 20 March 2006 - 08:28 PM

I’ve been trying to find out how to make a dynamic page displaying info from a xml feed. The explanations I have found have been somewhat confusing. I was wondering if someone here could help me out with this?
[a href="http://www.myconga.co.uk/ipod/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iPod Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/macmini/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]Mac mini Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/shuffle/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iPod Shuffle Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/imac/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iMac Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/alienware/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]Alien Ware Conga Line[!--colorc--][/span][!--/colorc--][/a]

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 20 March 2006 - 08:31 PM

Did you create the XML file?

Have you looked through the PHP manual on XML DOM?

Explain "Dynamic" in terms of how you mean it. Do you want a page that updates itself periodically if someone is watching it, or a page that reads from the file automatically?

Info: PHP Manual


#3 dreado

dreado
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationUK

Posted 21 March 2006 - 10:18 PM

Hi ober

No, I didn’t create the XML file, it’s one provided by another site.

Not sure if I have seen that manual or not, got a feeling I have but didn’t understand it.

What I’d like to achieve is to display the data from the XML feed in a page of my own, so I can style it to look how I want.

[a href="http://www.myconga.co.uk/ipod/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iPod Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/macmini/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]Mac mini Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/shuffle/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iPod Shuffle Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/imac/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iMac Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/alienware/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]Alien Ware Conga Line[!--colorc--][/span][!--/colorc--][/a]

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 March 2006 - 01:56 AM

Take a look at [a href=\"http://minixml.psychogenic.com\" target=\"_blank\"]http://minixml.psychogenic.com[/a] which is a fairly light weight class for parsing XML files into an array.

Ken

#5 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 22 March 2006 - 11:26 AM

Are you just trying to parse an RSS feed?

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#6 dreado

dreado
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationUK

Posted 22 March 2006 - 01:03 PM

[!--quoteo(post=357247:date=Mar 22 2006, 11:26 AM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Mar 22 2006, 11:26 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Are you just trying to parse an RSS feed?
[/quote]
Yeah

[a href="http://www.myconga.co.uk/ipod/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iPod Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/macmini/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]Mac mini Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/shuffle/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iPod Shuffle Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/imac/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iMac Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/alienware/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]Alien Ware Conga Line[!--colorc--][/span][!--/colorc--][/a]

#7 dreado

dreado
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationUK

Posted 08 April 2006 - 08:56 AM

[!--quoteo(post=357174:date=Mar 22 2006, 02:56 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 22 2006, 02:56 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Take a look at [a href=\"http://minixml.psychogenic.com\" target=\"_blank\"]http://minixml.psychogenic.com[/a] which is a fairly light weight class for parsing XML files into an array.

Ken
[/quote]

Thanks for the link, but in their code, I can't see where you'd put the url of the feed.

Maybe someone can look at the code I initially used and tell me what might be wrong with it (it generates nothing but a blank page).

<?php

function parseIntoArray( $url )
{
    $data = file_get_contents( $url );
    $xml_parser = xml_parser_create();
    xml_parse_into_struct($xml_parser, $data, $vals, $index);
    xml_parser_free($xml_parser);

    $params = array();
    $level = array();
    foreach ($vals as $xml_elem) {
      if ($xml_elem['type'] == 'open') {
       if (array_key_exists('attributes',$xml_elem)) {
         list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
       } else {
         $level[$xml_elem['level']] = $xml_elem['tag'];
       }
      }
      if ($xml_elem['type'] == 'complete') {
       $start_level = 1;
       $php_stmt = '$params';
       while($start_level < $xml_elem['level']) {
         $php_stmt .= '[$level['.$start_level.']]';
         $start_level++;
       }
       $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
       eval($php_stmt);
      }
    }
    return $params;
}

function getPlayerData( $url )
{
    $arrayData = parseIntoArray( $url );
    return $arrayData[ "PLAYERDATA" ];
}

$playerData = getPlayerData( "http://bf1942.gametrack.org/player.php?id=2297153&format=xml" );

echo "<pre>";
print_r( $playerData );
echo "</pre>";
?>

<?php echo $playerData["PLAYER"];?> <br />
<?php echo $playerData["GRADE"];?>

[a href="http://www.myconga.co.uk/ipod/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iPod Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/macmini/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]Mac mini Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/shuffle/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iPod Shuffle Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/imac/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]iMac Conga Line[!--colorc--][/span][!--/colorc--][/a] | [a href="http://www.myconga.co.uk/alienware/" target="_blank"][!--coloro:green--][span style="color:green"][!--/coloro--]Alien Ware Conga Line[!--colorc--][/span][!--/colorc--][/a]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users