1 XML Introduction

It is best to edit XML files with a dedicated XML editor. A free, open-source XML editor is Butterfly. Another free XML editor is XmlWrench but that is PC-only. Some browsers are also quite good at displaying XML files.

1.1 Exercises

1) Install an XML editor. The exercises below assume that you are installing the Butterfly editor, but other XML editors should have similar functionality. (If you have problems installing an editor, see item 1.2 below for exercises that don't involve an XML editor.)
To run the Butterfly editor on Unix type "sh run.sh" in the Butterfly folder; on Windows click on run.bat or ButterflyXML.exe.

2) Click on "Create a new XML file". Create an XML file for a weekly schedule with

Note: Butterfly does not have a copy/paste function, but Ctrl-C/Ctrl-V might work. Otherwise, once you have entered the information for one day, you can right click on the tags and use "add sibling/add child" to enter the tags for other days, etc.

Think about the best modelling strategy for this XML file. Should Monday, Tuesday, etc be elements, attributes or PCDATA (ie content of elements)?

3) When you are finished entering the data, right click on a tag and then select "Generate DTD". Modify the DTD so that events are optional and descriptions of elements are optional. (Note: DTDs use a regular expression like notation: * means 0 or more, + means 1 or more, ? means 0 or 1).

4) The following XML is a mess! Save it as a file and open it in your XML editor. Fix all the problems. Note: don't try to generate the DTD while there are errors in your XML because Butterfly may crash. Save the XML before creating a DTD.

<!- This is about movies! ->
  <!ELEMENT actor ( #PCDATA )>
  <!ELEMENT movie (actor*, title, year*)>
  <!ELEMENT title ( #PCDATA )>
  <!ELEMENT year ( #PCDATA )>

  <movie id = 1>
    <title>The Quest</title>
    <actor>Tom Smith</actor>
  <movie id = 2>
    <actor>Susie Black</actor>
    <actor>Paul White</actor>
 <movie id = 3><title>Hello World</movie></title>

1.2 More exercises

Andrew Cumming has created on-line tutorials for learning XML: DTD and more DTD. They contain more details than will be covered in this class, but you might want to do some of the exercises from these tutorials to get a better understanding of DTDs. There is also a tutorial for XML Schema which is an alternative to using DTDs, but will not be covered in this class.