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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:27:33+00:00 2026-05-29T20:27:33+00:00

I have a python script that parses an XML file that contains part information

  • 0

I have a python script that parses an XML file that contains part information as well as a command on what the script should do with the information.

<?xml version="1.0"?>
<servicexml>
    <action>
        <command>Add</command>
        <partnumber>1223</partnumber>
        <partname>Battery</partname>
        <partdescription>Holds charge</partdescription>
    </action>

    <action>
        <command>Add</command>
        <partnumber>4444</partnumber>
        <partname>Pump</partname>
        <partdescription>Pumps stuff</partdescription>
    </action>

</servicexml>

I am attempting to write a python script that will strip out all relevant information and put it into a dictionary so I can look up values by key. Currently my code can only hold a single dimension – this means that in my XML file the last tag is the only value written. How Can I dynamically allocate dimensions in my dictionary to hold multiple actions from my XML file? This way I can access key values for each part sequence.

Here is my Python code

from lxml import etree
from StringIO import StringIO

actionInformation = []
tagsOfInterest = ['command','partnumber','partname','partdescrip']
tagDataOfIntrest = {}
xmlFile = "parts.xml"
context = etree.iterparse(xmlFile)

for action, elem in context:
    if elem.tag in tagsOfInterest:
        actionInformation.append([elem.tag,elem.text])

tagDataOfInterest = dict(actionInformation)    
print tagDataOfInterest

SOLUTION Based on David Alber’s Answer

I had to change some of the imported modules however his method was still used.

from lxml import etree
xmlFile = "parts.xml"
context = etree.parse(xmlFile)
actions = context.findall('action')

parsed = [{field.tag: field.text for field in action} for action in actions]
  • 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-29T20:27:34+00:00Added an answer on May 29, 2026 at 8:27 pm

    Here is an approach that works. It does not make the partnumber elements integers, but it does not look like you were doing that anyway. It would not be much trouble to modify it for that, though.

    from xml.etree.ElementTree import ElementTree
    
    tree = ElementTree()
    tree.parse(xmlFile)
    actions = tree.findall('action')
    
    parsed = [{field.tag: field.text for field in action} for action in actions]
    

    Fully self-contained example

    Here is a fully contained example to allow easy verification. The difference is that xmlFile from above has been replaced with a StringIO object.

    import StringIO
    from xml.etree.ElementTree import ElementTree
    
    s = """<?xml version="1.0"?>
    <servicexml>
        <action>
            <command>Add</command>
            <partnumber>1223</partnumber>
            <partname>Battery</partname>
            <partdescription>Holds charge</partdescription>
        </action>
    
        <action>
            <command>Add</command>
            <partnumber>4444</partnumber>
            <partname>Pump</partname>
            <partdescription>Pumps stuff</partdescription>
        </action>
    
    </servicexml>"""
    
    st = StringIO.StringIO(s)
    
    tree = ElementTree()
    tree.parse(st)
    actions = tree.findall('action')
    
    parsed = [{field.tag: field.text for field in action} for action in actions]
    

    After running this, you can do

    >>> parsed
    [{'command': 'Add',
      'partdescription': 'Holds charge',
      'partname': 'Battery',
      'partnumber': '1223'},
     {'command': 'Add',
      'partdescription': 'Pumps stuff',
      'partname': 'Pump',
      'partnumber': '4444'}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script that parsing an xml file and is returning the
I have a python script that has to launch a shell command for every
I have a python script that, once executed from command line, performs the needed
I have a python script which should parse a file and produce some output
I am writing a python script that parses a 3D model file from one
I have a python script that analyzes a set of error messages and checks
I have a Python script that needs to execute an external program, but for
I have a python script that is a http-server: http://paste2.org/p/89701 , when benchmarking it
I have a python script that I would like to add a Shutdown when
I have a Python script that calls an executable program with various arguments (in

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.