xml tags in a list

This is the place for queries that don't fit in any of the other categories.

xml tags in a list

Postby bajaj » Wed May 29, 2013 7:04 pm

Hi
My xml file is like this
Code: Select all
<feed text='11' r_version='abc'>
     <entry>   
        <author>   
            <name>Mark</name>
            <uri>ha</uri>   
       </author>
       <title>Dive into history, 2009 edition</title> 
       <link rel='alternate' type='text/html'  href='aa'/> 
       <id>tag20090327172042</id>
       <updated>2009-03-27T21:56:07Z</updated>
       <published>2009-03-27T17:20:42Z</published>
       <category scheme='bb' term='diveintopython'/>   
       <category scheme='b1' term='docbook'/>   
       <category scheme='b2' term='html'/>
       <summary type='html'>some text</summary>
     </entry>
     <entry>
        <author>
             <name>Mark</name>   
             <uri>hw</uri> 
        </author>   
        <summary type='html'>some other text.</summary>
     </entry>
</feed>


what I am trying to do is read this xml file using ElementTree and save it in the list. I am getting that result but the list braket are wrong.

Code: Select all
tree =ET.parse(my.xml)
   root=tree.getroot()
   element_list=[]
   for elem in tree.iter():   
         element_list=(elem.tag)
         child_dict=(elem.attrib)
         temp_list=[]
         temp_list.append(element_list)
         temp_list.append(child_dict)
         all_list_item.append(temp_list)         
    print(all_list_item)


output---
Code: Select all
[["feed", {"text": "11", "r_version": "abc"}], ["entry", {}], ["author", {}], ["name", {}], ["uri", {}], ["title", {}], ["link", {"href": "aa", "type": "text/html", "rel": "alternate"}], ["id", {}], ["updated", {}], ["published", {}], ["category", {"term": "diveintopython", "scheme": "bb"}], ["category", {"term": "docbook", "scheme": "b1"}], ["category", {"term": "html", "scheme": "b2"}], ["summary", {"type": "html"}], ["entry", {}], ["author", {}], ["name", {}], ["uri", {}], ["summary", {"type": "html"}]]

since feed tag start at first and end at last so the result should be like same with the entry tag

Code: Select all
[[["feed", {"text": "11", "r_version": "abc"}], ["entry", {}], ["author", {}], ["name", {}], ["uri", {}], ["title", {}], ["link", {"href": "aa", "type": "text/html", "rel": "alternate"}], ["id", {}], ["updated", {}], ["published", {}], ["category", {"term": "diveintopython", "scheme": "bb"}], ["category", {"term": "docbook", "scheme": "b1"}], ["category", {"term": "html", "scheme": "b2"}], ["summary", {"type": "html"}], ["entry", {}], ["author", {}], ["name", {}], ["uri", {}], ["summary", {"type": "html"}]] ]


I need help to achive this.

thanks
Last edited by stranac on Wed May 29, 2013 7:09 pm, edited 1 time in total.
Reason: Added code tags
bajaj
 
Posts: 2
Joined: Wed May 29, 2013 6:41 pm

Re: xml tags in a list

Postby MichelFJM » Thu May 30, 2013 2:12 pm

Hello
You should be more clear with what you want, give the correct output (the second given has only one [] more : it is probably not yet correct) and give a working python program.
MichelFJM
 
Posts: 19
Joined: Wed May 22, 2013 1:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], jshaun.obryan and 2 guests