Python Forum
How to display XML tree structure with Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to display XML tree structure with Python?
#1
Hi,

I have a large multi-level XML document of a complicated structure, without any namespace definition.
I would like to generate a simplified tree view of its structure, so that every possible element from the XML is shown and only once.

As a simplified example take this XML:

<data>
	<timestamp>...</timestamp>
	<people>
		<person>
			<name>...</name>
			<age>...</age>
		</person>
		<person>
			<name>...</name>
			<age>...</age>
			<degree />
		</person>
		<person>
			<name>...</name>
			<age>...</age>
			<degree />
			<siblings>
				<brother>...</brother>
				<brother>...</brother>
				<sister>...</sister>
			</siblings>			
		</person>
	</people>
	<cities>
		<city>
			<name>...</name>
			<country>...</country>
			<continent>...</continent>
			<capital />
		</city>
		<city>
			<name>...</name>
			<country>...</country>
			<continent>...</continent>
		</city>
	</cities>
</data>
Using Python I would like to generate a view of its structure, looking something like this:

-data-
	-timestamp-
	-people-
		-person-
			-name-
			-age-
			-degree-
			-siblings-
				-brother-
				-sister-
	-cities-
		-city-
			-name-
			-country-
			-continent-
			-capital-
So, basically I am not interested in the values, or how many elements of the same type are in the XML, etc.
I only want to see which elements are in there.

I know there might be visual tools to achieve this, but I need to be able to generate such tree view also directly inside python script.

Thanks for any ideas.
Reply


Messages In This Thread
How to display XML tree structure with Python? - by sonicblind - Nov-10-2017, 03:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Web app structure with python as backend alt19 1 2,025 Oct-06-2020, 11:28 PM
Last Post: scidam
  Cant set api response as tree in Element tree hey_arnold 4 3,807 Mar-04-2019, 03:25 PM
Last Post: dsarin

Forum Jump:

User Panel Messages

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