help with xml parsing using php

Posted by fayer on Stack Overflow See other posts from Stack Overflow or by fayer
Published on 2010-03-17T05:49:13Z Indexed on 2010/03/17 5:51 UTC
Read the original article Hit count: 645

Filed under:
|

i've got following example xml:

<entity id="1">
    <name>computer</name>
    <type>category</type>
    <entities>
        <entity id="2">
            <name>mac</name>
            <type>category</type>
        </entity>
        <entity id="3">
            <name>linux</name>
            <type>category</type>
            <entities>
                <entity id="4">
                    <name>ubuntu</name>
                    <type>category</type>
                </entity>
                <entity id="5">
                    <name>redhat</name>
                    <type>category</type>
                    <entities>
                        <entity id="6">
                            <name>server</name>
                            <type>category</type>
                        </entity>
                        <entity id="7">
                            <name>desktop</name>
                            <type>category</type>
                        </entity>
                    </entities>
                </entity>
            </entities>
        </entity>
    </entities>
</entity>

if i've got an id, lets say 5. is it possible to retrieve the following:

  • the name of the entity with the id=5 (redhat)
  • ALL the child entities and their name and id (server/6 and desktop/7)
  • all the parent entities and their name and id (computer/1, mac/2 and linux/3)

im a noob on parsing xml. is this accomplished by xpath only or xquery/xpath?

i would appreciate if someone could give me some example code to do this with simplexml.

thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml