1

Here is the code but the exported xml appears badly formatted.

import xml.etree.ElementTree as ET
import os

sampleXML = """<?xml version="1.0" encoding="ASCII"?>
    <Metadata version="1.0">
        <CODE_OK>510</CODE_OK>
        <DeliveryDate>13/08/2018</DeliveryDate>
    </Metadata>
    """

tree = ET.ElementTree(ET.fromstring(sampleXML))
for folder in os.listdir("YourPath"):         #Iterate the dir
    tree.find("CODE_OK").text = folder        #Update dir name in XML
    tree.write(open(os.path.join(r"Path", folder, "newxml.xml"), "wb")) #Write to XML

How to make the exported xml appear normally formatted?

1 Answers1

1

I found in docs that xml module has an implementation of Document Object Model interface. I provide a simple example

from xml.dom.minidom import parseString

example = parseString(sampleXML) # your string

# write to file
with open('file.xml', 'w') as file:
    example.writexml(file, indent='\n', addindent=' ')

Output:

<?xml version="1.0" ?>
<Metadata version="1.0">


 <CODE_OK>510</CODE_OK>


 <DeliveryDate>13/08/2018</DeliveryDate>


</Metadata>

Update

You can also write like this

example = parseString(sampleXML).toprettyxml()
with open('file.xml', 'w') as file:
    file.write(example)

Output:

<?xml version="1.0" ?>
<Metadata version="1.0">


    <CODE_OK>510</CODE_OK>


    <DeliveryDate>13/08/2018</DeliveryDate>


</Metadata>

Update 2

I copy all your code and only add indent from this site. And for me is working correctly

import xml.etree.ElementTree as ET
import os

sampleXML = "your xml"
tree = ET.ElementTree(ET.fromstring(sampleXML))

indent(tree.getroot()) # this I add

for folder in os.listdir(path):
    tree.find("CODE_OK").text = folder
    tree.write(open(os.path.join(path, folder, "newxml.xml"), "wb"))
RobJan
  • 1,351
  • 1
  • 14
  • 18
  • I thought that you have problem with formatting string to _xml_ file – RobJan Aug 06 '18 at 13:12
  • This is correct. Is that what the code does? Just explain what the code does. Thanks – user10185274 Aug 06 '18 at 13:14
  • You put your string into `parseString` function which return a [_Document_](https://docs.python.org/3/library/xml.dom.minidom.html#xml.dom.minidom.parseString). Then you open your file in write mode and use `writexml` method on the _Document node_ and specify indent and addindent (_The `addindent` parameter is the incremental indentation to use for subnodes of the current one_) – RobJan Aug 06 '18 at 13:21
  • The code seems fine now thanks but it uses a different library than what I used therefore i don't know how to do the rest of the my code where replaces a certain number with the name of every folder. Do you see that in the question's code? So with this we fix something but lose something else. Do you know how to do what I did using your library too? – user10185274 Aug 06 '18 at 13:30
  • or maybe read somehow the xml I made and change the format to what your code does? – user10185274 Aug 06 '18 at 13:31
  • I found this [indent](https://stackoverflow.com/a/12940014/9511702) function. You simply before for loop add `indent(tree)` in your code. Check and response – RobJan Aug 06 '18 at 13:43
  • TypeError: object of type 'ElementTree' has no len() – user10185274 Aug 06 '18 at 13:52
  • If you can find a way to do what I did in the loop that can work with your code would be good. – user10185274 Aug 06 '18 at 13:58
  • Instead of `tree` try `tree.getroot()` – RobJan Aug 06 '18 at 14:07
  • it runs now but nothing changes. I typed: `indent(tree.getroot())` and later the loop. – user10185274 Aug 06 '18 at 14:16
  • It's weird. I reproduce your example. Before for loop I insert `indent(tree.getroot())` and I recive formatted files. – RobJan Aug 06 '18 at 14:39
  • Can you edit your answer with the code as you write it as well with the existing code you wrote? Thank you. – user10185274 Aug 06 '18 at 14:42