...

View Full Version : Help with simplexml



Courtney
03-02-2010, 03:00 AM
I am totally stumped. I have managed to use simpleXML and XPATH to get this far.. now I am trying to neatly extract the keys and values from the following XML string. Trouble is I cannot figure out how to do it so that the number of [reagents] does NOT matter. Some strings will have 1 [reagent] some 15.

XML as I can access it:



<product id="12049" name="Corn Bread" icon="INV_Corn_09" minCount="1" maxCount="1">
<reagent id="4339" name="Corn Meal" quality="1" icon="INV_Baking_03" count="2" />
<reagent id="4291" name="Flour" quality="1" icon="INV_Baking_02" count="3" />
</product>





SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 12049
[name] => Corn Bread
[icon] => INV_Corn_09
[minCount] => 1
[maxCount] => 1
)

[reagent] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 4339
[name] => Corn Meal
[quality] => 1
[icon] => INV_Baking_03
[count] => 2
)

)

[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 4291
[name] => Flour
[quality] => 1
[icon] => INV_Baking_02
[count] => 3
)

)

)

)




Can someone point me in the right directon? This is mainly for learning and I am a novice.

Dormilich
03-02-2010, 07:52 AM
now I am trying to neatly extract the keys and values from the following XML string.

what is the key and what is the value?

Courtney
03-03-2010, 07:51 PM
Looking for a foreach loop I suppose to grab all of the keys and values. I cannot figure out how to get the item listed under:



[reagent] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array

Dormilich
03-03-2010, 08:05 PM
a look in the manual (http://de3.php.net/manual/en/simplexmlelement.attributes.php) will give the solution

foreach ($reagent->attributes() as $key => $value)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum