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,
<?xml version="1.0" encoding="UTF-8"?> <breakfast_menu> <food> <name attr="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.
public class XmlElement {
private String tagName;
private String tagValue;
private Map<String, String> attributes = new HashMap<String,String>();
private HierarchyIdentifier hierarchyIdentifier;
private int parentId;
private boolean persisted;
//Setters and Getters for the members
public String getAtrributeString(){
ObjectMapper jsonMapper = new ObjectMapper();
try {
return jsonMapper.writeValueAsString(this.attributes);
} catch (JsonProcessingException e) {
LOGGER.severe(e.getMessage());
e.printStackTrace();
}
return null;
}
}
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
public class HierarchyIdentifier { private int depth; private int width; private int id; //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> <name attr="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> <name attr="two">Masala Dosa</name> <price>$10.95</price> <description>South India's famous slim pancake with mashed potatoes</description> <calories>650</calories> <eaten> <name first="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..
Good post. I’m a bit confused between your xml element code and your graph representation. Perhaps best to use a graph model picture of your xml that uses the tag names as visible titles.
Also this post would be a great graphgist. See gist.neo4j.org
Ahhh… That’s a good idea.. I will make the update with a graph model picture and graphgist.. And the feedback is much appreciated..
Micharl… I have added a GraphGist section to the blog. Please note that, I have added an extra attribute in the gist, TAG, to denote the tag name. This is quite optional.
The better way is the have the tag as a Label. This can also be seen in the gist. Hope everything is clear now..
Thanks for the graph gist, really cool. Could you try a version where actually the element names are the names of the relationships? I think that would be most expressive and really cool for navigation along the structure. Imagine using cypher to query XML instead of XPATH 🙂
That sounds interesting.. Guess I will try it out.. 🙂
Thanks for the tip Michael.. I really did not like the generic ‘CHILD_OF’ relationship anyway.. pushed a really raw code with the change to the GitHub repo. Planing to come up with a documentation to use Cypher as XPATH.. 🙂