views:

211

answers:

5

I'm doing a personal organizer for learning purposes, and i've never worked with XML so i'm not sure if my solution is the best. Here's the basic structure for the XML file i came with:

<calendar>
    <year value="2008">
        <month value="october">
            <day value="16">
                <activity name="mike's birthday" time="21:00" address="mike's apartment" urgency="10">
                     activity description.
                </activity>
            </day>
        </month>
    </year>
</calendar>

The urgency attribute should be on a scale of 1 to 10.
I did a quick search on google and couldn't find a good example. Maybe that's not the best solution, and i'd like to know if its adequate. I'm doing the application in PHP if that has any relevance.

+2  A: 

Your way is quite adequate to me. However, I prefer child tags to attributes, so my way would be more like:

<activity>
  <name>Mike's Birthday</name>
  <time>2100</time>
  <address>Mike's Place</address>
  <urgency>10</urgency>
  <description>activity description</description>
</activity>

But like I said, your way is just fine.

Quick question, though - why not a database?

Adam V
Wait, XML isn't a database? (tongue in cheek, sarcasm, etc) ;-D
Adam Davis
One of the aims when i was proposed the development of this application was to learn XML manipulation. I'll be messing with MySQL later on. Thanks for the answer!
David McDavidson
@bullettime: No problem! I figured it this project was partially an XML learning tool, and you're on the right path as far as that goes. Good luck with everything!
Adam V
A: 

You could flatten that hierarchy down to:

<calendar>
    <activity
     id="123456"
     name="mike's birthday" 
     year="2008"
     month="10"
     day="16"
     time="21:00" 
     address="mike's apartment" 
     urgency="10">
      activity description.
     </activity>
</calendar>

or..

<calendar>
    <activity id="12345">
     <name>mike's birthday</name>
     <year>2008</year>
     <month>10<month>
     <day>16</day>
     <time>21:00</time>
              <urgency>10</urgency>
     <address>mike's apartment<address>
     <description>activity description.</description>
    </activity>
</calendar>

It'd make life a bit less painful doing XPath queries. I also added an id attribute so you can uniquely identify an activity.

Kev
This structure looks like a better option. Thanks!
David McDavidson
That's true; however, it makes it harder to determine whether there are any events on a specific day because you have to drill into each activity.
Adam V
I have to agree to what Adam said, even though Kev's XML looks a lot cleaner than what i came up with. It's precisely for these thoughts that i asked this question here on stackoverflow. Thanks to both of you
David McDavidson
@Adam - you have all three (yr, month, day) attributes on the same node or elements as sibilings in the second example. How can that be harder?
Kev
@Kev: If you want to see if there's an activity on a certain day (say, April 1 '08), you look for a single (2008/04/01) element. If it's not there, you're done. If arranged your way, you have to check every activity element for matching dates.
Adam V
@adam - you mean harder for the xpath engine?
Kev
@Kev: I mean taking up more time. If you're broken down by date, the engine is able to look for one specific node and then stop. If not, you're looking at every activity node. This means it will also get slower as the XML gets larger.
Adam V
@Adam = yeah i get where you're coming from.
Kev
A: 

I think your structure will be fine for what you are doing.

If you're planning to use this partly to learn about XML, you might consider using a mix of attributes and elements so that you get practice working with collections of each. Once you're more comfortable with XML, you will probably start to define rules that you'll use to determine which properties become attributes and which properties become elements.

With the right code, you can move information back and forth between XML files and database tables. You could also start learning XSL so that you can practice moving things around without changing the original XML file (or, once the data is in a table, not even have an original XML file).

Dave DuPlantis
The more i read about XML more i stumble upon other terms like XSL, XPath, etc.. I originally though that PHP DOM was enough, but it seems i still have a lot to learn. Thanks for the answer.
David McDavidson
A: 

It might be worth looking at xCal, an XML-compliant representation of the iCalendar standard, for some potentially well-thought-out ideas.

Joe Lencioni
iCalendar seems to be way more complex than my application has to be. Thanks nevertheless
David McDavidson
+1  A: 

You may have arrived at this naively, but the primary feature of your XML design is that it is optimized for searching by date. If your XML document is large, and you do a lot of searching by date (which I suspect is the most common use case in a personal organizer), this is a good thing.

Executing this XPath pattern:

/calendar/year[@value='2008']/month[@value='10']/day[@value='7']/activity

will examine many fewer nodes than will using the pattern you'd need to use with Kev's simpler flattened-out organization:

/calendar/activity[@year='2008' and @month='10' and @day='7']

which basically has to look at every node in the document.

Note, by the way, that I'm assuming that the month and day attributes are numeric. This is important because you'll almost certainly want to sort this data at some point, and unless you're going to maintain the sort order in the document (which, I'll admit, an argument can be made for), you'll want those attributes in a form that it's easy to sort them in.

It's also important that you're consistent in how you store numeric data in those attributes. (If you want to sound smart in meetings, you can say that you're establishing canonical representations of your data types.) If you use leading zeroes some times and not others, for instance, none of those XPath patterns will work reliably, because @day='7' won't match a day attribute set to "07". (You can get around that by converting the attributes to numbers in your XPath using the number() function, but avoiding the problem in the first place is better.)

Robert Rossney
I definitely have to take a look at XPath. Thanks
David McDavidson
XPath is really what makes XML worth using.
Robert Rossney