how can it do an xml with element tree , python and a loop to look like this?

Viewed 20

I need to do something like

<a code="007">
  <element name="b" replace="true" id="" date="02-09-2022 15:30:00" special="true" from="" to="">
  <component name="value" valor="55"/>
  </element>
<element name="c" replace="true" id="" date="02-09-2022 18:30:00" special="true" from="" to="">
  <component name="value" valor="15"/>
  </element>

   ****a loop for "n" elements here****       

    </a>

i have a code that is constantly open and close "a" in every round of "element" , thats my biggest problem.

i would need to open a at the begging , then a loop (could be a for) to fill the list of "element" , and then to close the "a" tag.

any hint would be very appreciated

1 Answers

I suggest taking look at xml.dom.minidom, consider following simple example

from xml.dom.minidom import getDOMImplementation
impl = getDOMImplementation()
doc = impl.createDocument(None, "a", None)
for i in ['X','Y','Z']:
    elem = doc.createElement("b")
    elem.setAttribute("idcode",i)
    doc.documentElement.appendChild(elem)

gives output

<?xml version="1.0" ?><a><b idcode="X"/><b idcode="Y"/><b idcode="Z"/></a>
Related