Python Forum
Load XML and iterate through tags
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load XML and iterate through tags
#1
Hi.

I'm trying to use Python (3.6.5) and lxml to iterate through an existing XML file.

I would like to take the XML file's opening tag names and create a folder based on the XML structure and tag name.

So, for example, if the XML file contained the following:

<FolderA>
   <FolderB>
   </FolderB>
   <FolderC>
      <FolderD>
      </FolderD>
   </FolderC>
   <FolderE>
   </FolderE>
</FolderA>
then I would like to create the following folders:

C:\FolderA
C:\FolderA\FolderB
C:\FolderA\FolderC
C:\FolderA\FolderC\FolderD
C:\FolderA\FolderE

The first thing I'm doing is loading the xml file and then parse through using 'interparse' :

tree = etree.xmlfile(xml)
context = etree.iterparse(tree)
When I run this, however, I'm getting a 'TypeError: expected str, bytes, or os.PathLike object, not lxml.etree.xmlfile'. Any suggestions on what I need to be doing differently to now iterate through the tags and create the folders?

Any suggestions would be appreciated.

Thanks.
Reply
#2
You are not showing enough code to properly analyze what the problem is.
For example, how is xml defined?
what is the real (verbatim) error you are receiving.
Post a runable code snippet, and complete error traceback.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through tags inside tags in Selenium/Python xpack24 1 5,694 Oct-23-2019, 10:15 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020