...

View Full Version : DOM XML Parser problems.



Mentally
03-14-2009, 02:07 PM
Hi there guys! Maybe some of the bright minds out there know a solution to my current..nuisance. :)

I'm trying to import different profiles from the World of Warcraft Armory (some profiler thing for a game I play.) that uses XML as their informaton scheme. The problem I'm having lies in duplicate entries? I'm not sure if that's the right way to say it, anyhow, I'm using the DOMDocument parser that comes native with PHP to extract my information but, whenever there are multiple tags like these 2 examples:


<character battleGroup="Rampage" charUrl="r=Neptulon&amp;n=Nieeru" class="Mage" classId="8" faction="Alliance" factionId="0" gender="Female" genderId="1" guildName="CoX" guildUrl="r=Neptulon&amp;n=CoX&amp;p=1" lastModified="March 11, 2009" level="80" name="Nieeru" points="3160" prefix="" race="Draenei" raceId="11" realm="Neptulon" suffix="">
<arenaTeams>
<arenaTeam battleGroup="Rampage" created="1229472000000" faction="Alliance" factionId="0" gamesPlayed="0" gamesWon="0" lastSeasonRanking="0" name="The Popcorns" ranking="0" rating="1500" realm="Neptulon" realmUrl="b=Rampage&amp;r=Neptulon&amp;ts=2&amp;t=The+Popcorns&amp;ff=realm&amp;fv=Neptulon&amp;select=The+Popcorns" season="0" seasonGamesPlayed="0" seasonGamesWon="0" size="2" teamSize="2" teamUrl="r=Neptulon&amp;ts=2&amp;t=The+Popcorns&amp;select=The+Popcorns" url="r=Neptulon&amp;ts=2&amp;t=The+Popcorns&amp;select=The+Popcorns">
<emblem background="ff4366c2" borderColor="ffdaace0" borderStyle="6" iconColor="ff70f2ad" iconStyle="40"/>
<members>
<character battleGroup="" charUrl="r=Neptulon&amp;n=Nieeru" class="Mage" classId="8" contribution="1500" gamesPlayed="0" gamesWon="0" gender="Female" genderId="1" guild="CoX" guildId="3217918" guildUrl="r=Neptulon&amp;n=CoX&amp;p=1" name="Nieeru" race="Draenei" raceId="11" seasonGamesPlayed="0" seasonGamesWon="0" teamRank="0"/>
<character battleGroup="" charUrl="r=Neptulon&amp;n=Nilor" class="Paladin" classId="2" contribution="1500" gamesPlayed="0" gamesWon="0" gender="Female" genderId="1" guild="CoX" guildId="3217918" guildUrl="r=Neptulon&amp;n=CoX&amp;p=1" name="Nilor" race="Draenei" raceId="11" seasonGamesPlayed="0" seasonGamesWon="0" teamRank="1"/>
</members>
</arenaTeam>
</arenaTeams>
</character>


and this


<skill key="enchanting" max="450" name="Enchanting" value="441"/>
<skill key="tailoring" max="450" name="Tailoring" value="435"/>


It makes my life a living hell, trying to fetch the information. I'm not sure if looping is the way to go, if so, I'm doing something extremely wrong. I've tried using IF statements to filter out information, but, it just keeps, in the first example, putting out the name "Nieeru" twice, for example.

In the latter code statement, I've tried to set 4 variables to null by start. name1, value1, name2 and value2. Using IF statements to set them and such, but, all of them seems to automatically just over-write themselves to the last if statement, no matter what I try.


$pfs = $xml->getElementsByTagName("skill");
foreach($pfs as $profession) {
$name1 = null;
$value1 = null;
# -- Get the first profession name & value
if($profession->getAttribute("name") == "Alchemy" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Blacksmithing" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Enchanting" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Engineering" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Herbalism" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Mining" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Inscription" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Jewelcrafting" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Leatherworking" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Skinning" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Tailoring" && $profession->getAttribute("name") != $name1) {
$name1 = $profession->getAttribute("name");
$value1 = $profession->getAttribute("value");
}
$name2 = null;
$value2 = null;
# -- Get the second profession name & value
if($profession->getAttribute("name") == "Alchemy" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Blacksmithing" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Enchanting" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Engineering" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Herbalism" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Mining" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Inscription" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Jewelcrafting" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Leatherworking" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Skinning" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
} elseif($profession->getAttribute("name") == "Tailoring" && $profession->getAttribute("name") != $name2) {
$name2 = $profession->getAttribute("name");
$value2 = $profession->getAttribute("value");
}
}


I'm not really sure what to do, whether or not to go the way I've already done. (Using DOMDocument, or using the event-based API.)

It's really starting to get on my nerves now, after strugling with this problem for the last week. So I'm at a loss.

Does anyone have any ideas or something, that can help me on my way?
Any help would be greatly appreciated!

Cheers,
- Mentally.

mlseim
03-14-2009, 06:34 PM
What is supposed to happen with the data you read?
Are you displaying it on the screen, saving it somewhere?

I think how you parse, and what the end result is supposed
to be will change the answer on how to do it.

I don't know anything about war-games, so I'm not sure if
the top XML example is data for one "battlegroup", and there
are many of those?

So you display the data by "battlegroup"?

Mentally
03-14-2009, 08:04 PM
Ah, hi there. At the moment, what I'm trying to do is break down pieces of information from the downloaded XML sheet and pull it into a database.

The battlegroup thing. The servers in World of Warcraft are split up in different Battlegroups. A cluster of servers in one group where people from all those servers can bash their heads together in PvP-Battleground instances and Arena matches, etc. That's basically what it's for. The data I was wanting to parse is in the <character> tab, but as it turns out, if you look at the source within the <ArenaTeams> tabs, there's also <character> tabs so when trying to use:


$xml->getElementsByTagName("character");
$xml->getAttribute("name");

It automatically fetches anything that maches it, even if it's within the <ArenaTeam> bracket.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum