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: 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 Subject: Re: delete xml elements - using xpath search Date: Tue, 16 Oct 2012 07:50:53 +0200 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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) > > NC > > if i find the above particular string , i need to delete the XML > elements (parent of the element. problem is i have different > parent nodes with different names.. > > like > > > xxx > xxx > NC > > > > > xxx > NC > > > > xxx > xxx > M > > > > as an example, i need to remove section and section > from my xml, (because it has the word NC) but need to keep > > 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