Proper syntax for this?


<icons>
    <icon_set name="Icons1">
	<icon_url>http://website.com/icon1.gif</icon_url>
    </icon_set>
    <icon_set name="Icons2">
	<icon_url>http://website.com/icon2.gif</icon_url>
    </icon_set>
    <icon_set name="Icons3">
	<icon_url>http://website.com/icon3.gif</icon_url>
    </icon_set>
</icons>

I’m fairly new to SimpleXML and learning as I go. Most information I can find online, but having a bit of trouble with this one.

In order to pull a specific icon set url from the above, what would the correct syntax be???

I’m assigning this to a variable in my php.

I tried something like $var1 = $xml->icons->icon_set[‘Icons3’]->icon_url … in order to pull the url from icon3. Which of course didn’t work.

I know this is probably simple, but I think I have confused myself with all of the reading online. :lol:

Thanks for any help.

Why don’t you do something like print_r($xml); to view the structure of the parsed xml. It should be easy to figure out after that.

With $xml being the simpleXML object - “icons”, var_dump and print_r give

object(SimpleXMLElement)#1 (1) {
  ["icon_set"]=>
  array(3) {
    [0]=>
    object(SimpleXMLElement)#2 (2) {
      ["@attributes"]=>
      array(1) {
        ["name"]=>
        string(6) "Icons1"
      }
      ["icon_url"]=>
      string(28) "http://website.com/icon1.gif"
    }
    [1]=>
    object(SimpleXMLElement)#3 (2) {
      ["@attributes"]=>
      array(1) {
        ["name"]=>
        string(6) "Icons2"
      }
      ["icon_url"]=>
      string(28) "http://website.com/icon2.gif"
    }
    [2]=>
    object(SimpleXMLElement)#4 (2) {
      ["@attributes"]=>
      array(1) {
        ["name"]=>
        string(6) "Icons3"
      }
      ["icon_url"]=>
      string(28) "http://website.com/icon3.gif"
    }
  }
}
SimpleXMLElement Object
(
    [icon_set] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => Icons1
                        )

                    [icon_url] => http://website.com/icon1.gif
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => Icons2
                        )

                    [icon_url] => http://website.com/icon2.gif
                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => Icons3
                        )

                    [icon_url] => http://website.com/icon3.gif
                )

        )

)

Whatever you see in “[some_node]” is what you use to “drill down” to what you’re after. eg.
$xml->icon_set[2]->icon_url
depends on you knowing the numeric “key”, ie. “2” in this case.

Maybe you need to use XPATH?

print_r is definately helpful … thanks for that tip.

i knew i could use node numbers, such as specifying

$var1 = $xml->icons->icon_set[3]->icon_url

but was just curious if it was possible, and if so how, to get the url assigned to $var1 based on the “name=” within icon_set

just in case the list order was ever changed, my script would continue to pull the correct url based on the set name and not whatever happens to be sitting in the icon_set[3] node. :wink:

i dont suspect the list would ever change order, was just thinking ahead

haven’t looked into xpath yet … but will give it a read.

I guess you could get the attribute values and use test conditionals.

If you start to get messy/long files, you could include “it”; as a file or a Class.

But if you know straight off where what you want is, then using XPATH gives good results digging down to what’s wanted.

More complex a language in ways, but once you grasp the basics the rest comes quick (well, it did for me, relatively speaking) it makes for cleaner code.