0

I am trying to achieve merge of two xmls together then save it.

every XML has structure to this:

<graphflow>
    <graphnodes>
        <graphnode>
            <attr some="attributes">
        </graphnode>
    </graphnodes>
    <flows>
        <graph from="1" to="2"/>
    </flows>
</graphflow>

I have merged two XMLs with following function:

def _merge_taskflow_with_gpp_xml(f1, f2):
    f1_task = f1.find("graphnodes")
    f2_task = f2.find("graphnodes")
    f1_task.extend(f2_task)
    f1_deps = f1.find("flows")
    f2_deps = f2.find("flows")
    f1_deps.extend(f2_deps)
    return f1

Then I used pretty print:

def _taskflow_pretty_print(input):
    result = ET.tostring(input, "utf-8")
    res_reparsed = minidom.parseString(result)
    res_reparsed = res_reparsed.toprettyxml(indent=4 * " ")
    return res_reparsed

But result was not met in terms of "pretty" :), as there are whitespaces that troubles me. If I look into element, flow.text is None.

Part of XML1:

<graph from="1" to="2"/>
<graph from="2" to="3"/>
<graph from="3" to="100"/>

Part of XML2:

<graph from="100" to="101"/>
<graph from="101" to="102"/>
<graph from="100" to="103"/>
<graph from="100" to="104"/>

Expectation:

<graph from="1" to="2"/>
<graph from="2" to="3"/>
<graph from="3" to="100"/>
<graph from="100" to="101"/>
<graph from="101" to="102"/>
<graph from="100" to="103"/>
<graph from="100" to="104"/>

Reality:

<graph from="1" to="2"/>
<graph from="2" to="3"/>
<graph from="3" to="100"/>


<graph from="100" to="101"/>


<graph from="101" to="102"/>


<graph from="100" to="103"/>


<graph from="100" to="104"/>

What am I missing? Thanks for answers

  • In Python 3.9, ElementTree has a built-in function for pretty-printing that you might want to try: https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.indent. Also note that questions about pretty-printing XML have been asked and answered many times before: https://stackoverflow.com/q/749796/407651 – mzjn Sep 16 '21 at 10:31

0 Answers0