I am using lxml to read my xml file with:
tree = etree.parse(r'C:\Users\xxx\Desktop\misc work\xmledit\SalesTransactionCustom.xml')
And get an xml file like:
<?xml version="1.0" encoding="UTF-8"?>
<ProcessSalesTransactionCustom xmlns="http://schema.xxxx.com/xxxxx/2" releaseID="9.2">
<ApplicationArea>
<Sender>
<LogicalID>xxxxxx.file.syncxxxxx5salesinvoice</LogicalID>
<ComponentID>External</ComponentID>
<ConfirmationCode>OnError</ConfirmationCode>
</Sender>
<CreationDateTime>2020-04-16T14:50:26.976Z</CreationDateTime>
<BODID>xxxx-nid:xxxxx:1001::Default_1001#320000:?SalesTransactionCustom&verb=Process</BODID>
</ApplicationArea>
<DataArea>
<Process>
<TenantID>xxx</TenantID>
<AccountingEntityID>4710</AccountingEntityID>
<LocationID>S_4710</LocationID>
<ActionCriteria>
<ActionExpression actionCode="Add"/>
</ActionCriteria>
</Process>
<SalesTransactionCustom>
<FinancialBatch>
<TransactionDate>2019-09-27T00:00:00</TransactionDate>
<BatchReference>KUKS_20190928052427</BatchReference>
</FinancialBatch>
<TransactionHeader>
<TransactionType>HEI</TransactionType>
<SalesInvoice>
<Invoice>19001160</Invoice>
<BusinessPartner>417B00</BusinessPartner>
<DocumentDate>2019-09-27T00:00:00</DocumentDate>
<DueDate>2019-11-20T00:00:00</DueDate>
<Amount>152248.80</Amount>
<Currency>EUR</Currency>
<TaxCountry>DK</TaxCountry>
<TaxCode>BESIT</TaxCode>
<NonFinalizedTransaction>
<TransactionReference>417B00 PC210LCI-11</TransactionReference>
<LedgerAccount>50000400</LedgerAccount>
<Dimension1>100</Dimension1>
<Dimension2>KUK</Dimension2>
<Dimension3/>
<Dimension4/>
<Dimension5/>
<Dimension6/>
<Dimension7/>
<Dimension8/>
<TaxAmount>0.00</TaxAmount>
<DebitCreditFlag>credit</DebitCreditFlag>
<Amount>152248.80</Amount>
</NonFinalizedTransaction>
</SalesInvoice>
</TransactionHeader>
<TransactionHeader>
<TransactionType>HEI</TransactionType>
<SalesInvoice>
<Invoice>19001161</Invoice>
<BusinessPartner>412600</BusinessPartner>
<DocumentDate>2019-09-27T00:00:00</DocumentDate>
<DueDate>2019-11-20T00:00:00</DueDate>
<Amount>113848.17</Amount>
<Currency>EUR</Currency>
<TaxCountry>AT</TaxCountry>
<TaxCode>GBSI</TaxCode>
<NonFinalizedTransaction>
<TransactionReference>412600 PC210NLC-11</TransactionReference>
<LedgerAccount>50000400</LedgerAccount>
<Dimension1>100</Dimension1>
<Dimension2>KUK</Dimension2>
<Dimension3/>
<Dimension4/>
<Dimension5/>
<Dimension6/>
<Dimension7/>
<Dimension8/>
<TaxAmount>0.00</TaxAmount>
<DebitCreditFlag>credit</DebitCreditFlag>
<Amount>113848.17</Amount>
</NonFinalizedTransaction>
</SalesInvoice>
</TransactionHeader>
</SalesTransactionCustom>
</DataArea>
</ProcessSalesTransactionCustom>
I have a pandas dataframe like (here the first row are column names):
Tag Old Value New Value
BusinessPartner 417B00 BPE000104
BusinessPartner 412600 BPE000153
LedgerAccount 50000400 108092200
I want to replace the attributes of the elements in the xml with reference to this pandas dataframe. I want to be able to find the combination of tag and old value and replace the attribute with the new value. I also need to be able to write the edited text back to disk as an XML.
How can I do this with lxml and pandas?
Thank you in advance
Edit: Here is the code that works thanks to @Partha Mandal
import pandas as pd
from lxml import etree
df=pd.read_excel("Sample.xlsx")
df.columns=['Tag','Old','New']
df['Old'] = df['Old'].astype(str)
df['New'] = df['New'].astype(str)
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(r'C:\Users\xxx\Desktop\misc work\xmledit\testxml2.xml',parser)
string = etree.tostring(tree)
string = bytes.decode(string)
tag = df.Tag; old = df.Old; new = df.New
for i in range(len(tag)):
string = string.replace("<"+tag[i]+">"+old[i]+"</"+tag[i]+">","<"+tag[i]+">"+new[i]+"</"+tag[i]+">")
string=str.encode(string)
root = etree.fromstring(string)
my_tree = etree.ElementTree(root)
with open('testxml2.xml', 'wb') as f:
f.write(etree.tostring(my_tree))