Topic: PHP and XML (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=29225" title="Pages that link to Topic: PHP and XML (Page 1 of 1)" rel="nofollow" >Topic: PHP and XML <span class="small">(Page 1 of 1)</span>\

 
Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 05-20-2007 08:58

I don't really use XML that much but I have been screwing with PHP for a long time, PHP 5 is suppose to have some cool XML fuctions which I am trying to grasp but really falling short in the understanding.

I figure I'd start with the basics find an XML file and try to parse it with PHP for information I actually wanted.

To begin, lets look at an XML document for a game I play, it basicly lists realm status, load and type.
http://www.worldofwarcraft.com/realmstatus/status.xml

Ok so processing information, lets say I wanted to list on a PHP page all servers that are PVP based, ignoring all other types, how might this be accomplished? I know it should be parsable into an array but i'm just not getting it.

Also how about single data parsing, lets say I only want to know the status for Dragonblight server, how does filtering work?

I think I learn best by example and trying to break down and understand what something is doing, rather than figuring out the entire thing by starting from scratch and documentation.

In general i'd also like to know if you are aware of any good tutorials on this type of stuff? I'd like to start incorporating XML into PHP in the future

Thanks for any help you can offer.

Synthetic's Chess Player Page

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 05-20-2007 11:33

I see 2 easy ways:

XPATH expressions return a collection with the nodes matching the expression. XLSLT can be used to transform the XML and filter it using some paramaters.


Sorry I haven't done such things in PHP for years, let alone with PHP5. Only in JavaScript.

Hope that helps anyway,

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 05-28-2007 20:40

see if this might help :

code:
$request_url = "http://www.worldofwarcraft.com/realmstatus/status.xml";
$xml_data = simplexml_load_file($request_url) or die("feed not loading");


echo "<pre>";
var_dump($xml_data);
echo "</pre>";



This will just dump the xml file into the array and show the SimpleXMLElement object stuff.

Or you can play around with each of the attributes like


code:
$request_url = "http://www.worldofwarcraft.com/realmstatus/status.xml";
$xml_data = new SimpleXMLElement($request_url,null,true);
 

foreach($xml_data_ as $rs) {

    echo "Name: {$rs['n']}" . "<br />";
    echo "Type : {$rs['t']}" ."<br />";
    echo "Status : {$rs['s']}" ."<br />";
    echo "Load" : {$rs['l']}" . "<br />";

}



I am guessing that from the xsl they have listed in that file, it does some stuff with the "t" attribute to tell it that 1 = POV, etc...
actually, looking at the xsl document, they have this :

code:
<xsl:call-template name="rankingRowAlternate"/>
&#8722;
	<xsl:choose>
&#8722;
	<xsl:when test="@t = 1">
<small style="color: #234303;">Normal</small>
</xsl:when>
&#8722;
	<xsl:when test="@t = 2">
<small style="color: #660D02;">(PVP)</small>
</xsl:when>
&#8722;
	<xsl:when test="@t = 3">
<small style="color: #535600;">(RP)</small>
</xsl:when>
&#8722;
	<xsl:when test="@t = 0">
<small style="color: #535600;">(RPPVP)</small>
</xsl:when>
</xsl:choose>



So, based on that, you could simply just do a switch statement or something that if the "t" attribute = "2" then echo "(PVP)" or something like that.

Hope that helps.

Later,

C:\

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 05-30-2007 03:49

i was actually playing around with this a few minutes ago.

see if this might help out a bit more

code:
<?
$request_url = "http://www.worldofwarcraft.com/realmstatus/status.xml";
// load as file
$xml_data = new SimpleXMLElement($request_url,null,true);

?>

<table width="100" cellpadding="5" cellspacing="5" border="1">
	<tr>
		<td>Name</td>
		<td>Type</td>
	</tr>
<?


foreach($xml_data as $rs) {
	echo "<tr>";

	echo "<td>" . "{$rs['n']}" . "</td>";

	
	$typeServer = $rs['t'];

	if($typeServer == "1"){
		echo "<td>" . "Normal" ."</td>";
	}elseif ($typeServer == "2"){
		echo "<td>" .  "(PVD)" . "</td>";
	}else{
		echo "<td>" . "RP" . "</td>";
	}

	
    echo "</tr>";
}


?>


</table>



Later,

C:\

<edit>took out commented lines </edit>

(Edited by CPrompt on 05-30-2007 03:52)

Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 06-19-2007 18:46

i guess my post to this never actually went through lol, damn school connection lag >.<

Anyway thanks for the info posted, got me going in the right direction few weeks ago

Synthetic's Chess Player Page



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu