Mit Version 0.2.0 unterstützt XmlElement, siehe XmlElement - ein einfacher XML-Generator (und Parser), nun das Erzeugen von XmlElements (und damit XML-Dateiinhalten) aus einem POJO-like Object:
from XmlElement import XmlElement as X
xml = X.from_object(node_name='CustomerFavorites', data={
'@xmlns:a': 'http://a.ecmind.ch/test/',
'@xmlns:b': 'http://b.ecmind.ch/test/',
'Customer': {
'@id': 'SISBEN',
'Name': 'Sisko',
'Firstname': 'Benjamin',
'a:Items': {'Item': [
{'b:No': 123456, 'Name': 'Bio-mimetic gel', '@clearance_required': True},
{'b:No': 234567, 'Name': 'Self-sealing stem bolts'}
]
},
'Customer': {
'@id': 'NERKIR',
'Name': 'Kira',
'Firstname': 'Nerys',
'a:Items': {'Item': [
{
'b:No': 345678,
'Name':
'Photon torpedo target system',
'@clearance_required': True,
'': 'This is the info text for a modern photon torpedos targeting system'
}
]
}
}
},
'Count': 2
}
)
print(xml)
Dies ergibt (zzgl. pretty print):
<CustomerFavorites xmlns:a="http://a.ecmind.ch/test/"
xmlns:b="http://b.ecmind.ch/test/">
<Customer id="SISBEN">
<Name>Sisko</Name>
<Firstname>Benjamin</Firstname>
<a:Items>
<Item clearance_required="true">
<b:No>123456</b:No>
<Name>Bio-mimetic gel</Name>
</Item>
<Item>
<b:No>234567</b:No>
<Name>Self-sealing stem bolts</Name>
</Item>
</a:Items>
<Customer id="NERKIR">
<Name>Kira</Name>
<Firstname>Nerys</Firstname>
<a:Items>
<Item clearance_required="true">This is the info text for a modern photon torpedos targeting system<b:No>345678</b:No>
<Name>Photon torpedo target system</Name>
</Item>
</a:Items>
</Customer>
</Customer>
<Count>2</Count>
</CustomerFavorites>
Download via pip install --upgrade XmlElement
aus PyPi: