Jump to content

XML if switch exists?


Serenitee

Recommended Posts

I'm attempting to write my first XML parser, and the XML I will be parsing (pardon any incorrect terminology here) at times has 3rd level tags within the main tags, and at other times does not.

 

If I comment out these third level tags, the parser works GREAT - but I want that information when it exists.  Im using a switch with cases for each tag - but as soon as it comes to one of those that does have the tag, it ignores any previous ones without, provides only those that DO have that tag - and the last one (who doesn't).

 

I certainly hope I'm clear enough, I'm in new waters with case/switch (and break for that matter).

 

I'm hoping someone can point the way to something like "if($key exists) {case $key: toss into my array}

 

Most attempts I've worked with have errored completely, with some just providing very unwanted results.

 

Any point in a solid direction would be appreciated!

Link to comment
https://forums.phpfreaks.com/topic/171557-xml-if-switch-exists/
Share on other sites

Ok, after much research I still haven't found how to fix this, but I may understand better how to word it:

 

In the XML, the nodes are nested (as is usual), I'm able to access all nodes and their information, but the problem comes in at the 4 level, this node does not always exist.  When parsing it will return only the 3 with this node + the last.  If I remove parsing for this node, I receive all 77 items.  I have attempted things such as:

 

if(isset($alchemy_key)
{case $alchemy_key:
	$toon_array[$counter]->alchemy = $data;
	break;}

 

In hopes that I could check if that node exists, and if so, then go further - but this errors as does any other variations I attempt.  From everything I see, if it doesn't exist it should just be ignored (and move along - but it seems it instead ignores any without it, except for the last which it tacks on).

 

Hopefully that's a bit more clear in the actual issue.

 

Link to comment
https://forums.phpfreaks.com/topic/171557-xml-if-switch-exists/#findComment-904828
Share on other sites

Are you suggesting an additional switch inside a switch?

 

Some of the keys (1 of the 'fully functional', and the ones that cause issues):

$lastweekrp_key = "*GUILD*CHARACTERS*CHARACTER*LASTWEEKRP";

$alchemy_key = "*GUILD*CHARACTERS*CHARACTER*TRADESKILLS*ALCHEMY";
$armorcraft_key = "*GUILD*CHARACTERS*CHARACTER*TRADESKILLS*ARMORCRAFT";
$fletching_key= "*GUILD*CHARACTERS*CHARACTER*TRADESKILLS*FLETCHING";
$spellcraft_key = "*GUILD*CHARACTERS*CHARACTER*TRADESKILLS*SPELLCRAFT";
$tailoring_key = "*GUILD*CHARACTERS*CHARACTER*TRADESKILLS*TAILORING";
$weaponcraft_key = "*GUILD*CHARACTERS*CHARACTER*TRADESKILLS*WEAPONCRAFT";

 

actual code:

 

function endTag($parser, $data)
{global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);}


function contents($parser, $data)
{global $current_tag, $ToonName, $LastOn, $id_key, $server_key, $race_key, $class_key, $level_key, $mlevel_key, $mpath_key, $totalrp_key, $lastweekrp_key, $alchemy_key, $armorcraft_key, $fletching_key, $spellcraft_key, $tailoring_key, $weaponcraft_key, $counter, $toon_array;

switch($current_tag)
	{case $id_key:
	$toon_array[$counter] = new xml_toon();
	$toon_array[$counter]->characterName = $ToonName;
	$toon_array[$counter]->LastOn = $LastOn;
	$toon_array[$counter]->id = $data;
	break;

	case $server_key:
	$toon_array[$counter]->server = $data;
	break;

	case $race_key:
	$toon_array[$counter]->race = $data;
	break;

	case $class_key:
	$toon_array[$counter]->class = $data;
	break;

	case $level_key:
	$toon_array[$counter]->level = $data;
	break;

	case $mlevel_key:
	$toon_array[$counter]->mlevel = $data;
	break;

	case $mpath_key:
	$toon_array[$counter]->mpath = $data;
	break;

	case $totalrp_key:
	$toon_array[$counter]->totalrp = $data;
	break;

	case $lastweekrp_key:
	$toon_array[$counter]->lastweekrp = $data;
	break;

	case $alchemy_key:
	$toon_array[$counter]->alchemy = $data;
	break;

	case $armorcraft_key:
	$toon_array[$counter]->armorcraft = $data;
	break;

	case $fletching_key:
	$toon_array[$counter]->fletching = $data;
	break;

	case $spellcraft_key:
	$toon_array[$counter]->spellcraft = $data;
	break;

	case $tailoring_key:
	$toon_array[$counter]->tailoring = $data;
	break;

	case $weaponcraft_key:
	$toon_array[$counter]->weaponcraft = $data;
	$counter++;
	break;
	}

}

 

The tradeskills node only exists at times - once I attempt to retrieve that information, it ignores all but the 3 that have the last tradeskill in the list (I've commented 'em all out one by one and it's the case no matter which it is) and gives the last person in the list as well (who happens to have no skills).

 

I suppose I should've just added the code prior - I always try to be clear/brief.. and sometimes I'm unclear and far to brief.

tia!

Link to comment
https://forums.phpfreaks.com/topic/171557-xml-if-switch-exists/#findComment-905116
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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