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


Groups > comp.lang.python > #28070

Re: Cut out XML subtree

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <andipersti@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.025
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; '"""': 0.05; 'python': 0.09; 'sep': 0.09; 'aug': 0.13; 'andreas': 0.16; 'bye,': 0.16; 'subject:XML': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'element': 0.17; '>>>': 0.18; '+0200': 0.20; 'import': 0.21; 'received:mail- bk0-f46.google.com': 0.22; 'header:In-Reply-To:1': 0.25; 'received:209.85.214.46': 0.27; 'tree': 0.27; 'to:addr:python- list': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'there': 0.35; 'message-id:@gmail.com': 0.36; 'test': 0.36; 'charset:us-ascii': 0.36; 'xml': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'first': 0.61; 'header:Message-Id:1': 0.62; 'more': 0.63; 'cut': 0.71; 'florian': 0.84; 'subject:Cut': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:in-reply-to:references:x-mailer :mime-version:content-type:content-transfer-encoding; bh=GIs6KmGDwCCfZw6d5OPNZa40DM6DBbgEH8c+B36wW7A=; b=JLtFB5SrYni0hkBkTOEYAI8/tVwqnnKUhMdo8pdzQ8JsKxr0Oi8T3B1WZdYfsLvUxR UsvDuk0g8WmvdYXqgUVY3AoOKX4ElPnUdkWqzjXvd0aL1Ojseqj3I1DtX7P244tuDbSq Uz9rdFMKqPjeaws96zhazvETcURYHUtIcUtkJwK8aYWYb4f8ijnuFnPQoVAakA7QNv0i UK1Io2QdmFjlLXN4XCpx7UtBDRciaq9N+Vq3mSGHFnk5ahFtMs8cfvgFhc1O2CgA+tkn KB7v30i4N/cqIeAL0iVr1OgDlMHW9l8Cv7UdruS370F/OKRyhSekEU/qMSSY3HK15Oyf x3Hw==
Date Wed, 29 Aug 2012 19:31:52 +0200
From Andreas Perstinger <andipersti@gmail.com>
To python-list@python.org
Subject Re: Cut out XML subtree
In-Reply-To <5184413.thaGFiKO57@horus>
References <5184413.thaGFiKO57@horus>
X-Mailer Sylpheed 3.2.0beta3 (GTK+ 2.24.6; i686-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3941.1346261517.4697.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346261517 news.xs4all.nl 6878 [2001:888:2000:d::a6]:52963
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28070

Show key headers only | 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