Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions for the entity list #3

Open
ray-linn opened this issue Feb 27, 2015 · 2 comments
Open

Questions for the entity list #3

ray-linn opened this issue Feb 27, 2015 · 2 comments

Comments

@ray-linn
Copy link

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?
@fdintino
Copy link
Member

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.

@ray-linn
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants