django-xml icon indicating copy to clipboard operation
django-xml copied to clipboard

Questions for the entity list

Open ray-linn opened this issue 10 years ago • 2 comments

Thanks a lot for the django-xml, it is pretty nice to work for XML files. and I have 2 questions when keep me in trouble for several weeks, could you help to explain for it?

  1. In your advance exmaple, the element etnries is mapped via following code entries = xmlmodels.XPathListField("/atom:feed/atom:entry", required=False) then it will return a node_set list to entries field, but how can map as a list of plain object? The plain object looks like class entry title = xmlmodels.XPathTextFiled("("/atom:feed/atom:entry/atom:title) ..... I tried lxml_extension to map each element as the plain object ,but it reports "This is not a supported node-set result: <entry: entry object>", do you have any solution for this?
  2. is it possible to store the XmlModel back into database after parser the XML?

ray-linn avatar Feb 27 '15 08:02 ray-linn

To answer you first question: yes, there is a way to do what you're asking. django-xml has "embedded" fields which act similarly to the Django ForeignKey fields (the code can be found here). Here is how you would point each atom:entry to an instance of another XmlModel:

class AtomFeed(xmlmodels.XmlModel):
    class Meta:
        extension_ns_uri = "urn:local:atom-feed-functions"
        namespaces = {
            "fn":   extension_ns_uri,
            "atom": "http://www.w3.org/2005/Atom",
        }
    # ... other fields
    entries = xmlmodels.EmbeddedXPathListField("AtomEntry", "/atom:feed/atom:entry",
        required=False)

class AtomEntry(xmlmodels.XmlModel):
    class Meta:
        namespaces = {"atom": "http://www.w3.org/2005/Atom"}
    title = xmlmodels.XPathTextField("atom:title", required=False)
    # etc...

then the values can be accessed by using:

for entry in atom_feed.entries:
    print "title = %s" % entry.title

With regard to your second question, I'm afraid the answer is no. I have not built the functionality that would allow setting data and serializing it back to XML. I can definitely see the usefulness of such a feature, but I haven't ever gotten around to writing it.

fdintino avatar Feb 27 '15 13:02 fdintino

Thanks a lot for your kindly support. Via the experience in C#, serialize/deserialize from/to XML is really an elegant method to deal with markup files. And I also like to see any method to serialzie the XmlModel into database, as we know, it is hard to doing statistics across XML.

ray-linn avatar Feb 28 '15 02:02 ray-linn