delete from pattern to pattern if it contains match

Joaquin Alzola Joaquin.Alzola at lebara.com
Mon Apr 25 06:19:56 EDT 2016


I put some code I did before for the xmlns:

                              xml_root = ET.ElementTree(ET.fromstring(xml_decoded)).getroot()
                              for elem in xml_root.getiterator():
                                             if('{http://request.messagepush.interfaces.comviva.com/xsd}shortCode'==elem.tag):
                                                            shortCode = (elem.text).rstrip()
                                             if('{http://request.messagepush.interfaces.comviva.com/xsd}text'==elem.tag):
                                                            send_text = (elem.text).rstrip()
                                             if('{http://request.messagepush.interfaces.comviva.com/xsd}item'==elem.tag):
                                                            subscribers = (elem.text).rstrip()
                              result_sms = send_sms(subscribers,shortCode,send_text)

Reuse it.

-----Original Message-----
From: Python-list [mailto:python-list-bounces+joaquin.alzola=lebara.com at python.org] On Behalf Of Peter Otten
Sent: 25 April 2016 11:14
To: python-list at python.org
Subject: Re: delete from pattern to pattern if it contains match

harirammanohar at gmail.com wrote:

> Here is the code:

Finally ;)

> import xml.etree.ElementTree as ET
> ET.register_namespace("", "http://xmlns.jcp.org/xml/ns/javaee")

I don't know what this does, but probably not what you expected.

> tree = ET.parse('sample.xml')
> root = tree.getroot()
>
> for servlet in root.findall('servlet'):
>         servletname = servlet.find('servlet-name').text

I think you have to specify the namespace:

for servlet in root.findall('{http://xmlns.jcp.org/xml/ns/javaee}servlet'):
    servletname = servlet.find(
        '{http://xmlns.jcp.org/xml/ns/javaee}servlet-name').text

>         if servletname == "controller":

You could have added a print statement to verify that the line below is executed.

>                 root.remove(servlet)
>
> tree.write('output.xml')
>
> This will work if <web-app> </web-app> doesnt have below...
>
> xmlns="http://xmlns.jcp.org/xml/ns/javaee"
>   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
>                       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"



--
https://mail.python.org/mailman/listinfo/python-list
This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.



More information about the Python-list mailing list