Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts

    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.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,394
    Thanks
    13
    Thanked 353 Times in 349 Posts
    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?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,394
    Thanks
    13
    Thanked 353 Times in 349 Posts
    a look in the manual will give the solution
    PHP Code:
    foreach ($reagent->attributes() as $key => $value
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •