Visualizing XML as a Graph Using Neo4j
Join the DZone community and get the full member experience.
Join For Free
neo4j
is quite awesome for pretty much
everything
. here is an extract i found from
this
blog
whenever someone gives you a problem, think graphs . they are the most fundamental and flexible way of representing any kind of a relationship, so it’s about a 50-50 shot that any interesting design problem has a graph involved in it. make absolutely sure you can’t think of a way to solve it using graphs before moving on to other solution types.
as a baby step, let us try to visualize an xml as a graph.
we can start with a simple xml here,
<?xmlversion="1.0"encoding="utf-8"?> <breakfast_menu> <food> <nameattr="one">belgian waffles</name> <price>$5.95</price> <description>two of our famous belgian waffles with plenty of real maple syrup</description> <calories>650</calories> </food> </breakfast_menu>
it is a simple ‘breakfast menu’ with just one item, ‘ belgian waffles ‘.
neo4j has a neat interface which lets us visualize the graph we created. the graph for the above xml looks slick..
the above xml has 6 tags and we have 6 nodes in our graph. the nested tags/nodes has a ‘child-of’ relationship with the parent tag/node.
in the graph above,
- node numbered 0 is the root node – corresponds to the <breakfast_menu> tag
- node numbered 1 is the immediate child of the root – corresponds to the <food> tag
- nodes numbered from 2-4 are the tags which come under <food> tag, viz <name>, <price>, <description> and <calories>
transforming xml..
let us parse the xml into a data structure which can be easily persisted using neo4j . personally, i would prefer sax parser as there are serious memory constraints when creating a dom object (really painful if you talking big data as xml). additionally sax parsing gives you unlimited freedom to do whatever you want to do with the xml.
this is how i represent a node, using the object xmlelement . each xmlelement is identified by an id. this is basically an integer or long. only thing to make sure is that the number has to be unique across all xmlelements.
publicclassxmlelement { privatestring tagname; privatestring tagvalue; privatemap<string, string> attributes = newhashmap<string,string>(); privatehierarchyidentifier hierarchyidentifier; privateintparentid; privatebooleanpersisted; //setters and getters for the members publicstring getatrributestring(){ objectmapper jsonmapper = newobjectmapper(); try{ returnjsonmapper.writevalueasstring(this.attributes); } catch(jsonprocessingexception e) { logger.severe(e.getmessage()); e.printstacktrace(); } returnnull; } }
the xml tag name and value are stored as string and the attributes are stored as a map. also, there is another member object, hierarchyidentifier
publicclasshierarchyidentifier { privateintdepth; privateintwidth; privateintid; //getters and setters for member element }
the hierarchyidentifier class contains the id class which is used to identify an xmlelement , which translates to identifying an xml tag. each xmlelement has the id of it’s parent stored.
and after the parse, the xml should be represented as a map of <integer, xmlelement > and each xmlelement identified by the corresponding integer id. you can see the sax parser here ..
so we pass the map to the graphwriter object.
we create a node with the following properties
- value – the value of the xml tag
- id – the id of the current node
- parent – the id of the parent tag
- attributes – the string representation of the tag attributes
also, the node has the following labels
- node – for all nodes
- parent – for the seed ( highest parent ) node
- xml tag name – say, the node for the tag <food> will have label food
currently, there is only a single type of relationship, ‘child_of’ , between immediate nested tags.
see the below the example for a bigger xml
<breakfast_menu> <food> <nameattr="one">belgian waffles</name> <price>$5.95</price> <description>two of our famous belgian waffles with plenty of real maple syrup</description> <calories>650</calories> </food> <food> <nameattr="two">masala dosa</name> <price>$10.95</price> <description>south india's famous slim pancake with mashed potatoes</description> <calories>650</calories> <eaten> <namefirst="nikhil"/> <age>25</age> </eaten> </food> </breakfast_menu>
i have added a second item to the ‘breakfast_menu’.. the legendary masala dosa .
also, the second food item has a new child tag, <eaten>. and the resultant graph looks prettier
the sets of tags,relationships and properties can be seen in the neo4j local server interface.
the entire codebase can be found here in github ..
visualizing as a graphgist
an easy representation can be done in graphgist. i have created the graph of this xml.
here is the link to the gist . also, the gist script can be found in the github gist here ...
Published at DZone with permission of Nikhil Kuriakose. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments