1

I'm trying export and xml file with nested elements that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<GetCategoriesResponse xmlns="urn:ebay:apis:eBLBaseComponents">
    <Timestamp>2015-11-16T03:51:35.809Z</Timestamp>
    <Ack>Success</Ack>
    <Version>927</Version>
    <Build>E927_CORE_API_17590338_R1</Build>
    <CategoryArray>
            <Category>
            <BestOfferEnabled>true</BestOfferEnabled>
            <AutoPayEnabled>true</AutoPayEnabled>
            <CategoryID>20081</CategoryID>
            <CategoryLevel>1</CategoryLevel>
            <CategoryName>Antiques</CategoryName>
            <CategoryParentID>20081</CategoryParentID>
            </Category>
    </CategoryArray>
    <CategoryCount>18282</CategoryCount>
    <UpdateTime>2015-09-01T22:57:09.000Z</UpdateTime>
    <CategoryVersion>113</CategoryVersion>
    <ReservePriceAllowed>true</ReservePriceAllowed>
    <MinimumReservePrice>0.0</MinimumReservePrice>
</GetCategoriesResponse>

I'm trying to acces the contents of the Tag with python element Tree I've tried different approaches such as:

import xml.etree.ElementTree as ET

xtree = ET.parse("categories.xml")
xroot = xtree.getroot()

for node in xroot.findall('CategoryArray'):
    for snode in node.findall('Category'):
            categoryName = snode.find("CategoryName").text
            categoryLevel = snode.find("CategoryLevel").text
            categoryParentID = snode.find('CategoryParentID').text
            
            print(categoryName, categoryLevel, categoryParentID)

but it does not output anything, what am I doing wrong?

1 Answer 1

3

Namespaces(ns) is key here, so you would need to factor in the ns. for more documentation pls refer.

import xml.etree.ElementTree as ET

xtree = ET.parse("categories.xml")
xroot = xtree.getroot()

ns = {'myns': 'urn:ebay:apis:eBLBaseComponents'} # define the namesapce

for node in xroot.findall('myns:CategoryArray',ns): # and then find them based on ns
    for snode in node.findall('myns:Category',ns):
            categoryName = snode.find("myns:CategoryName",ns).text
            categoryLevel = snode.find("myns:CategoryLevel",ns).text
            categoryParentID = snode.find('myns:CategoryParentID',ns).text
            
            print(categoryName, categoryLevel, categoryParentID)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.