Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-02-2010, 02:00 AM   PM User | #1
Courtney
New Coder

 
Join Date: Feb 2010
Posts: 20
Thanks: 2
Thanked 2 Times in 2 Posts
Courtney is an unknown quantity at this point
Help with simplexml

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:

Code:
<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>

Code:
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.

Last edited by Courtney; 03-02-2010 at 02:23 AM..
Courtney is offline   Reply With Quote
Old 03-02-2010, 06:52 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by Courtney View Post
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?
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 03-03-2010, 06:51 PM   PM User | #3
Courtney
New Coder

 
Join Date: Feb 2010
Posts: 20
Thanks: 2
Thanked 2 Times in 2 Posts
Courtney is an unknown quantity at this point
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:

Code:
[reagent] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
Courtney is offline   Reply With Quote
Old 03-03-2010, 07:05 PM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
a look in the manual will give the solution
PHP Code:
foreach ($reagent->attributes() as $key => $value
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:25 AM.


Advertisement
Log in to turn off these ads.