Python Forum

Full Version: Load XML and iterate through tags
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.