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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • Beyond n8n for Workflow Automation: Agent Graphs as Your Universal Agent Harness
  • The Hidden Engineering Cost of XML in Enterprise Development Workflows
  • Building Scalable Agentic Assistants: A Graph-Based Approach

Trending

  • Good Data, Bad Metric: A Mutation Testing Pattern for Analytics Engineering
  • Liquid Glass, Material 3, and a Lot of Plumbing
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Is the Data Warehouse Dead? 3 Patterns From Enterprise Architecture That Answer This Question
  1. DZone
  2. Data Engineering
  3. Databases
  4. Visualizing XML as a Graph Using Neo4j

Visualizing XML as a Graph Using Neo4j

By 
Nikhil Kuriakose user avatar
Nikhil Kuriakose
·
May. 29, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
25.8K 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.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • Beyond n8n for Workflow Automation: Agent Graphs as Your Universal Agent Harness
  • The Hidden Engineering Cost of XML in Enterprise Development Workflows
  • Building Scalable Agentic Assistants: A Graph-Based Approach

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook