Python - Element Tree find
python, xml
Solution
The XML elements are in the default namespace `urn:ebay:apis:eBLBaseComponents` So all tags get prepended with the full URI of the namespace.
Changing your `find()` parameter to include the URI should work.
import xml.etree.ElementTree as ET
XML = #Above Code in String
tree = ET.ElementTree(ET.fromstring(XML))
root = tree.getroot()
for Category in root[0]:
one = Category.find("{urn:ebay:apis:eBLBaseComponents}WantedParm1").text
two = Category.find("{urn:ebay:apis:eBLBaseComponents}WantedParm2").text
print(one, two)
You can find more information on dealing with namespaces on the element tree docs here or this stack overflow post.
Problem
I have an xml string that looks something like this: I am using the Element Tree library ``` <?xml version="1.0" encoding="UTF-8"?> <GetCategoriesResponse xmlns="urn:ebay:apis:eBLBaseComponents"><CategoryArray><Category><WantedParm1>true</WantedParm1><UnwantedParm1>true</UnwantedParm1><WantedParm2>20081</WantedParm2></Category></CategoryArray></GetCategoriesResponse> ``` I want to get some values of the Category Node, let's call them Wanted Parms 1 and 2. However I am getting an AttributeError probably because the code I wrote is not able to find the child node of the Category item. AttributeError: 'NoneType' object has no attribute 'text'. ``` import xml.etree.ElementTree as ET XML = #Above Code in String tree = ET.ElementTree(ET.fromstring(XML)) root = tree.getroot() for Category in root[0]: one = Category.find("WantedParm1").text two = Category.find("WantedParm2").text print(one, two) ```