PDA

View Full Version : PHP XML Parsing?


Vercingitorix
06-24-2004, 04:02 PM
Is there a way to parse an .xml file if your host doesn't have the XML libraries installed?

I'm not sure if I'm using the right terminology actually. I was going to post the file I'm using but it appears to be to long. However it doesn't look like it's calling anything from a library but maybe it doesn't have to... I don't know.

Thanks for any help given. :)

scoutt
06-24-2004, 05:49 PM
they should be loaded by default. if your host has them disabled then I don't believe there is anything you can do.

you can get real ugly and use regular expressions and make your own library so to speak. but that is almost not worth the time.

Vercingitorix
06-24-2004, 05:57 PM
Maybe it's something I'm doing. Here's the file broken up.


<?php


class guildXML {

/*
* This class takes a Dark Age of Camelot Herald XML guild data file and parses it
*/

var $xml_parser;
var $xml_file;
var $html;
var $open_tag ;
var $close_tag ;
var $in_char;
var $in_alliance;
var $chars_array;
var $alliance_array;
var $current_tag ='';

/*
* Class Constructor
*/
function guildXML() {
$this->xml_parser = "";
$this->xml_file = "";
$this->html = "";
}

//Class Destructor (has to be invoked manually as PHP does not support destructors)
function destroy() {
xml_parser_free($this->xml_parser);
}

//Class Members
function concat($str) {
$this->html .= $str;
}

function startElement($parser, $name, $attrs) {
global $totalchars, $num_activechars,$num_inactivechars, $insideElement, $in_char, $current_tag, $i;

if (($in_char==1)||($in_alliance==1)){
$current_tag = $name;
}
if ($name=='GUILD'){
$current_tag= $name;
if (sizeof($attrs)) {
while (list($k, $v) = each($attrs)) {
if ($k=='GUILDRP'){$this->guildrp=$v;};
if ($k=='TIMESTAMP'){$this->timestamp=$v;};
if ($k=='REALM'){$this->realm=$v;};
}
}
}

if ($name=='CHARACTER'){
$in_alliance = 1;
}
if ($name=='CHARACTER'){
$in_char=1;
$totalchars++;
if (sizeof($attrs)) {
while (list($k, $v) = each($attrs)) {
if ($k=='NAME'){
$this->chars_array[$i]['name']=trim($v);
$this->chars_array[$i]['firstname']= trim (substr($this->chars_array[$i]['name'],0, strpos($this->chars_array[$i]['name'],' ')));
$this->chars_array[$i]['lastname']= trim (substr($this->chars_array[$i]['name'], strpos($this->chars_array[$i]['name'],' ')));
// Italian servers with Base64 encoding for last name, comment the above line and uncomment the following line
//$this->chars_array[$i]['lastname']= base64_decode(trim (substr($this->chars_array[$i]['name'], strpos($this->chars_array[$i]['name'],' '))));

if (!$this->chars_array[$i]['firstname'] && $this->chars_array[$i]['lastname']){
$this->chars_array[$i]['firstname'] = $this->chars_array[$i]['lastname'];
$this->chars_array[$i]['lastname']='';
}
}

if ($k=='LASTON') {
$this->chars_array[$i]['laston']=$v;
if ($v!='Inactive') {
$num_activechars++;
} elseif($v=='Inactive'){
$num_inactivechars++;
}
}
}
}
}

}

scoutt
06-24-2004, 05:58 PM
and what errors do you get?

Vercingitorix
06-24-2004, 05:59 PM
function endElement($parser, $name) {
global $close_tag, $current_tag, $in_char, $i;

if ($name=='CHARACTER'){$i++; $in_char=0;}
if ($name=='ALLIANCE'){$i++; $in_alliance=0;}
}

function characterData($parser, $data) {
global $guildrp, $inactiverp, $current_tag, $in_char, $i;
//$content_output .= '<br>'.$current_tag.' = '.$data.'<br>';
if (($in_char==1) && ($current_tag=='RACE')){
$this->chars_array[$i]['race'].=$data;
}
if (($in_char==1) && ($current_tag=='CLASS')){
$this->chars_array[$i]['class'].=$data;
}
if (($in_char==1) && ($current_tag=='LEVEL')){
$this->chars_array[$i]['level'].=$data;
}
if (($in_char==1) && ($current_tag=='TOTALRP')){
$this->chars_array[$i]['rp'].=$data;
if ($this->chars_array[$i]['laston']=='Inactive'){
$inactiverp += $data;
} else {
$guildrp += $data;
}

}
if (($in_char==1) && ($current_tag=='GUILDRANK')){
$this->chars_array[$i]['guildrank'].=$data;
}
//$this->html .= $data;
}

function parse() {

$this->xml_parser = xml_parser_create();
xml_set_object($this->xml_parser, &$this);
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, true);

xml_set_element_handler($this->xml_parser, "startElement", "endElement");
xml_set_character_data_handler($this->xml_parser, "characterData");
//xml_set_processing_instruction_handler($this->xml_parser, "PIHandler");

if (!($fp = fopen($this->xml_file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($this->xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser)));
}
}
}
}
// End of class

// Support functions

/*
* The function sortItems() is used for sorting, called upon by uksort()
* It uses querystring parameter $param as the sorting parameter
* querystring parameter $dir indicates ascending or descending order
*/

function sortItems($a, $b) {
global $gu, $dir, $param;

$val_a = ($gu[$a][$param]);
$val_b = ($gu[$b][$param]);

if (!$param){$param='level';}
if (!$dir){$dir='down';}
switch ($param){
case 'rp':
$val_a = $val_a - 0;
$val_b = $val_b - 0;
break;

case 'level':
$val_a = $val_a - 0;
$val_b = $val_b - 0;
break;
}

if ($dir=='up'){
if ( $val_a > $val_b ){return 1;}
if ( $val_a < $val_b ){return -1;}
if ( $val_a = $val_b ){return 0;}
} else {
if ( $val_a < $val_b ){return 1;}
if ( $val_a > $val_b ){return -1;}
if ( $val_a = $val_b ){return 0;}
}
}

/*
* returns array with Guild Rank information
*/

function guildrank($rank){
switch ($rank){
case 0: $rankname = array('Guildmaster', 'Description for Guildmaster');
break;

case 1: $rankname = array('Rank 1 Title', 'Description for Guild Rank 1');
break;

case 2: $rankname = array('Rank 2 Title', 'Description for Guild Rank 2');
break;

case 3: $rankname = array('Rank 3 Title', 'Description for Guild Rank 3');
break;

case 4: $rankname = array('Rank 4 Title', 'Description for Guild Rank 4');
break;

case 5: $rankname = array('Rank 5 Title', 'Description for Guild Rank 5');
break;

case 6: $rankname = array('Rank 6 Title', 'Description for Guild Rank 6');
break;

case 7: $rankname = array('Rank 7 Title', 'Description for Guild Rank 7');
break;

case 8: $rankname = array('Rank 8 Title', 'Description for Guild Rank 8');
break;

case 9: $rankname = array('Rank 9 Title', 'Description for Guild Rank 9');
break;
}
return $rankname;
}

Vercingitorix
06-24-2004, 06:01 PM
I don't get any errors, it just sits there loading and eventually stops.

Vercingitorix
06-24-2004, 06:05 PM
/*
* Creates the output table
*/

$content_output .= 'Total Realm Points: <b>'. $guildrp .'</b>';
$content_output .= '<br><span style="font-size:9pt;">Data last updated: <b>'.$guildtimestamp.'</b>&nbsp;::&nbsp;<a href="'.$xml_file.'" target="xml">View raw XML data</a></span><hr noshade="true">
<table cellspacing="0" cellpadding="0" style="border: solid 1px #3F5471;">
<tr>
<td>
<table width="100%" cellspacing="0" cellpadding="2">
<tr bgcolor="#E5E5E5">
<form action="'. $PHP_SELF .'" method="get">
<td>
<SELECT name="param">
<option value="name">Name</option>
<option value="level">Level</option>
<option value="class">Class</option>
<option value="race">Race</option>
<option value="rp">RP</option>
<option value="guildrank">Guild Rank</option>
</select>
<SELECT name="dir">
<option value="up">Ascending</option>
<option value="down">Descending</option>
</select>
<input type="submit" value="search"><input type="hidden" name="active" value="'.$_GET['active'].'">
</td>
</form>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table cellspacing="0" cellpadding="5">
<tr style="font-weight: bold;" bgcolor="#CCCCCC">
<td>#</td>
<td>Name</td>
<td>Level</td>
<td>Class</td>
<td>Race</td>
<td>RPs</td>
<td>Rank</td>
<td>level</td>
<td>Guild</td>
</tr>
';

$i=1;

foreach ($gu as $key=>$value){

if ($_GET['active']!='no'){
if ($value['laston']!='Inactive') {
if ($i%2==0){$color='#CCCCCC';}else{$color='#DDDDFF';}
$content_output .= '
<tr bgcolor="'.$color.'" style="font-size:9pt;">
<td>#'.$i.'</td>
<td><b>'.$value['firstname'].' '.$value['lastname'].'</b></td>
<td>'.$value['level'].'</td>
<td>'.$value['class'].'</td>
<td>'.$value['race'].'</td>
<td>';
if ($value['level']>=20){
$content_output .= $value['rp'];
} else {
$content_output .= '&nbsp;';
}
$content_output .= '
</td>
<td>';
if ($value['level']>=20){
$content_output .= realmtitle($value['rp']);
} else {
$content_output .= '&nbsp;';
}
$content_output .= '
</td>
<td>';
if ($value['level']>=20){
$content_output .= realmlevel($value['rp']);
} else {
$content_output .= '&nbsp;';
}
$content_output .= '
</td>';
$guildrank = guildrank($value['guildrank']);
$content_output .= '
<td>'.$guildrank[0].'</td>
</tr>';
$i++;
}
} else {
if ($value['laston']=='Inactive') {
if ($i%2==0){$color='#CCCCCC';}else{$color='#DDDDFF';}
$content_output .= '
<tr bgcolor="'.$color.'" style="font-size:9pt;">
<td>#'.$i.'</td>
<td><b>'.$value['firstname'].' '.$value['lastname'].'</b></td>
<td>'.$value['level'].'</td>
<td>'.$value['class'].'</td>
<td>'.$value['race'].'</td>
<td>';
if ($value['level']>=20){
$content_output .= $value['rp'];
} else {
$content_output .= '&nbsp;';
}
$content_output .= '
</td>
<td>';
if ($value['level']>=20){
$content_output .= realmtitle($value['rp']);
} else {
$content_output .= '&nbsp;';
}
$content_output .= '
</td>
<td>';
if ($value['level']>=20){
$content_output .= realmlevel($value['rp']);
} else {
$content_output .= '&nbsp;';
}
$content_output .= '
</td>';
$guildrank = guildrank($value['guildrank']);
$content_output .= '
<td>'.$guildrank[0].'</td>
</tr>';
$i++;
}

}

}
$content_output .= '
</table>
</td>
</tr>
</table>';

/*
For Euro guilds: uncomment the following instructions
*/

$content_output = utf8_decode($content_output); // handles UTF8 character encoding

/*
* Outputs table; end of script
*/

print $content_output;
?>

scoutt
06-24-2004, 06:05 PM
well, if it was disabled you would get undefined function error.

Vercingitorix
06-24-2004, 06:08 PM
Well that's odd then. If you want to take a look at what I'm talking about the file is at

http://fianna.org.uk/guildxml.php

I tried it in IE, Firefox and Netscape neither worked.

Is there a site you can point me to that will explain how to build your own libraries if that's what I need to do in the end?

Thanks

scoutt
06-24-2004, 08:14 PM
is that all the code? it is incomplete. you never start the class that I can see.

post the all files here in an attatchment, if more than one than zip it up then attatch it

Vercingitorix
06-25-2004, 04:48 AM
That's all of it I believe I cut out 10 or so lines in the beginning to save space but was just comments by the guy who made the file to begin with.

I'll attach the whole file unedited as a txt file.

scoutt
06-25-2004, 07:53 AM
yeah you left out the most vital part. :)

I don't see any problems off hand. let me play with it to see what I get.

Vercingitorix
06-25-2004, 02:47 PM
Well just dug around a bit and found out that the hosting server I'm using is PHP4 and not 4.2 like that guys site says you need. So perhaps that's why it isn't working?

Oh well, I'll keep searching for a parser that doesn't use the server or whatever will work :)

Thanks :)

Vercingitorix
06-25-2004, 02:50 PM
Would DOMIT! work?

http://www.engageinteractive.com/mambo/index.php?option=content&task=view&id=3606&Itemid=10137

I'm all confused with this XML stuff now :(

scoutt
06-25-2004, 03:03 PM
Originally posted by Vercingitorix
Well just dug around a bit and found out that the hosting server I'm using is PHP4 and not 4.2 like that guys site says you need. So perhaps that's why it isn't working?

Oh well, I'll keep searching for a parser that doesn't use the server or whatever will work :)

Thanks :)
what version of php are you using? php4 is the same as 4.x but you ned to see hwat version you have first.

Vercingitorix
06-25-2004, 03:35 PM
I believe it's version 4.1.1 it's definately not 4.2.2 or higher as someone posted in their support forums yesterday asking 'em to upgrade it to that or higher. But couldn't find an exact number.

I think that was the last stable release before 4.2.2 so that would most likely be it.

Vercingitorix
06-25-2004, 03:44 PM
Just found the info digging through 15 pages of forum happiness.

Version 4.1.2

*edit*

From support
"Your account has PHP 4.1.2 (fully patched and updated) which was complied with XML support."

So now I just need to dig around the php site and xml site and find out what's compatable then see if I can make heads or tails of it :)

Thanks for all the help

scoutt
06-25-2004, 08:48 PM
ok, I ran this on my test server that has php4.3 and it runs just fine. as long as you have the correct url in there for the file.

$guild->xml_file = "771.xml";

change that to point to the correct file and it should work. that is if you have xml enabled.

Vercingitorix
06-26-2004, 02:25 AM
So it'll parse the xml if the .xml file is on my server but not if it's on a remote server?

scoutt
06-26-2004, 07:46 AM
well maybe that is the problem. I did it local just because. try it and see.

Vercingitorix
06-26-2004, 03:03 PM
Yeah, it seems to work ok opening the file locally...

Odd I thought it's supposed to give an error if it can't use fopen() to open a remote file.