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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:19:32+00:00 2026-05-13T06:19:32+00:00

I have an XML file in the following format: <doc> <id name=X> <type name=A>

  • 0

I have an XML file in the following format:

<doc>
<id name="X">
  <type name="A">
    <min val="100" id="80"/>
    <max val="200" id="90"/>
   </type>
  <type name="B">
    <min val="100" id="20"/>
    <max val="20" id="90"/>
  </type>
</id>

<type...>
</type>
</doc>

I would like to parse this document and build a hash table

{X: {"A": [(100,80), (200,90)], "B": [(100,20), (20,90)]}, Y: .....} 

How would I do this in Python?

  • 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-13T06:19:32+00:00Added an answer on May 13, 2026 at 6:19 am

    As others have stated minidom is the way to go here. You open (and parse) the file, while going through the nodes you check if its relevant and should be read. That way, you also know if you want to read the child nodes.

    Threw together this, seems to do what you want. Some of the values are read by attribute position rather than attribute name. And theres no error handling. And the print () at the end means its Python 3.x.

    I’ll leave it as an exercise to improve upon that, just wanted to post a snippet to get you started.

    Happy hacking! 🙂

    xml.txt

    <doc>
    <id name="X">
      <type name="A">
        <min val="100" id="80"/>
        <max val="200" id="90"/>
       </type>
      <type name="B">
        <min val="100" id="20"/>
        <max val="20" id="90"/>
      </type>
    </id>
    </doc>
    

    parsexml.py

    from xml.dom import minidom
    data={}
    doc=minidom.parse("xml.txt")
    for n in doc.childNodes[0].childNodes:
        if n.localName=="id":
            id_name = n.attributes.item(0).nodeValue
            data[id_name] = {}
            for j in n.childNodes:
                if j.localName=="type":
                    type_name = j.attributes.item(0).nodeValue
                    data[id_name][type_name] = [(),()]
                    for k in j.childNodes:
                        if k.localName=="min":
                            data[id_name][type_name][0] = \
                                (k.attributes.item(1).nodeValue, \
                                 k.attributes.item(0).nodeValue)
                        if k.localName=="max":
                            data[id_name][type_name][1] = \
                                (k.attributes.item(1).nodeValue, \
                                 k.attributes.item(0).nodeValue)
    print (data)
    

    Output:

    {'X': {'A': [('100', '80'), ('200', '90')], 'B': [('100', '20'), ('20', '90')]}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an XML file like the following: <holidays> <holiday1 val=New Years, 01/01,
I have an XML File with the following format: <containers> <container> <item> item name
I have an xml file in the following format: <food> <desert> cake <desert> </food>
I have a XML file with the following data format: <net NetName=abc attr1=123 attr2=234
I have an XML file with the following line: <VALUE DECIMAL_VALUE=0.2725 UNIT_TYPE=percent/> I would
I have the following text in xml file: <Config Builder=LP Wizard> <Libraries> <Library Name=XCAMSource/>
I have a XML File in the following format: <?xml version='1.0' encoding='UTF-8'?> <entry xmlns='http://www.w3.org/2005/Atom'
I have XML in the following format: <Products> <Product name=MyProduct1> <Components> <Component name=MyComponent1> <Teams>
All, I have a config xml file in the following format: <?xml version=1.0?> <configdata>
I have an xml file with the following format <someXML> <a> <b></b> <b test='someValue'></b>

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.