Reputation: 141
I'm having trouble parsing this XML. I'm fairly new to python so I may not be understanding a concept, or I may be missing a step.
The XML chunk:
<result created="2015-12-05T12:46:00-06:00" host="www.systemmonitor.us" status="OK">
<items>
<client>
<clientid>67300</clientid>
<name>
<![CDATA[ ACME Company ]]>
</name>
<site>
<siteid>85663</siteid>
<name>
<![CDATA[ Los Angeles ]]>
</name>
<workstations/>
<servers>
<server>
<id>597207</id>
<name>
<![CDATA[ SERVER1 ]]>
</name>
<offline>
<description>
<![CDATA[ OFFLINE - MAINTENANCE MODE ]]>
</description>
<startdate>2015-11-25</startdate>
<starttime>01:40:07</starttime>
</offline>
</server>
<server>
<id>2252213</id>
<name>
<![CDATA[ SERVER2 ]]>
</name>
<overdue>
<description>
<![CDATA[ Overdue ]]>
</description>
<startdate>2015-11-25</startdate>
<starttime>01:57:40</starttime>
</overdue>
</server>
</servers>
</site>
</client>
I need to extract certain elements so I can PUSH those elements into our CRM system.
Here is my code:
import requests
import xml.etree.ElementTree as ET
url = "https://www.systemmonitor.us/api/"
querystring = {"apikey":"SUPERSECRETAPIKEY","service":"list_failing_checks"}
response = requests.request("GET", url, params=querystring)
msg = response.text
tree = ET.ElementTree(ET.fromstring(msg))
client_ids = tree.find('clientid')
print client_ids
If I try to get the client_id from msg.find('clientid') it only returns an integer. 144. (I assume that is the number of times it is found in the xml.) If I use tree.find('clientid') I get []. I haven't tried to iterate over the data yet because I can't seem to even find it.
I think I've tried every other combination I can find and think of but I cannot get this resolved. find(), findall(), etc. My brain hurts from slamming it on the desk.
I would need to isolate clientid, name, overdue/description.
Can someone please explain what I have done incorrectly or what I am missing? Thank you.
Upvotes: 0
Views: 117
Reputation: 42778
You have to give the full path:
client_ids = tree.find('items/client/clientid')
or search all occurences with:
client_ids = tree.findall('.//clientid')
To get the contents of the elements use for example
for client_id in client_ids:
print client_id.text
Upvotes: 1