XML Parsing

Peter Otten __peter__ at web.de
Mon Sep 4 06:26:12 EDT 2017


Sambit Samal wrote:

> Hi ,
> 
> Need help in Python Script using xml.etree.ElementTree  to update the
> value of any element in below XML ( e.g SETNPI to be 5 ) based on some
> constraint ( e.g <ruleset id="2"> ) .

Something along the lines

from xml.etree import ElementTree as ET

tree = ET.parse("original.xml")
for e in tree.findall(".//ruleset[@id='2']//SETNPI"):
    e.text = "5"
tree.write("modified.xml")

might work for you.

> <?xml version="1.0"?>
>           <TRN xmlns:xsi="http:/www.w3.org/2001/XMLSchema-instance">
>         <ProfileName>DRATRN</ProfileName>
>             <Rules>
>         
>         <ruleset id="1">
>          <Priority>1</Priority>
>          <!-- Set_NAI_1_NPI_1_CLNGPN -->
>            <TriggerPoint>
>          <ConditionTypeCNF>1</ConditionTypeCNF>
>              <SPT id="0">
>              <Group>0</Group>
>               <CALLEVENT>ORIG</CALLEVENT>
>              <CALLEVENT>CFORIG</CALLEVENT>
>             <CALLEVENT>TERM</CALLEVENT>
>                </SPT>
>             </TriggerPoint>
>             <Action>
>               <CLNGPN_DIGITS>
>            <SETNAI>1</SETNAI>
>               <SETNPI>1</SETNPI>
>               </CLNGPN_DIGITS>
>              <CAUSE>CONTINUE</CAUSE>
>             </Action>
>            </ruleset>
> 
>           <ruleset id="2">
>           <Priority>2</Priority>
>             <TriggerPoint>
>             <ConditionTypeCNF>1</ConditionTypeCNF>
>              <SPT id="0">
>               <Group>0</Group>
>               <CALLEVENT>TERM</CALLEVENT>
>               </SPT>
>                 </TriggerPoint>
>             <Action>
>               <CLDPN_DIGITS>
>                <SETNAI>1</SETNAI>
>                <SETNPI>1</SETNPI>
>                </CLDPN_DIGITS>
>                <CAUSE>CONTINUE</CAUSE>
>                </Action>
>                </ruleset>





More information about the Python-list mailing list