Skip to content

Commit 0c4cf82

Browse files
committed
Merge pull request #32 from totaler/allow_xml_str
IMP Allow xml as str in lists for building
2 parents c1c2f02 + 79b6911 commit 0c4cf82

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ This library permits XML generation from Python objects
2222
<PRICE>9.90</PRICE>
2323
<YEAR>1988</YEAR>
2424
</CD>
25+
<CD>
26+
<TITLE>Tupelo Honey</TITLE>
27+
<ARTIST>Van Morrison</ARTIST>
28+
<COUNTRY>UK</COUNTRY>
29+
<COMPANY>Polydor</COMPANY>
30+
<PRICE>8.20</PRICE>
31+
<YEAR>1971</YEAR>
32+
</CD>
2533
</CATALOG>
2634
```
2735

@@ -69,6 +77,16 @@ cd.feed({
6977
'year': 1988
7078
})
7179
catalog.cds.append(cd)
80+
# Also we can add XML String directly as a new element
81+
cd = """<CD>
82+
<TITLE>Tupelo Honey</TITLE>
83+
<ARTIST>Van Morrison</ARTIST>
84+
<COUNTRY>UK</COUNTRY>
85+
<COMPANY>Polydor</COMPANY>
86+
<PRICE>8.20</PRICE>
87+
<YEAR>1971</YEAR>
88+
</CD>"""
89+
self.catalog.cds.append(cd)
7290
catalog.build_tree()
7391
print catalog
7492
```

libcomxml/core/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import xml.etree.cElementTree as etree
1818
except ImportError:
1919
import xml.etree.ElementTree as etree
20+
import re
2021

2122

2223
def get_xml_default_encoding():
@@ -25,6 +26,10 @@ def get_xml_default_encoding():
2526
return xml_enc
2627

2728

29+
def clean_xml(xml_string):
30+
return re.sub('\s+<', '<', xml_string)
31+
32+
2833
class Field(object):
2934
"""Base Field class
3035
"""
@@ -235,7 +240,8 @@ def build_tree(self):
235240
continue
236241
self.doc_root.append(field.doc_root)
237242
elif isinstance(field, list):
238-
# we just allow XmlFields and XmlModels in the list
243+
# we just allow XmlFields and XmlModels
244+
# Also xml as str for memory management
239245
for item in field:
240246
if isinstance(item, XmlField):
241247
ele = item.element()
@@ -247,6 +253,9 @@ def build_tree(self):
247253
if self.drop_empty and len(item.doc_root) == 0:
248254
continue
249255
self.doc_root.append(item.doc_root)
256+
elif isinstance(item, str):
257+
ele = etree.fromstring(clean_xml(item))
258+
self.doc_root.append(ele)
250259
item = None
251260
elif (field.parent or self.root.name) == self.root.name:
252261
ele = field.element()

tests/test_libcomxml.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from . import unittest
33
import locale
44
import re
5-
from libcomxml.core import XmlField, XmlModel
5+
from libcomxml.core import XmlField, XmlModel, clean_xml
66

77

88
class Cd(XmlModel):
@@ -24,6 +24,31 @@ def __init__(self):
2424
super(Catalog, self).__init__('CATALOG', 'catalog')
2525

2626

27+
class TestCleaned(unittest.TestCase):
28+
def setUp(self):
29+
self.xml = "<?xml version='1.0' encoding='UTF-8'?>\n"
30+
self.xml += """
31+
<CATALOG>
32+
<CD>
33+
<ARTIST>Bob Dylan</ARTIST>
34+
<ARTIST>Bob Dylan</ARTIST>
35+
<COMPANY>Columbia</COMPANY>
36+
<TITLE>Empire Burlesque</TITLE>
37+
<YEAR>1985</YEAR>
38+
<PRICE>10.9</PRICE>
39+
</CD>
40+
</CATALOG>"""
41+
self.cleaned_xml = "<?xml version='1.0' encoding='UTF-8'?>"
42+
self.cleaned_xml += "<CATALOG><CD><ARTIST>Bob Dylan</ARTIST>"
43+
self.cleaned_xml += "<ARTIST>Bob Dylan</ARTIST>"
44+
self.cleaned_xml += "<COMPANY>Columbia</COMPANY>"
45+
self.cleaned_xml += "<TITLE>Empire Burlesque</TITLE><YEAR>1985</YEAR>"
46+
self.cleaned_xml += "<PRICE>10.9</PRICE></CD></CATALOG>"
47+
48+
def test_clean(self):
49+
self.assertEqual(clean_xml(self.xml), self.cleaned_xml)
50+
51+
2752
class TestFields(unittest.TestCase):
2853
def setUp(self):
2954
self.field = XmlField('Quantity', '10000', attributes={'uom': 'unit'})
@@ -76,6 +101,14 @@ def setUp(self):
76101
<YEAR>1988</YEAR>
77102
<PRICE>9.9</PRICE>
78103
</CD>
104+
<CD>
105+
<TITLE>Tupelo Honey</TITLE>
106+
<ARTIST>Van Morrison</ARTIST>
107+
<COUNTRY>UK</COUNTRY>
108+
<COMPANY>Polydor</COMPANY>
109+
<PRICE>8.20</PRICE>
110+
<YEAR>1971</YEAR>
111+
</CD>
79112
</CATALOG>""")
80113
self.catalog = Catalog()
81114
cd = Cd()
@@ -99,7 +132,16 @@ def setUp(self):
99132
'year': 1988
100133
})
101134
self.catalog.cds.append(cd)
135+
cd = """<CD>
136+
<TITLE>Tupelo Honey</TITLE>
137+
<ARTIST>Van Morrison</ARTIST>
138+
<COUNTRY>UK</COUNTRY>
139+
<COMPANY>Polydor</COMPANY>
140+
<PRICE>8.20</PRICE>
141+
<YEAR>1971</YEAR>
142+
</CD>"""
143+
self.catalog.cds.append(cd)
102144
self.catalog.build_tree()
103145

104146
def test_xml(self):
105-
self.assertEqual(str(self.catalog), self.xml)
147+
self.assertEqual(str(self.catalog), self.xml)

0 commit comments

Comments
 (0)