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 8604467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:34:51+00:00 2026-06-12T02:34:51+00:00

Just a heads up, I’m using lxml imported like this: from lxml import etree

  • 0

Just a heads up, I’m using lxml imported like this: from lxml import etree.

I’m trying to make some working code more flexible. My script takes an input .xml file containing all the items in an order by name, refers to a dictionary containing item ID numbers and item names to generate a list of item IDs based off that .xml file, and then polls a web server for the price data of those item IDs which it then uses to output a grand total cost. My question is not about any of these steps.

Rather my question is about how to handle parsing an .xml file that contains more than one order in it.

So far my sample .xml file might look like this:

<?xml version="1.0" ?>
    <cars>
        <order="This is a test order">
            <description value=""/>
            <carType value="Model1"/>
            <upgrade slot="interior 0" type="Leather Seats"/>
            <upgrade slot="interior 1" type="6-Disc CD Player"/>
        </order>
    </cars>

I can parse this into a list that includes both the base item carType (which is the model of car) and the various upgrades like this:

for element in root.iterchildren('carType'):
    modlist.append ("%s" % (element.get('value')))

for element in root.iter('upgrade'):
    modlist.append ("%s" % str.upper((element.get('type'))))

and it would give me a list called modlist like ['Model1', 'Leather Seats', '6-Disc CD Player'] which I can run through my other functions to get the ID numbers for those items and from there get the price information and total it up to find out how much this Model1 car with a Leather Seat upgrade and a 6-Disc CD Player upgrade would cost.

Here is where I run in to difficulty. How can I have multiple cars in one .xml file? An example might look like this:

<?xml version="1.0" ?>
    <cars>
        <order="This is a test order">
            <description value=""/>
            <carType value="Model1"/>
            <upgrade slot="interior 0" type="Leather Seats"/>
            <upgrade slot="interior 1" type="6-Disc CD Player"/>
        </order>
        <order="This is a 2nd order">
            <description value=""/>
            <carType value="Model3"/>
            <upgrade slot="interior 0" type="Vinyl Seats"/>
            <upgrade slot="wheels 0" type="Chrome Wheels"/>
            <upgrade slot="wheels 1" type="8 Ply Tires"/>
        </order>
        <order="This is a 3rd order">
            <description value=""/>
            <carType value="Model7"/>
            <upgrade slot="engine 0" type="V8"/>
            <upgrade slot="interior 0" type="Leather Seats"/>
            <upgrade slot="interior 1" type="Sunroof"/>
        </order>
    </cars>

I want to run my functions on one order at a time so that this example would output 3 numbers – the grand total price of this Model1 car with its upgrades, the grand total price of the Model3 car with its upgrades, and the grand total price of the Model7 car with its upgrades.

How can I tell my functions to just run on one order at a time? I’m thinking something about iterchild() but I can’t get anything to work.

  • 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-06-12T02:34:52+00:00Added an answer on June 12, 2026 at 2:34 am

    Instead of calling root.iterchildren, use order.iterchildren, where order iterates through <order> elements:

    for order in root.xpath('//order'):
        modlist = []
        for element in order.iterchildren('carType'):
            modlist.append ("%s" % (element.get('value')))
    
        for element in order.iter('upgrade'):
            modlist.append ("%s" % str.upper((element.get('type'))))
        # report grand total for order
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wanted to run this by other heads to make sure I wasn't
Just as a little heads up, I'm completely new to this whole coding thing
Using Git Bash, I just edited some commits. In the shell it says: Successfully
I am just starting out with GIT (i'm coming from cvs) and would like
I've been trying to Google and find some guides or heads-up on how to
I've been trying to filter this array (which is full of NSDictionaries) using NSPredicate...
Well being a total newbie to ASP.NET, I just want some heads up that
I've just wrapped my head around monads (at least I'd like to think I
Just trying to get my head around what can happen when things go wrong
Just trying to still get my head around IOC principles. Q1: Static Methods -

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.