0

I have a xml tree and I would like to replace the sub elements in the xml structure. This is actual xml tree read from the file

xml_data = ET.parse('file1.xml')

<?xml version='1.0' encoding='UTF-8'?> 
<call method="xxxx" callerName="xxx">  
<credentials login="" password=""/>
<filters>
<accounts>
<account code="" ass="" can=""/>
</accounts>
</filters>
</call>

I'm expecting this format from looping the list

a = [1,23453, 3543,4354,3455, 6345]

<?xml version='1.0' encoding='UTF-8'?> 
<call method="xxxx" callerName="xxx">  
<credentials login="" password=""/>
<filters>
<accounts>
<account code="1" ass="34" can="yes"/>
<account code="23453" ass="34" can="yes"/>
<account code="3543" ass="34" can="yes"/>
<account code="4354" ass="34" can="yes"/>
<account code="3455" ass="34" can="yes"/>
<account code="6345" ass="34" can="yes"/>
</accounts>
</filters>
</call>

New to xml-parsing. Any help would be appreciated. Thanks in advance

1 Answer 1

1

I'm new in python, but it's work. I find only 1 bug: len of a must be equal len . It would be great if i help you. Good luck.

from xml.etree import ElementTree as ET

a = [1, 23453, 3543, 4354, 3455, 6345]
code = 0
xmlfile = "./log/logs.xml"
tree = ET.parse(xmlfile)
root = tree.getroot()
for filters in root.findall("filters"):
    for accounts in filters.findall("accounts"):
        for account in accounts.findall("account"):
            attributes = account.attrib
            attributes["code"] = str(a[code])
            attributes["ass"] = "34"
            attributes["can"] = "yes"
            code += 1
tree.write(xmlfile)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.