OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   RE: [xml-dev] how to save an n-ary tree of nodes to and from an xml file

[ Lists Home | Date Index | Thread Index ]

> I am trying to find how to implement something in XML
> and was disappointed and surprised to find not too
> many in-depth tutorials or information; unlike in Java
> where the Sun Java tutorial at java.sun.com is enough
> to get one working productively...
> [ok, rant over]
> 
> ---
> I am trying to represent a n-ary tree of nodes in xml.
> More accurately, I am trying to save/instantiate a
> tree of nodes to-from an xml file.
> Am finding it difficult trying to represent the
> parent-child relationships.
> 
> Java: (simplified)
> -----
> 
> class Node {
> Arraylist childNodes;
> Node parent;
> String data;
> }

The "parent" pointer here is redundant (you could reconstruct it from other
information). In my experience, when designing XML data structures it's best
to avoid such redundancy. All it achieves is to increase the burden on
senders to make the document consistent and on recipients to check that it
is indeed consistent.

Once you eliminate the redundancy, you seem to have a pure hierarchy, with a
very natural XML representation:

<node>
  <node>
    <data>x</data>
  </node>
  <node/>
  <node>
    <node/>
    <node>
      <data>x</data>
      <node/>
      <node/>
    </node>
  </node>
</node>

Of course, you could flatten this if you prefer so all the nodes are on the
same level, and the hierarchic relationship are represented by id/idref
attributes. But it seems more natural to use the XML hierarchy to represent
the information hierarchy when you can.

Michael Kay
http://www.saxonica.com/







 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS