SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Post Preferred Method - Parent Referenced by Children vs Parent Containing Children

    Hey guys. I'm just wondering about the preferred method in the following situation.

    Say I have a set of categories, with indefinite sub-category levels, for example:
    Code:
    Main Category
        - SubCategory 1
            - SubCategory 1.1
                - SubCategory 1.1.1
                - SubCategory 1.1.2
            - SubCategory 1.2
                - SubCategory 1.2.1
            - SubCategory 1.3
        - SubCategory 2
            - SubCategory 2.1
                - SubCategory 2.1.1
            - SubCategory 2.2
        - SubCategory 3
            - SubCategory 3.1
            - SubCategory 3.2
    Each category has a parent and a name, so in a standard database I would have three columns: ID, Name, Parent

    However, I know XML is used differently, for example the ability to place an element inside a parent element.

    So, which of the following methods would be preferred?
    Code XML:
    <Categories>
        <Category Name="Main Category" ID="1" Parent="-1" />
        <Category Name="SubCategory 1" ID="2" Parent="1" />
        <Category Name="SubCategory 1.1" ID="3" Parent="2" />
        <Category Name="SubCategory 1.1.1" ID="4" Parent="3" />
        <Category Name="SubCategory 1.1.2" ID="5" Parent="3" />
        <Category Name="SubCategory 1.2" ID="6" Parent="2" />
        <Category Name="SubCategory 1.2.1" ID="7" Parent="6" />
        <Category Name="SubCategory 1.3" ID="8" Parent="2" />
        <Category Name="SubCategory 2" ID="9" Parent="1" />
        <Category Name="SubCategory 2.1" ID="10" Parent="9" />
        <Category Name="SubCategory 2.1.1" ID="11" Parent="10" />
        <Category Name="SubCategory 2.2" ID="12" Parent="9" />
        <Category Name="SubCategory 3" ID="13" Parent="1" />
        <Category Name="SubCategory 3.1" ID="14" Parent="13" />
        <Category Name="SubCategory 3.2" ID="15" Parent="13" />
    </Categories>

    Code XML:
    <Category Name="Main Category">
        <Category Name="SubCategory 1">
            <Category Name="SubCategory 1.1">
                <Category Name="SubCategory 1.1.1" />
                <Category Name="SubCategory 1.1.2" />
            </Category>
            <Category Name="SubCategory 1.2">
                <Category Name="SubCategory 1.2.1" />
            </Category>
            <Category Name="SubCategory 1.3" />
        </Category>
        <Category Name="SubCategory 2">
            <Category Name="SubCategory 2.1">
                <Category Name="SubCategory 2.1.1" />
            </Category>
            <Category Name="SubCategory 2.2" />
        </Category>
        <Category Name="SubCategory 3">
            <Category Name="SubCategory 3.1" />
            <Category Name="SubCategory 3.2" />
        </Category>
    </Category>

    Thanks,
    Jake
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok, so I take it that both are valid approaches, that the one I'd choose will depend on the situation?

    I can see benefits of the latter:

    • Grabbing all children of an element should be more efficient.
    • Easier for a person to navigate the file
    • Searching for an element that has a certain name match and that is under another element would be MUCH easier.



    But, with the former, I can't think of that many benefits. So, I suppose I'll be going with the latter!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard silver trophybronze trophy
    Join Date
    Jul 2008
    Location
    New York, NY
    Posts
    1,432
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With the latter you will also have more control, which is probably what you want

    What do you think about this?
    Code XML:
    <Category Name="Main Category">
        <Category Name="SubCategory 1">
    		<Category Name="SubCategory 1.1" />
    		<Category Name="SubCategory 1.1.1" />
    		<Category Name="SubCategory 1.1.2" />
    		<Category Name="SubCategory 1.2.1" />
    		<Category Name="SubCategory 1.3" />
        </Category>
        <Category Name="SubCategory 2">
            <Category Name="SubCategory 2.1" />
            <Category Name="SubCategory 2.1.1" />
            <Category Name="SubCategory 2.2" />
        </Category>
        <Category Name="SubCategory 3">
            <Category Name="SubCategory 3.1" />
            <Category Name="SubCategory 3.2" />
        </Category>
    </Category>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •