Skip to content

Commit 4513cd3

Browse files
committed
[#149][#189] Pretty print output XML
Based on 50399b5
1 parent 28c341d commit 4513cd3

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

examples/iati/expected.xml

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,57 @@
1-
<iati-activities><!--XML generated by flatten-tool--><iati-activity><iati-identifier>AA-AAA-123456789-ABC123</iati-identifier><reporting-org ref="AA-AAA-123456789" type="40"><narrative>Organisation name</narrative></reporting-org><participating-org ref="AA-AAA-123456789" role="1"/><activity-status code="2"/><activity-date iso-date="2011-10-01" type="1"/><recipient-country code="AF" percentage="40"/><recipient-country code="XK" percentage="60"/><title><narrative>A title</narrative></title><description><narrative>A description</narrative></description><transaction><transaction-type code="2"/><transaction-date iso-date="2012-01-01"/><value value-date="2012-01-01">10</value></transaction><transaction><transaction-type code="3"/><transaction-date iso-date="2012-03-03"/><value value-date="2012-03-03">20</value></transaction></iati-activity><iati-activity><iati-identifier>AA-AAA-123456789-ABC124</iati-identifier><reporting-org ref="AA-AAA-123456789" type="40"><narrative>Organisation name</narrative></reporting-org><participating-org ref="AA-AAA-123456789" role="1"/><activity-status code="3"/><activity-date iso-date="2016-01-01" type="2"/><recipient-country code="AG" percentage="30"/><recipient-country code="XK" percentage="70"/><title><narrative>Another title</narrative></title><description><narrative>Another description</narrative></description><transaction><transaction-type code="2"/><transaction-date iso-date="2013-04-04"/><value value-date="2013-04-04">30</value></transaction><transaction><transaction-type code="3"/><transaction-date iso-date="2013-05-05"/><value value-date="2013-05-05">40</value></transaction></iati-activity></iati-activities>
1+
<iati-activities>
2+
<!--XML generated by flatten-tool-->
3+
<iati-activity>
4+
<iati-identifier>AA-AAA-123456789-ABC123</iati-identifier>
5+
<reporting-org ref="AA-AAA-123456789" type="40">
6+
<narrative>Organisation name</narrative>
7+
</reporting-org>
8+
<participating-org ref="AA-AAA-123456789" role="1"/>
9+
<activity-status code="2"/>
10+
<activity-date iso-date="2011-10-01" type="1"/>
11+
<recipient-country code="AF" percentage="40"/>
12+
<recipient-country code="XK" percentage="60"/>
13+
<title>
14+
<narrative>A title</narrative>
15+
</title>
16+
<description>
17+
<narrative>A description</narrative>
18+
</description>
19+
<transaction>
20+
<transaction-type code="2"/>
21+
<transaction-date iso-date="2012-01-01"/>
22+
<value value-date="2012-01-01">10</value>
23+
</transaction>
24+
<transaction>
25+
<transaction-type code="3"/>
26+
<transaction-date iso-date="2012-03-03"/>
27+
<value value-date="2012-03-03">20</value>
28+
</transaction>
29+
</iati-activity>
30+
<iati-activity>
31+
<iati-identifier>AA-AAA-123456789-ABC124</iati-identifier>
32+
<reporting-org ref="AA-AAA-123456789" type="40">
33+
<narrative>Organisation name</narrative>
34+
</reporting-org>
35+
<participating-org ref="AA-AAA-123456789" role="1"/>
36+
<activity-status code="3"/>
37+
<activity-date iso-date="2016-01-01" type="2"/>
38+
<recipient-country code="AG" percentage="30"/>
39+
<recipient-country code="XK" percentage="70"/>
40+
<title>
41+
<narrative>Another title</narrative>
42+
</title>
43+
<description>
44+
<narrative>Another description</narrative>
45+
</description>
46+
<transaction>
47+
<transaction-type code="2"/>
48+
<transaction-date iso-date="2013-04-04"/>
49+
<value value-date="2013-04-04">30</value>
50+
</transaction>
51+
<transaction>
52+
<transaction-type code="3"/>
53+
<transaction-date iso-date="2013-05-05"/>
54+
<value value-date="2013-05-05">40</value>
55+
</transaction>
56+
</iati-activity>
57+
</iati-activities>
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
<iati-organisations><!--XML generated by flatten-tool--><iati-organisation><organisation-identifier>AA-AAA-123456789</organisation-identifier><reporting-org ref="AA-AAA-123456789" secondary-reporter="0" type="40"/></iati-organisation></iati-organisations>
1+
<iati-organisations>
2+
<!--XML generated by flatten-tool-->
3+
<iati-organisation>
4+
<organisation-identifier>AA-AAA-123456789</organisation-identifier>
5+
<reporting-org ref="AA-AAA-123456789" secondary-reporter="0" type="40"/>
6+
</iati-organisation>
7+
</iati-organisations>

flattentool/xml_output.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,8 @@ def toxml(data, xml_root_tag):
7878
root = dict_to_xml(data, xml_root_tag, nsmap=nsmap)
7979
comment = ET.Comment('XML generated by flatten-tool')
8080
root.insert(0, comment)
81-
return ET.tostring(root)
81+
if USING_LXML:
82+
return ET.tostring(root, pretty_print=True)
83+
else:
84+
indent(root)
85+
return ET.tostring(root)

0 commit comments

Comments
 (0)