Analyze XML with Python from URL using minidom

advertisements

I have a problem during parsing the following xml from a url.

sample XML in my url path:

<?xml version="1.0" encoding="utf-8"?>
<Documents>
    <class>
        <mid name="yyyyyyyyyyyyy"></mid>
        <person name="yyyyyyyyyy"></person>
        <url name="yyyyyyyyy"></url>
    </class>
    <class>
        <mid name="xxxxx"></mid>
        <person name="xxxxxxxxxx"></person>
        <url name="xxxxxxxxxxx"></url>
    </class>
</Documents>

Below is my python code;

def staff_list(request):

    url = http://path.to.url/
    dom = minidom.parse(urlopen(url))
    person = dom.getElementsByTagName('person')
    for i in person:
        print i.attributes['name'].value

within forloop I want to print the person and url tag values in xml that belongs to same parent class.

I tried the following method with iteration but get the "too many values to unpack" ERROR

def staff_list(request):

    url = http://path.to.url/
    dom = minidom.parse(urlopen(url))
    person = dom.getElementsByTagName('person')
    mid = dom.getElementsByTagName('mid')
    url = dom.getElementsByTagName('url')
    for i,j,k in person,mid,url:
        print i.attributes['name'].value,j.attributes['name'].value,k.attributes['name'].value

Any suggestions ?


You want to use zip() to combine the elements, I think:

for i,j,k in zip(person, mid, url):

Do yourself a big favour though and use the ElementTree API instead; that API is far pythononic and easier to use than the XML DOM API.