0

I have XML file and I want to parse only tags, but I need to preserve hierarchy and order of that tags. I use xml.etree.ElementTree to do that but I extract the only list of tags.

My XML looks like:

<Collection variable="value">
    <Genre variable="value">
        <Timestamp>2017-05-15T18:14:07-05:00</Timestamp>
        <Date>2016-12-31</Date>
        <Identifier>
          <id>123456789</id>
          <Name>
            <BusinessName>AB & co</BusinessName>
          </Name>
        </Identifier>
    </Genre>
</Collection>

and desired output should be list of tags with their parent tags

['Collection/Genre',
 'Collection/Genre/Timestamp',
 'Collection/Genre/Date',
 'Collection/Genre/Identifier/id',
 'Collection/Genre/Identifier/Name/BusinessName']

any help would be appreciate.

1

1 Answer 1

1

Expanding on @mzjn's comment, you can use the lxml package to extract the paths from the ElementTree. Also, as a side note, the ampersand is a reserved character in XML.

from lxml import etree


x = '''<Collection variable="value">
    <Genre variable="value">
        <Timestamp>2017-05-15T18:14:07-05:00</Timestamp>
        <Date>2016-12-31</Date>
        <Identifier>
          <id>123456789</id>
          <Name>
            <BusinessName>AB and co</BusinessName>
          </Name>
        </Identifier>
    </Genre>
</Collection>'''

xml = etree.fromstring(x)
tree = xml.getroottree()
paths = [tree.getpath(d) for d in xml.iterdescendants()]

paths
# returns:
['/Collection/Genre',
 '/Collection/Genre/Timestamp',
 '/Collection/Genre/Date',
 '/Collection/Genre/Identifier',
 '/Collection/Genre/Identifier/id',
 '/Collection/Genre/Identifier/Name',
 '/Collection/Genre/Identifier/Name/BusinessName']
Sign up to request clarification or add additional context in comments.

1 Comment

With little modifications, works perfectly! Thanks to @James

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.