Jump to content

Xpath Help


mcfmullen

Recommended Posts

My parser:

<?php        
if (isset($_GET['formSubmit'])) {
$option = $_GET['option'];
$option = array_values($option);
if (!empty($option)){
	$xml = simplexml_load_file('differences.xml');
	$i = 0;
	$count = count($option); ?>
<table>
<tr>
	<td>Item</td>
        <td>Code</td>
        <td>Type</td>
        <td>Level</td>
    </tr>
<?php
	while($i < $count)
		{
			$selected = $xml->xpath("//item[contains(@name,'".$option[$i]."')]");
			echo "<tr>";
			echo "<td>".$selected[0]['name']."</td>";
			echo "<td>".$selected[0]['code']."</td>";
			echo "<td>".$selected[0]['type']."</td>";
			echo "<td>".$selected[0]['requiredLevel']."</td>";
			echo "</tr>";
			$i++;
		} ?>
</table>

 

This works great however requiredLevel is showing nothing. Here is what is stored in the $selected array:

 

Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [buildingName] => EiffelTower [code] => lM [type] => building ) [requiredLevel] => 5 [cost] => 5000 [built] => 03/31/1889  => SimpleXMLElement Object ( [@attributes] => Array ( [itemClass] => EiffelTower_construct ) ) [defaultItem] => SimpleXMLElement Object ( [@attributes] => Array ( [amount] => 1 [name] => iron ) ) [finishedReward] => iron [image] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [loadClass] => mc [name] => construct_0 [url] => /buildings/eiffel_tower.swf ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [loadClass] => mc [name] => construct_1 [url] => /buildings/eiffel_tower.swf ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [loadClass] => mc [name] => built_0 [url] => /buildings/eiffel_tower.swf ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => icon [url] => buildings/eiffel_tower.png ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => startIcon [url] => /buildings/eiffel_tower.png ) ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => halfIcon [url] => /buildings/eiffel_tower.png ) ) [6] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => completeIcon [url] => /buildings/eiffel_tower.png ) ) ) [constructionUI] => /buildings/Construct_eiffel_tower.swf [countryCard] => france )

 

My XML:

 

		<element>
			<item code="lM" name="snowman2010" type="building">
				<requiredLevel>5</requiredLevel>
				<cost>5000</cost>
				<built>03/31/1889</built>
				<storageType itemClass="EiffelTower_construct"/>
				<defaultItem amount="1" name="iron"/>
				<finishedReward>iron</finishedReward>
				<image loadClass="mc" name="construct_0" url="/buildings/eiffel_tower.swf"/>
				<image loadClass="mc" name="construct_1" url="/buildings/eiffel_tower.swf"/>
				<image loadClass="mc" name="built_0" url="/buildings/eiffel_tower.swf"/>
				<image name="icon" url="/buildings/eiffel_tower.png"/>
				<image name="startIcon" url="/buildings/eiffel_tower.png"/>
				<image name="halfIcon" url="/buildings/eiffel_tower.png"/>
				<image name="completeIcon" url="/buildings/eiffel_tower.png"/>
				<constructionUI>/buildings/eiffel_tower.swf</constructionUI>
				<countryCard>france</countryCard>
			</item>

 

Using the established code pattern in my parser.php how can I print out requiredLevel?

 

The output of my array is mighty confusing! [/code]

Link to comment
Share on other sites

Can anyone help me decode my array?

 

It completely baffles me what with the brackets and SimpleXMLElement Objects.

 

I can't find any resources written in plain english to help explain it nor can I find any resources to extract the information.

 

For example, how can I echo [requiredLevel] or [finishedReward] or icon

 

Any help would be GREATLY appreciated!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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