Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 899373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:12:25+00:00 2026-05-15T15:12:25+00:00

I’m trying to use the lxml library to parse an XML file…what I want

  • 0

I’m trying to use the lxml library to parse an XML file…what I want is to use XML as the datasource, but still maintain the normal Django-way of interactive with the resulting objects…from the docs, I can see that lxml.objectify is what I’m suppossed to use, but I don’t know how to proceed after: list = objectify.parse('myfile.xml')

Any help will be very much appreciated. Thanks.

A sample of the file (has about 100+ records) is this:

<store>
   <book>
      <publisher>Hodder &...</publisher>
      <isbn>345123890</isbn>
      <author>King</author>
      <comments>
         <comment rank='1'>Interesting</comment>
      <comments>
      <pages>200</pages>
   </book>
   <book>
      <publisher>Penguin Books</publisher>
      <isbn>9011238XX</isbn>
      <author>Armstrong</author>
      <comments />
      <pages>150</pages>
   </book>
</store>

From this, I want to do the following (something just as easy to write as Books.objects.all() and Books.object.get_object_or_404(isbn=selected) is most preferred ):

  1. Display a list of all books with their respective attributes
  2. Enable viewing of further details of a book by selecting it from the list
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:12:25+00:00Added an answer on May 15, 2026 at 3:12 pm

    Firstly, “list” isn’t a very good variable because it “shadows” the built-in type “list.”

    Now, say you have this xml:

    <root>
    <node1 val="foo">derp</node1>
    <node2 val="bar" />
    </root>
    

    Now, you could do this:

    root = objectify.parse("myfile.xml")
    print root.node1.get("val") # prints "foo"
    print root.node1.text # prints "derp"
    print root.node2.get("val") # prints "bar"
    

    Another tip: when you have lots of nodes with the same name, you can loop over them.

    >>> xml = """<root>
        <node val="foo">derp</node>
        <node val="bar" />
        </root>"""
    >>> root = objectify.fromstring(xml)
    >>> for node in root.node:
        print node.get("val")
    
    foo
    bar
    

    Edit

    You should be able to simply set your django context to the books object, and use that from your templates.

    context = dict(books = root.book,
                   # other stuff
                   )
    

    And then you’ll be able to iterate through the books in the template, and access each book object’s attributes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
I am trying to understand how to use SyndicationItem to display feed which is
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.