Re: delete xml elements - using xpath search

Discussion in 'Python' started by Stefan Behnel, Oct 16, 2012.

  1. 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
    Stefan Behnel, Oct 16, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,964
    jan V
    Jul 29, 2005
  2. ETL
    Replies:
    9
    Views:
    805
  3. Ultrus
    Replies:
    4
    Views:
    2,576
    Stefan Behnel
    Aug 30, 2007
  4. Claudio Calboni
    Replies:
    4
    Views:
    249
  5. Tharanga Abeyseela

    delete xml elements - using xpath search

    Tharanga Abeyseela, Oct 16, 2012, in forum: Python
    Replies:
    0
    Views:
    154
    Tharanga Abeyseela
    Oct 16, 2012
Loading...

Share This Page