Home > Blockchain >  Change Value of XML attribute using xml.etree.ElementTree
Change Value of XML attribute using xml.etree.ElementTree

Time:10-12

I have a xml like this. I am not sharing full one but a snippet

<ALLINVENTORYENTRIES.LIST>
       <STOCKITEMNAME>Item1--1ltr</STOCKITEMNAME>
       <ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>
       <ISLASTDEEMEDPOSITIVE>No</ISLASTDEEMEDPOSITIVE>
       <ISAUTONEGATE>No</ISAUTONEGATE>
       <ISCUSTOMSCLEARANCE>No</ISCUSTOMSCLEARANCE>
       <ISTRACKCOMPONENT>No</ISTRACKCOMPONENT>
       <ISTRACKPRODUCTION>No</ISTRACKPRODUCTION>
       <ISPRIMARYITEM>No</ISPRIMARYITEM>
       <ISSCRAP>No</ISSCRAP>
       <RATE>1805.08/pc</RATE>
       <AMOUNT>36101.60</AMOUNT>
       <ACTUALQTY> 20 pc</ACTUALQTY>
       <BILLEDQTY> 20 pc</BILLEDQTY>
       <INCLVATRATE>2129.99/pc</INCLVATRATE>
       <BATCHALLOCATIONS.LIST>
        <MFDON>20220813</MFDON>
        <GODOWNNAME>Main Location</GODOWNNAME>
        <BATCHNAME>ABCDEF</BATCHNAME>
        <DESTINATIONGODOWNNAME>Main Location</DESTINATIONGODOWNNAME>
        <INDENTNO/>
        <ORDERNO/>
        <TRACKINGNUMBER/>
        <DYNAMICCSTISCLEARED>No</DYNAMICCSTISCLEARED>
        <AMOUNT>36101.60</AMOUNT>
        <ACTUALQTY> 20 pc</ACTUALQTY>
        <BILLEDQTY> 20 pc</BILLEDQTY>
        <INCLVATRATE>2129.99/pc</INCLVATRATE>
        <EXPIRYPERIOD JD="44785" P="31-Aug-24">31-Aug-24</EXPIRYPERIOD>
        <ADDITIONALDETAILS.LIST>        </ADDITIONALDETAILS.LIST>
        <VOUCHERCOMPONENTLIST.LIST>        </VOUCHERCOMPONENTLIST.LIST>
       </BATCHALLOCATIONS.LIST>
       <ACCOUNTINGALLOCATIONS.LIST>
        <OLDAUDITENTRYIDS.LIST TYPE="Number">
         <OLDAUDITENTRYIDS>-1</OLDAUDITENTRYIDS>
        </OLDAUDITENTRYIDS.LIST>
        <LEDGERNAME>Sales</LEDGERNAME>
        <CLASSRATE>100.00000</CLASSRATE>
        <GSTCLASS/>
        <GSTOVRDNNATURE>Sales Taxable</GSTOVRDNNATURE>
        <ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>
        <LEDGERFROMITEM>No</LEDGERFROMITEM>
        <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES>
        <ISPARTYLEDGER>No</ISPARTYLEDGER>
        <ISLASTDEEMEDPOSITIVE>No</ISLASTDEEMEDPOSITIVE>
        <ISCAPVATTAXALTERED>No</ISCAPVATTAXALTERED>
        <ISCAPVATNOTCLAIMED>No</ISCAPVATNOTCLAIMED>
        <AMOUNT>36101.60</AMOUNT>
        <SERVICETAXDETAILS.LIST>        </SERVICETAXDETAILS.LIST>
        <BANKALLOCATIONS.LIST>        </BANKALLOCATIONS.LIST>
        <BILLALLOCATIONS.LIST>        </BILLALLOCATIONS.LIST>
        <INTERESTCOLLECTION.LIST>        </INTERESTCOLLECTION.LIST>
        <OLDAUDITENTRIES.LIST>        </OLDAUDITENTRIES.LIST>
        <ACCOUNTAUDITENTRIES.LIST>        </ACCOUNTAUDITENTRIES.LIST>
        <AUDITENTRIES.LIST>        </AUDITENTRIES.LIST>
        <INPUTCRALLOCS.LIST>        </INPUTCRALLOCS.LIST>
        <DUTYHEADDETAILS.LIST>        </DUTYHEADDETAILS.LIST>
        <EXCISEDUTYHEADDETAILS.LIST>        </EXCISEDUTYHEADDETAILS.LIST>
        <RATEDETAILS.LIST>        </RATEDETAILS.LIST>
        <SUMMARYALLOCS.LIST>        </SUMMARYALLOCS.LIST>
        <STPYMTDETAILS.LIST>        </STPYMTDETAILS.LIST>
        <EXCISEPAYMENTALLOCATIONS.LIST>        </EXCISEPAYMENTALLOCATIONS.LIST>
        <TAXBILLALLOCATIONS.LIST>        </TAXBILLALLOCATIONS.LIST>
        <TAXOBJECTALLOCATIONS.LIST>        </TAXOBJECTALLOCATIONS.LIST>
        <TDSEXPENSEALLOCATIONS.LIST>        </TDSEXPENSEALLOCATIONS.LIST>
        <VATSTATUTORYDETAILS.LIST>        </VATSTATUTORYDETAILS.LIST>
        <COSTTRACKALLOCATIONS.LIST>        </COSTTRACKALLOCATIONS.LIST>
        <REFVOUCHERDETAILS.LIST>        </REFVOUCHERDETAILS.LIST>
        <INVOICEWISEDETAILS.LIST>        </INVOICEWISEDETAILS.LIST>
        <VATITCDETAILS.LIST>        </VATITCDETAILS.LIST>
        <ADVANCETAXDETAILS.LIST>        </ADVANCETAXDETAILS.LIST>
       </ACCOUNTINGALLOCATIONS.LIST>
       <DUTYHEADDETAILS.LIST>       </DUTYHEADDETAILS.LIST>
       <SUPPLEMENTARYDUTYHEADDETAILS.LIST>       </SUPPLEMENTARYDUTYHEADDETAILS.LIST>
       <TAXOBJECTALLOCATIONS.LIST>       </TAXOBJECTALLOCATIONS.LIST>
       <REFVOUCHERDETAILS.LIST>       </REFVOUCHERDETAILS.LIST>
       <EXCISEALLOCATIONS.LIST>       </EXCISEALLOCATIONS.LIST>
       <EXPENSEALLOCATIONS.LIST>       </EXPENSEALLOCATIONS.LIST>
       <UDF:FSITEMPESTUDF.LIST DESC="`FSItempestUDF`" ISLIST="YES" TYPE="String" INDEX="1006">
        <UDF:FSITEMPESTUDF DESC="`FSItempestUDF`">-</UDF:FSITEMPESTUDF>
       </UDF:FSITEMPESTUDF.LIST>
       <UDF:FSITEMIN_UDF.LIST DESC="`FSitemIn_UDF`" ISLIST="YES" TYPE="String" INDEX="1012">
        <UDF:FSITEMIN_UDF DESC="`FSitemIn_UDF`">-</UDF:FSITEMIN_UDF>
       </UDF:FSITEMIN_UDF.LIST>
       <UDF:SOLDFORWHICHCROP.LIST DESC="`SoldForWhichCrop`" ISLIST="YES" TYPE="String" INDEX="8897">
        <UDF:SOLDFORWHICHCROP DESC="`SoldForWhichCrop`">-</UDF:SOLDFORWHICHCROP>
       </UDF:SOLDFORWHICHCROP.LIST>
      </ALLINVENTORYENTRIES.LIST>

I want to set the value of RATE to new value

I tried below code

import xml.etree.ElementTree as ET

tree = ET.parse(r'Book1.xml')
root = tree.getroot()

for i in root[1][0][1][0][0]:
    #print(i.tag)
    if i.tag == "ALLINVENTORYENTRIES.LIST":
        for elem in i:
            #print(elem.tag)
            rpt_side = i.find('RATE')
            print(rpt_side.text)
            rpt_side.set(rpt_side.tag, "2000")
            #print(a)
            print(rpt_side.text)
            #print(elem)

It is adding new value as

<RATE RATE="2000">1805.08/pc</RATE>

I want value same as it is in my xml

CodePudding user response:

The set() method in ElementTree sets the attribute value of an attribute of an element to a particular value. If the attribute doesn't exist - the method creates it.

So

rpt_side.set(rpt_side.tag, "2000")

creates an attribute with a name equal to the name of the element in question (rpt_side.tag) (that is RATE) and assigns to it 2000 as the attribute value.

What you are probably looking for is something like:

rpt_side.text="2000"

which should leave the element intact, except for the new text value.

  • Related