My XML file is below. xml.etree.ElementTree doesn't find the root node, or any of the child backgroundJob nodes. It works when I remove the <?xml version= ...> line at the top. Any ideas how I can get it to recognize the root node and the structure of the XML, so I can pull the backgroundJob child nodes from the XML?
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-3.11.xsd">
<pagination pageNumber="1" pageSize="500" totalAvailable="21"/>
<backgroundJobs>
<backgroundJob id="5b36f868-4a59-4f59-a777-fec1640ada90" status="Failed" createdAt="2022-07-25T11:04:51Z" startedAt="2022-07-25T11:04:51Z" endedAt="2022-07-25T11:04:51Z"/>
<backgroundJob id="9f2dd4fd-49fe-4c3c-beed-aa7ee3fbe75c" status="Failed" createdAt="2022-07-25T11:04:51Z" startedAt="2022-07-25T11:04:51Z" endedAt="2022-07-25T11:04:51Z"/>
<backgroundJob id="c18f9fe5-5995-4071-9fbc-113dd719d578" status="Failed" createdAt="2022-07-25T11:04:51Z" startedAt="2022-07-25T11:04:51Z" endedAt="2022-07-25T11:04:51Z"/>
<backgroundJob id="d3f6a06d-ecec-4b72-8e66-513ae2b4656a" status="Failed" createdAt="2022-07-25T11:04:51Z" startedAt="2022-07-25T11:04:51Z" endedAt="2022-07-25T11:04:51Z"/>
<backgroundJob id="a55bac30-20b6-4230-ab65-f1dfa6ccca2f" status="Failed" createdAt="2022-07-25T12:00:23Z" startedAt="2022-07-25T12:00:23Z" endedAt="2022-07-25T12:00:23Z"/>
<backgroundJob id="d238b16c-df5a-497c-8e00-e956f9f36e35" status="Failed" createdAt="2022-07-25T12:00:23Z" startedAt="2022-07-25T12:00:23Z" endedAt="2022-07-25T12:00:23Z"/>
<backgroundJob id="8d395f85-367f-4d51-b76d-3a5e4b104eb0" status="Failed" createdAt="2022-07-25T12:00:23Z" startedAt="2022-07-25T12:00:23Z" endedAt="2022-07-25T12:00:23Z"/>
<backgroundJob id="de3d5546-5a17-4521-861d-b981c87e1065" status="Failed" createdAt="2022-07-25T12:00:24Z" startedAt="2022-07-25T12:00:24Z" endedAt="2022-07-25T12:00:24Z"/>
<backgroundJob id="6e25d930-5951-4e4f-9f36-aeb84c00af8a" status="Failed" createdAt="2022-07-25T12:00:24Z" startedAt="2022-07-25T12:00:24Z" endedAt="2022-07-25T12:00:24Z"/>
<backgroundJob id="06616f04-de9c-4530-8e77-8aa99b497865" status="Failed" createdAt="2022-07-25T14:00:53Z" startedAt="2022-07-25T14:00:53Z" endedAt="2022-07-25T14:00:53Z"/>
<backgroundJob id="cefb4adf-cd80-4f4b-9bdb-23a1431616d6" status="Failed" createdAt="2022-07-25T14:00:53Z" startedAt="2022-07-25T14:00:53Z" endedAt="2022-07-25T14:00:53Z"/>
<backgroundJob id="e01ac66d-41cf-453d-8c0e-c509abc9e96e" status="Failed" createdAt="2022-07-25T14:00:53Z" startedAt="2022-07-25T14:00:53Z" endedAt="2022-07-25T14:00:53Z"/>
<backgroundJob id="79e9f0c6-aa9d-44d7-af1f-6ee84fb2a500" status="Failed" createdAt="2022-07-25T14:00:53Z" startedAt="2022-07-25T14:00:53Z" endedAt="2022-07-25T14:00:53Z"/>
<backgroundJob id="db6549ca-3392-4559-8698-ccaa7d4a5df9" status="Failed" createdAt="2022-07-25T14:00:54Z" startedAt="2022-07-25T14:00:54Z" endedAt="2022-07-25T14:00:54Z"/>
<backgroundJob id="a36ee3bc-6917-4008-b0b3-a11cdcdab2d1" status="Failed" createdAt="2022-07-25T14:00:54Z" startedAt="2022-07-25T14:00:54Z" endedAt="2022-07-25T14:00:54Z"/>
<backgroundJob id="4e9e571d-aeb4-4802-87d3-c4c7122627d4" status="Failed" createdAt="2022-07-25T14:00:54Z" startedAt="2022-07-25T14:00:54Z" endedAt="2022-07-25T14:00:54Z"/>
<backgroundJob id="a5a99d05-118a-40ee-bbba-f8431ddf54b4" status="Failed" createdAt="2022-07-25T14:00:54Z" startedAt="2022-07-25T14:00:54Z" endedAt="2022-07-25T14:00:54Z"/>
<backgroundJob id="69f18c45-9fdd-422d-bc7e-ccbce0eab313" status="Failed" createdAt="2022-07-25T14:00:54Z" startedAt="2022-07-25T14:00:54Z" endedAt="2022-07-25T14:00:54Z"/>
<backgroundJob id="61eed63c-9442-4f0f-ac0a-0169fa09aae9" status="Failed" createdAt="2022-07-25T14:00:54Z" startedAt="2022-07-25T14:00:54Z" endedAt="2022-07-25T14:00:54Z"/>
<backgroundJob id="57c0c0b9-f661-41da-af92-dba0f8a5f912" status="Failed" createdAt="2022-07-25T14:00:54Z" startedAt="2022-07-25T14:00:54Z" endedAt="2022-07-25T14:00:54Z"/>
<backgroundJob id="5e59eb69-4338-4bdd-9544-b9a248549deb" status="Failed" createdAt="2022-07-25T14:00:54Z" startedAt="2022-07-25T14:00:54Z" endedAt="2022-07-25T14:00:54Z"/>
</backgroundJobs>
</tsResponse>
Here's my code thus far to fetch each child backgroundJob's id attribute -
import xml.etree.ElementTree as et
tree = et.parse("C:\Stuff\subs.xml")
root = tree.getroot()
for n in tree.findall('.//backgroundJob'):
print(n.attrib['id'])
The output -
<Element '{http://tableau.com/api}tsResponse' at 0x0000016A04251580>
The code does work when I remove the leading <?xml version=... >
line, as well as all attributes of the tsResponse
root node. That's not an option for me, however, since the XML is provided in the format above. What am I missing?
CodePudding user response:
Does this help? This works for me.
import xml.etree.ElementTree as et
tree = et.parse("C:\Stuff\subs.xml")
root = tree.getroot()
for n in root.find("{http://tableau.com/api}backgroundJobs"):
print(n.attrib['id'])
Also, I'm not sure but I think xmlns="http://tableau.com/api"
may not be correct XML syntax