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


Groups > comp.lang.python > #31363 > unrolled thread

Re: delete xml elements - using xpath search

Started byStefan Behnel <stefan_ml@behnel.de>
First post2012-10-16 07:50 +0200
Last post2012-10-16 07:50 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#31363 — Re: delete xml elements - using xpath search

FromStefan Behnel <stefan_ml@behnel.de>
Date2012-10-16 07:50 +0200
SubjectRe: delete xml elements - using xpath search
Message-ID<mailman.2254.1350366671.27098.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web