Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Visualizing XML as a Graph Using Neo4j

DZone's Guide to

Visualizing XML as a Graph Using Neo4j

· Java Zone
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

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..

small-graph-xml

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 usingNeo4j. 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 XmlElementis 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 anXmlElement, 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

xml-med

The sets of tags,relationships and properties can be seen in the Neo4j local server interface.

xml-med-tags

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...

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Published at DZone with permission of Nikhil Kuriakose. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}