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


Groups > comp.lang.python > #31363

Re: delete xml elements - using xpath search

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'xml,': 0.05; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'snippet': 0.09; 'subject:using': 0.09; 'subject:xml': 0.09; 'file,': 0.15; 'element.': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'nodes': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:search': 0.16; 'string': 0.17; 'stefan': 0.17; 'elements': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'module.': 0.27; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; 'node': 0.29; 'parent': 0.29; 'code': 0.31; 'point': 0.31; 'file': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'text': 0.34; 'received:org': 0.36; 'but': 0.36; 'compare': 0.36; 'method': 0.36; 'xml': 0.37; 'subject:: ': 0.38; 'delete': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'remove': 0.61; 'different': 0.63; 'names..': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: delete xml elements - using xpath search
Date Tue, 16 Oct 2012 07:50:53 +0200
References <CAOMTrfecpoHumOXu7qOJ2ximm63vwq-7aY19QpGdTOT+wFos0w@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host fw-snc-frn5-de01.fw.telefonica.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1
In-Reply-To <CAOMTrfecpoHumOXu7qOJ2ximm63vwq-7aY19QpGdTOT+wFos0w@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.2254.1350366671.27098.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350366671 news.xs4all.nl 6985 [2001:888:2000:d::a6]:41509
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31363

Show key headers only | View raw


Tharanga Abeyseela, 16.10.2012 05:57:
> i need to delete the following pattern and its parent node from my xml
> file (IF THE SEARCH STRING FOUND)
> 
> <Rating>NC</Rating>
> 
> if i find the above particular string , i need to delete the XML
> elements (parent of the <Rating> element.  problem is i have different
> parent nodes with different names..
> 
> like
> 
> <movies>
>      <name> xxx</name>
>       <title>xxx </title>
>       <Rating>NC</Rating>
> </movies>
> 
> <tvshow>
>     <name> </name>
>      <title>xxx </title>
>      <Rating>NC</Rating>
> </tvshow>
> 
> <tvepisode>
>     <name>xxx </name>
>      <title>xxx </title>
>      <Rating>M</Rating>
> </tvepisode>
> 
> 
> as an example, i need to remove <movies> section and <tvshow> section
> from my xml, (because it has the word NC) but need to keep <tvepisode>
> 
> can someone point me to the correct direction, or code snippet

Use the xml.etree.ElementTree module. Just parse() in the file, iterate
over the nodes, use the findtext() method to search for the text of
"Rating" tags and compare it to "NC". Keep the nodes that don't match.

Stefan

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


Thread

Re: delete xml elements - using xpath search Stefan Behnel <stefan_ml@behnel.de> - 2012-10-16 07:50 +0200

csiph-web