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

The Archive Base Latest Questions

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

I have an XML file with the following structure: <root> <subroot id=someID> <val1 value=a/>

  • 0

I have an XML file with the following structure:

<root>
  <subroot id="someID">
    <val1 value="a"/>
    <val2 value="b"/>
    <val3 value="c"/>
    <val4 value="1"/>
    <val5 value="2"/>
    <val6 value="3"/>
    <otherval value="xyz"/>
  </subroot>
  <subroot id="anotherID">
    <val1 value="aa"/>
    <val2 value="bb"/>
    <val3 value="cc"/>
    <val4 value="11"/>
    <val5 value="22"/>
    <val6 value="33"/>
    <otherval value="xxyyzz"/>
  </subroot>
  .
  .
  .
  .
</root>

I am trying to retrieve the values in each tag. For example, my desired output would be:

val1=a
val2=b
val3=c
val4=1
val5=2
val6=3
otherval=xyz

Here is my non-working code that produces blanks, ie val1="", val2=""...:

def getValues(self):
    from xml.dom.minidom import parseString
    import json

    file = open('myfile.xml','r')
    data = file.read()
    dom = parseString(data)
    rows = dom.getElementsByTagName("root")[0].getElementsByTagName("subroot")
    valueString = ""
    for row in rows:
        valueString = valueString+json.dumps(
        {
        'val1': row.getAttribute("val1"),
        'val2': row.getAttribute("val2"),
        'val3': row.getAttribute("val3"),
        'val4': row.getAttribute("val4"),
        'val5': row.getAttribute("val5"),
        'val6': row.getAttribute("val6"),
        'other': row.getAttribute("otherval")},
        sort_keys=True,
        indent=4)+","
    response_generator = ( "["+valueString[:-1]+"]" )
    return HttpResponse(response_generator)
    otherval=xyz

I know that thisactually produces JSON but thats not important. The important thing is being able to extract the values and then I can do whatever with them afterwards.

Can someone show me what I’m missing here?
Also, should I change my XML so all the val1,val2,val3.... are just called val?

Thanks.

  • 1 1 Answer
  • 3 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-25T15:39:44+00:00Added an answer on May 25, 2026 at 3:39 pm

    My suggestion is to structure your data as follows:

    <root>
      <subroot id="someID">
        <value id="1">a</value>
        <value id="2">b</value>
        <value id="3">c</value>
        <value id="4">d</value>
        <value id="5">2</value>
        <value id="6">3</value>
        <value id="other">xyz</value>
      </subroot>
      <subroot id="anotherID">
        <value id="1">aa</value>
        <value id="2">bb</value>
        <value id="3">cc</value>
        <value id="4">11</value>
        <value id="5">22</value>
        <value id="6">33</value>
        <value id="other">xxyyzz</value>
      </subroot>
    </root>
    

    Then, to parse, I suggest the etree library – it’s in the standard library for Python and I’ve found it far nicer to work with than anything else. It’s just a simple case of iterating through the subroots and value elements and extracting the data.

    from xml.etree import ElementTree
    
    xml = ElementTree.parse("test.xml")
    
    root = xml.getroot()
    
    all = {}
    
    for group in root.findall("subroot"):
        temp = {}
        for value in group.findall("value"):
            temp[value.get("id")] = value.text
        all[group.get("id")] = temp
    

    all will then be:

    {'someID': {'1': 'a', '3': 'c', '2': 'b', '5': '2', '4': 'd', '6': '3', 'other': 'xyz'}, 'anotherID': {'1': 'aa', '3': 'cc', '2': 'bb', '5': '22', '4': '11', '6': '33', 'other': 'xxyyzz'}}
    

    You could also do this as a dict comprehension:

    all = {group.get("id"): {value.get("id"): value.text for value in group.findall("value")} for group in root.findall("subroot")}
    

    Note that this is a little hard to read and will fall apart if you try to do anything more complex.

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

Sidebar

Related Questions

The xml file is having the following structure <Root> <Child value=A/> <Child value=B/> <Child
I have an XML file of the following structure: <?xml version=1.0 encoding=utf-8?> <root> <ArrayOfLocationDTO
I have an XML file of the following structure <Root> <Child Name=First> <Data Name=a
I have a large xml file (approx. 10 MB) in following simple structure: <Errors>
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
I have an XML file with the following line: <VALUE DECIMAL_VALUE=0.2725 UNIT_TYPE=percent/> I would
I have an XML file in the following structure. <NewDataSet> <markers> <name>name text</name> <desc>desc
I have an XML file with the following structure: <Products> <Product name=MyProduct1> <Components> <Component
The XML file have the following structure <RootElement> <TestElement Count=10/> </RootElement> I need to
I have a sample htm file with following structure and it POSTs the xml

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.