Question

Various Way To Store hierarchical Data

Asked by: MickaelE

I have a website where we are currently using an XML file to store our menu content. The problem we are experiencing is that, with multiple users simultaneously accessing the file for updates, the file locking isn't working as expected. As a result data is either being over-written or written to the wrong parts of the XML file.
What are some alternatives for storing a hierarchical menu?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-01-16 at 13:31:41ID24059491
Tags

XML Alternatives

,

Data Storage

,

Locking Data

Topics

Web Development

,

MS SQL Server

,

Theory

Participating Experts
4
Points
250
Comments
18

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Hierarchical Trees
    I want to design a financial package around the chart of account of course; being the master table of such an application. My client has a special requirement: he wants his chart of account to be set up in a "Hierarchical Tree" fashion. Does anybody out there have...
  2. Hierarchical recordset objects cannot be persisted XML
    Hi all. I have an access adp that updates a hierarchical recordset I need to persist this recordset as xml I get an ado error when I try to save the recordet as xml Error: 'Hierarchical recordset objects cannot be persisted XML Looking for a way around this error, any help ...
  3. Reading Hierarchical XML in ASP
    This is my first XML project, and I am looking into using XML to recieve orders from external clients using ASP(VBScript). I have created some sample data which is Hierarchical, where each XML file can contain 1 or more Orders, each of which can have one or many Order Items....
  4. How to create an ERD from a hierarchical db
    I process data from hierarchical dbs, via flat files. I would like to draw up an ERD for the hierarchical db - is this (always) possible, or are there some aspects of hierarchical dbs which cannot be represented? Can you recommend a good link that explains hierarchical dbs an...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: BrandonGalderisiPosted on 2009-01-16 at 13:33:54ID: 23397647

Store them in SQL server.  If you post a sample of your XML, I can show you how the table should be structured.

 

by: remmuhPosted on 2009-01-16 at 14:09:23ID: 23397882

Yes, a database is the best way to store this type of hierarchical data. In fact, you can store an infinite amoutn of levels using just a single table.

You simply have the MenuItem table reference itself.

MenuItem
-----------------
id   int
itemName    varchar
parent     int

That parent column can be null (top-level category) or it can be a number referencing another item in the table, which would make it a sub-category of that item. If it doesn't have any children, it is expected to be a leaf or end-node instead of a branch.

And as @BrandonGalderisi stated, SQL Server would be a good db to use.

 

by: dqmqPosted on 2009-01-16 at 14:30:18ID: 23398079

>In fact, you can store an infinite amoutn of levels using just a single table.

That's an understatement.  You can store an infinite (well, a lot, anyway) amount of levels of an infinite amount of hierarchies using just a single table.  Not that I would recommend it.

While storing hierarchies in SQL databases is a breeze, retrieval is not always so easy.  Google the topic to find three or four techniques, each lending itself to different retrieval needs.

 

by: remmuhPosted on 2009-01-16 at 14:41:01ID: 23398181

Yes, @dqmq is exactly right. Retrieval is a little bit more time-consuming. It is best to build a view that captures your physical levels so it can return your data quicker.

 

by: BrandonGalderisiPosted on 2009-01-16 at 14:56:16ID: 23398310

Building a view won't help.  It's all in how you structure it and that's dependent upon how you intend to use it.

 

by: acperkinsPosted on 2009-01-18 at 09:39:36ID: 23405655

You can use the HIERARCHYID data type in SQL Server 2008.  If you are still using SQL Server 2000 or 2005 use Xml for this purpose.

 

by: MickaelEPosted on 2009-01-22 at 05:58:43ID: 23439285

How would I go about using XML in 2005? I believe that the retrieval process is the most difficult part that I'm facing right now. I need to get the data back into the XML format that I currently have in my flat file so that my SKM menu .NET control can display it.

 

by: dqmqPosted on 2009-01-22 at 09:40:02ID: 23441606

For a menu structure, I would store it in relational form. Then you can use the FOR XML clause to convert it to an XML format.  With 2005 you can also use CTE's to expand the hierarchy to all of it's levels without procedural code.

I don't know your requirements, but I'd probably play safe and opt for a structure something that allows a menuitem to appear on more than one menu.  Something like this:

MenuItem
   MenuItemID (PK)
   Description

MenuItemUse
   MenuItemID (PK, FK1-->references menuitem)
   UsedOnID (PK, FK2-->references menuitem parent)




 

by: acperkinsPosted on 2009-01-22 at 16:36:44ID: 23445393

>>I need to get the data back into the XML format that I currently have in my flat file so that my SKM menu .NET control can display it.<<
Then save the entire menu in one Xml column/row.

 

by: BrandonGalderisiPosted on 2009-01-23 at 05:26:51ID: 23448660

Won't there be contention with multiple applications writing to a large XML field.  

 

by: MickaelEPosted on 2009-01-23 at 06:54:53ID: 23449396

I really like the CTE solution but the question that comes to mind now is, how do I manage sorting between all the menu items? Do I have to use table locking to keep sorting consistent between simultaneous user insertions and updates? I can see CTE working for the retrieval of the menu but inserting a rank or priority value and keeping the integrity of that value is hard for me to picture.

For instance, if I BrandonGalderisi moves a menu item from the first position to the third position while I am inserting a menu item at the first position, where does Brandon's menu item actually land?

 

by: BrandonGalderisiPosted on 2009-01-23 at 07:30:06ID: 23449731

Well the problem is simpler than that while at the same time being more difficult.

There is no such thing as "two people writing at the same time" in SQL Server.  I'll follow up to that.  You both cannot write the SAME record at the SAME time.  So you could insert a record to position 1 at the same time someone else is updating a record to position 1.  Who wins?  Whoever updates last.  But what really needs to happen is that when you are updating you should account that the data you had may not be the most current.  So if you say "move item 1 from position 3 to position 2" and item 1 is no longer in position 3, then your data is outdated and you should refresh it before attempting to make any changes.

 

by: dqmqPosted on 2009-01-23 at 11:09:00ID: 23451951

Yes, you need to locks to prevent concurrent users from stepping on each other's toes.  While it is true that two users won't update the same record at the same time, life is not that simple when you're reordering multiple records and/or you don't want concurrent access to the records while the update is "in the works".

Locks do cause contention, but it's "good" contention because it protects your the integrity of your data.  With page level or row level locking, the inconvenience will be nowhere near what you experienced when locking the entire hierarchy at once or by putting the entire hierarchy XML.

Check out Optimistic and Pessimistic locking strategies on Google for two different approaches.  With optimistic, users are allowed to proceed through an update sequence only to be warned at the end that they have created a conflict.  It's a frustrating user interface, but the advantage is that locks are short term so conflicts may not occur that often.

With pessimistic, users are prevented from changing records that others are working on.  Much more friendly, but requires locks to be held for much longer periods of time.

Also, check out the BOL discussion about transactions.  You will want to use transactions to tie together updates that involve multiple records.  

Also, check out the BOL discussion on transaction isolation level.  Various options give you control over the degree of locking.





 



 

 

by: acperkinsPosted on 2009-01-23 at 15:35:01ID: 23454093

That is why writing the whole Xml to one record is always going to be a better approach (there is no locking involved).  Unfortunately, until you take the time to test it out, you will not see it for yourself.

Good luck.

 

by: dqmqPosted on 2009-01-23 at 21:25:17ID: 23455228

>That is why writing the whole Xml to one record is always going to be a better approach (there is no locking involved).

I beg to disagree.  Without a locking strategy, you will have a massive epidemic of lost updates and obsolete reads.  With a locking strategy, no two users will be allowed to work concurrently because you have to lock the the hierarchy instead of just the records being changed.



 

 

by: acperkinsPosted on 2009-01-24 at 08:45:15ID: 23457049

>>I have a website where we are currently using an XML file to store our menu content.<<
Your solution is quite simple:

1. Add an Xml column to your table.
2. Add the the contents of that file to the column.
3. Change where you are writing out the Xml to a file to instead update the single (this is the important part) value in the table.  Again no locking involved or needed.  The value will be updated in its entirety.

Test it out and you will see this is a vast improvement over handling it as a file without the need for a complicated hierarchical structure.  Above all else, you will be using something that you are familiar with.

Again, good luck.

 

by: dqmqPosted on 2009-01-25 at 13:22:48ID: 23462752

>Your solution is quite simple:

If AC is correct, your solution is even simpler:

1. Change where you are writing out the xml to a file to instead update the single (this is the important part) value in the file.

In other words, with that solution you gain nothing relevant to the discussion by moving the hierarchy to a database.

>Again no locking involved or needed.

I cannot let that statement pass.  Even in the simple, one-record scheme, SQL Server uses locks to prevent data corruption.  It's a complex topic and I want to avoid a 20-page tutorial about it, but it's way misleading to claim there is NO locking involved or needed.  Locks may be implicit and short-lived, but they are used none-the-less.

 


 

by: dqmqPosted on 2009-01-25 at 14:13:32ID: 23462911

Perhaps we need to distinquish "locking" from "blocking".  I do agree that the one-record scheme can be configured so that the locks have little-or-no blocking effect.  In other words, even though SQL server may implicitly issue locks behind the scenes, they may not block other users,   We DO have locking.  We DO NOT necessarily have blocking.

But, the point I want to make is that blocking is a good thing because it prevents users from stepping on one-another's toes (or worse).  If you don't have blocking (aka "concurrency control"),  then integrity problems arise.  The challenge is to implement blocking at the right level so that contention is not a problem.  

In your file-based implementation, you block the entire hierachy at once, which causes too much contention.  In the single record database equivalent, you would have:
   Blocking Read (of entire hierarchy)
   Make changes
   Update
That's the same amount of contention you had before...Oops.

But suppose you relax the blocking like this:
   NonBlocking Read
   Make changes
   Update

While you've indeed eliminated the contention, you've also lost the integrity safety net that blocking provides.  (And, btw, you can do this just as well with your file-based implementation).

But suppose, you limit changes to a single "value" in the hierarchy.  Then, you risk lost updates.   By that I mean:
   User A Reads XML (non blocking)
   User B Reads same XML (non blocking)
   User A changes one value
   User B changes another value (wipes out the User A change)

Furthermore, your business requirements call for things like reordering menu items which calls for changing more then one value at the same time. When you introduce that possibility, you not only risk lost updates, you risk data inconsistencies.

In summary, effective concurrency control depends on some form of blocking users that are dabbling in the same area of the hierarchy at the same time.  Locks are a good feature that are intended to help you implement the appropriate level of blocking.  Block too high, you get too much contention; Block too low, you get too much overhead.  Using a single XML structure limits your locking fexibility and forces you to accomodate for the deficiences in code.  Using a relational structure gives you the flexibility to lock at varying degrees of granularity using the mechansims designed into the database just for that purpose.

--dQmQ
 





 


 



 










For example, suppose user A and user B both discover two menu items are different







When two users are working on separate parts of the hierarchy,



But let's forget the locking details for the time being and consider the implications of that.  With two, three, four, or more users having the ability to work on the hierachy concurently, must we still not manage their work such that they don't step on each other's toes?  Yes, yes, yes...yes we do.   You want blocking...that's what keeps your hierarchy intact.    

Consider this example:

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...