Zooma
Zooma

Reputation: 73

Converting random xml file to tree diagram

I am regularly running into cases where I need to convert XML files to tree diagram drawings. Could be a product catalog structure or a managed object model that I want to visualize.

Just wonder if someone can suggest a tool that takes XML (or some other text based input) and generates these kind of diagrams? I can of course convert my XML files with an awk (etc) script to pretty much any format, so it could be CSV input or pretty much anything text based.

Cheers! Sam

Upvotes: 0

Views: 3726

Answers (1)

tomc
tomc

Reputation: 1207

xmlstarlet el -u whatever.xml|awk -F'/' 'BEGIN{print "digraph{"}{print $(NF-1)" -> "$NF}END{print"}"}'|dot -Tpng -o xml.png

display xml.png 

image of xml structure

Hey!
it worked!

I hope you work with smaller xml files than I do.

xmlstarlet parses xml, The el option displays the xml elements as simple xpath
the -u flag eliminates redundant sibling elements
there are other many options and refinements that would
for example include values or attributes.
The awk prints out the dot header,
then for each line in the file,
prints the last two elements of each xpath with a arrow separator
which corresponds to an edge between nodes in the ... err, nigh-DAG
in the dot format.
Notice, that without special handling this results in a self edge on any root node.
awk finally prints out the closing brace as a footer.

dot,
which is part of the GraphViz library,
reads the format awk has transformed the xmlstarlet output into
and is told to write out a png image
which may be viewed with anything

note: both graphviz/dot and xmlstarlet have a tons of options

The xml file used maybe found here (23 KB)

Upvotes: 4

Related Questions