Python print xml tree structure
Hi, Show I have a large multi-level XML document of a complicated structure, without any namespace definition. As a simplified example take this XML:
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 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. Posts: 11,306 Threads: 429 Joined: Sep 2016 Reputation: 442 lxml has etree (prettyprint option) see: http://lxml.de/api.html Posts: 13 Threads: 6 Joined: Oct 2017 Reputation: 0 prettyprint does not help me as it shows everything as is in the XML. That's exactly what I want to avoid. I need no duplicates and no values or attributes. Only the very basic tree structure. Posts: 11,306 Threads: 429 Joined: Sep 2016 Reputation: 442 You can look here to see what's available as packages: https://pypi.python.org/pypi?%3Aaction=s...mit=search
Posts: 6,378 Threads: 115 Joined: Sep 2016 Reputation: 481 Just getting name of tags work fine in both lxml and BeautifulSoup. Example getting tag names: from lxml import etree from bs4 import BeautifulSoup xml = '''\
wavic Posts: 2,908 Threads: 46 Joined: Sep 2016 Reputation: 89 Pretty straight away: from lxml import etree from collections import Counter xml = '''\
wavic Posts: 2,908 Threads: 46 Joined: Sep 2016 Reputation: 89 I have missed to put Posts: 13 Threads: 6 Joined: Oct 2017 Reputation: 0 Thanks to all of you for the tips! wavic - My aim was to have no duplicates, so your code was almost perfect, but I reworked it a bit also to include the attributes. Here is my final code in case it helps somebody else as well. import re, collections from lxml import etree xml = '''\ Which gives me following result:
wavic Posts: 2,908 Threads: 46 Joined: Sep 2016 Reputation: 89 Good! At first, I was thinking that this will be a difficult task but it seems that xpath is of great help. Posts: 3 Threads: 0 Joined: Aug 2020 Reputation: 0 Aug-12-2020, 08:51 AM (This post was last modified: Aug-12-2020, 08:51 AM by mreshko.) Hi sonicblind. Great code! Very useful. Thank you. It would be great if you could add these two feature to the code: (1) show the child's' number after the level, e.g. (2) show the number of identical siblings, for
example, if there were, say, 100 "person" elements, it would Many thanks How do you visualize an XML tree?Here are some options for viewing your XML in a tree structure: Open the XML in a web browser and get an outline view with collapsible elements. Open the XML in graphics view in Oxygen, QTAssistant, or XMLSpy. Use Graphviz or DotML ant build to create your own visual representations.
How do I print XML data in Python?You have a few options.. xml. etree. ElementTree. indent(). BeautifulSoup. prettify(). lxml. etree. parse(). xml. dom. minidom. parse(). How do I print a pretty XML string in Python?Use lxml. etree.
parse(source) to parse the XML file source and return an ElementTree object. Call lxml. etree. tostring(element_or_tree, encoding="unicode" pretty_print=True) to pretty print the contents of the XML file, with element_or_tree as the result of the previous step.
How do I parse XML in ElementTree?To parse XML file. ElementTree() This function is overloaded to read the hierarchical structure of elements to a tree objects. ... . getroot() This function returns root element of the tree root = tree.getroot(). getchildren() This function returns the list of sub-elements one level below of an element.. |