delete elements in xml?

Posted by fayer on Stack Overflow See other posts from Stack Overflow or by fayer
Published on 2010-03-23T12:10:13Z Indexed on 2010/03/23 12:13 UTC
Read the original article Hit count: 381

Filed under:
|

i've got some elements in a xml document i want to delete. so i want to create another xml document without those elements.

here is an example of how it looks like at the moment:

<entity id="1000070">
    <name>apple</name>
    <type>category</type>
    <entities>
        <entity id="7002870">
            <name>mac</name>
            <type>category</type>
            <entities>
                <entity id="7002907">
                    <name>leopard</name>
                    <type>sub-category</type>
                    <entities>
                        <entity id="7024080">
                            <name>safari</name>
                            <type>subject</type>
                        </entity>
                        <entity id="7024701">
                            <name>finder</name>
                            <type>subject</type>
                        </entity>
                    </entities>
                </entity>
            </entities>
        </entity>
        <entity id="7024080">
            <name>iphone</name>
            <type>category</type>
            <entities>
                <entity id="7024080">
                    <name>3g</name>
                    <type>sub-category</type>
                </entity>
                <entity id="7024701">
                    <name>3gs</name>
                    <type>sub-category</type>
                </entity>
            </entities>
        </entity>
        <entity id="7024080">
            <name>ipad</name>
            <type>category</type>
        </entity>
    </entities>
</entity>

i want to create another xml document without the sub-category and subject elements.

so the new one will look like this:

<entity id="1000070">
    <name>apple</name>
    <type>category</type>
    <entities>
        <entity id="7002870">
            <name>mac</name>
            <type>category</type>
        </entity>
        <entity id="7024080">
            <name>iphone</name>
            <type>category</type>
        </entity>
        <entity id="7024080">
            <name>ipad</name>
            <type>category</type>
        </entity>
    </entities>
</entity>

should i use simplexml/php or xslt to do this? are there other ways?

would be great with some code examples...thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml