I hope this is not a duplicate.
For some time now, I've been using a pattern which I think could be done more elegantly. I have a lot of XML files that need to be represented in a Django views. My current approach is like this:
I get a XML response from an API which gives me several documents.
First I define a mock class like this:
class MockDocument:
pass
Then I write a XPath expression that gives me a list of the document nodes:
from lxml import etree
with open("xmlapiresponse.xml", "r") as doc:
xml_response = doc.read()
tree = etree.fromstring(xml_response)
document_nodes = tree.xpath("//documents/item")
Now I iterate over the different documents and their fields using for nested-loops:
# list for storing the objects
document_objects = []
for document in document_nodes:
# create a MockDocument for each item returned by the API
document_object = MockDocument()
for field in document:
# store the corresponding fields as object attributes
if field.tag == "something":
document.something = field.text
document_objects.append(document)
Now I can pass the list of mock objects to a django view and represent them in the template in whichever way I want. Could this be done in a more simple fashion, though?