Search code examples
pythonxmlbeautifulsoupfogbugzfogbugz-api

How to parse an XML file with multiple non-specific elements


I'm trying to parse a list of cases that is returned from the Fogbugz API.

Current code:

from fogbugz import FogBugz
from datetime import datetime, timedelta
import fbSettings

fb = FogBugz(fbSettings.URL, fbSettings.TOKEN)

resp = fb.search(q='project:"Python Testing"',cols='ixBug')

print resp.cases.case.ixbug.string

The problem is that the XML has multiple cases returned simply as case. For instance my test search back the following:

<response>
<cases count="3">
 <case ixbug="133529" operations="edit,assign,resolve,email,remind">
  <ixbug>
   133529
  </ixbug>
 </case>
 <case ixbug="133528" operations="edit,assign,resolve,email,remind">
  <ixbug>
   133528
  </ixbug>
 </case>
 <case ixbug="133527" operations="edit,assign,resolve,email,remind">
  <ixbug>
   133527
  </ixbug>
 </case>
</cases>
</response>

But

print resp.cases.case.ixbug.string

only returns the first case number in the XML, ignoring the other two. How can I modify my print statement to return all three case numbers?


Solution

  • From the docs:

    from fogbugz import FogBugz
    import fbSettings
    
    fb = FogBugz(fbSettings.URL, fbSettings.TOKEN)
    
    resp = fb.search(q='project:"Python Testing"',cols='ixBug')
    
    # printing
    for case in resp.cases.findAll('case'):
        print case.ixbug.string
    
    # store in a list
    l = [case.ixbug.string for case in resp.cases.findAll('case')]