Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28070

Re: Cut out XML subtree

Date 2012-08-29 19:31 +0200
From Andreas Perstinger <andipersti@gmail.com>
Subject Re: Cut out XML subtree
References <5184413.thaGFiKO57@horus>
Newsgroups comp.lang.python
Message-ID <mailman.3941.1346261517.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, 29 Aug 2012 18:17:18 +0200 
Florian Lindner <mailinglists@xgm.de> wrote:
> I want to cut out an XML subtree like that:
[snip] 
> Is there a way I can do that using etree or DOM? The first is
> prefered...

Python 3.2.2 (default, Sep  5 2011, 22:09:30) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as etree
>>> test = """
... <root attribute="foobar">
...     <subA>
...         <section1>Element A.1</section1>
...         <section2>Element A.2</section2>
...     </subA>
...     <subB>
...         <section1>Element B.1</section1>
...         <section2>Element B.2</section2>
...     </subB>
... </root>"""
>>> tree = etree.fromstring(test)
>>> subA = tree.find("subA")
>>> tree.remove(subA)
>>> new = etree.tostring(tree, encoding="unicode")
>>> print(new)
<root attribute="foobar">
    <subB>
        <section1>Element B.1</section1>
        <section2>Element B.2</section2>
    </subB>
</root>

Bye, Andreas

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Cut out XML subtree Andreas Perstinger <andipersti@gmail.com> - 2012-08-29 19:31 +0200

csiph-web