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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:14:52+00:00 2026-06-11T15:14:52+00:00

I want to extract attributes out of the xml file using this code xml

  • 0

I want to extract attributes out of the xml file using this code
xml file is :

xml= "<graphics type='xxx' port=’0’ autoport='xxx' listen='0.0.0.0'>
  <listen type='address' address='0.0.0.0'/>
</graphics>"

and the code is :

def xml_to_dict(xml):  
      d={}   
      if xmlk.text:
         d[xmlk.tag] = xmlk.text  
      else:
         d[xmlk.tag] = {}   
     children = xmlk.getchildren()   
     if children:
         d[xmlk.tag] = map(xml_to_dict, children)  
         return d

     xml_to_dict(xyz) Output: {'graphics': [{'listen': {}}]}

i have tried dmlk,attrib instead of tag but to no avail. Does anybody knows this

  • 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-11T15:14:52+00:00Added an answer on June 11, 2026 at 3:14 pm

    I’d suggest lxml, but the following code works with either lxml or ElementTree

    You also need to tweak your agorithm a bit:

    from xml.etree import ElementTree as etree
    tree = etree.fromstring(xml)
    
    def xml_to_dict(tree):  
      d={}   
      if tree.text:
         d[tree.tag] = tree.text  
      elif len(tree) < 0:
         d[tree.tag] = {}   
      else:   
         d[tree.tag] = map(xml_to_dict, tree)  
     return d
    

    That gives you what you asked for.

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

Sidebar

Related Questions

I want to extract attributes from an xml file using Sax Parser I am
I am querying a particularly ugly HTML file using xpath. I want to extract
This is a xml structure that I want to parse using libxml parsing. How
I want to extract tags in XML which contains attribute condition as name=var .
I want extract the path of images from a html page using PHP-preg_match_all(), the
I want to extract the file name 13572_BranchInformationReport_2012-06-28.zip from the following text - 1:30,/icons/def13572_BranchInformationReport_2012-06-28.zip,13572_BranchInformationReport_2012-06-28.zip,0,184296,Jun
I want to extract the value between the apostrophes, for example from this string:
I want to extract information from Twitter using Python. There is a page with
I'm trying to parse this feed: http://musicbrainz.org/ws/1/artist/c0b2500e-0cef-4130-869d-732b23ed9df5?type=xml&inc=url-rels I want to grab the URLs inside
Hi I am trying to parse data out of an XML file I exported

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.