DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies
  • Tech Hiring: Trends, Predictions, and Strategies for Success
  • Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]

Trending

  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies
  • Tech Hiring: Trends, Predictions, and Strategies for Success
  • Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]
  1. DZone
  2. Data Engineering
  3. Databases
  4. Visualizing XML as a Graph Using Neo4j

Visualizing XML as a Graph Using Neo4j

Nikhil Kuriakose user avatar by
Nikhil Kuriakose
·
May. 29, 14 · Interview
Like (0)
Save
Tweet
Share
22.46K Views

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

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

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

Graph (Unix) XML Neo4j

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

Opinions expressed by DZone contributors are their own.

Trending

  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies
  • Tech Hiring: Trends, Predictions, and Strategies for Success
  • Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: