< Previous | Next >

Lesson 1.3: Create and edit your XML file

This section of the tutorial shows you how to generate an XML file based on your DTD (and associated with it) and then edit the XML file.
Before you begin, you must complete Lesson 1.2: Customize your DTD.

Generate an XML file based on your DTD

To generate an XML file from your DTD file follow these steps:
  1. In the Navigator view, right-click the CDTitle.dtd that you created earlier, and from its pop-up menu click Generate > XML File.
  2. Select your project and accept the default name. Click Next.
  3. Accept the default values in the Select Root Element page. Your XML file will be generated with a root element of CDLib and any elements generated will contain sample data.
  4. Click Finish.

The XML file appears in the Navigator view and automatically opens in the XML source page editor.

The XML file only contains the selected root element and any elements or attributes contained in the root element. You can now add elements, attributes, entities, and notations to the XML file, however, they must follow the rules established in the DTD that you used to create the XML file.

The format element is not included in the generated XML file because you did not select to create optional elements in the steps above.

Edit in the Design view

In the Design view, when you edit an XML file that has a set of constraints (or, a set of rules) defined by a DTD or an XML schema, you can turn those constraints on and off.

When constraints are turned on, you cannot make any changes that violate the rules of the DTD, such as adding an invalid element or removing necessary tags. Also, in this mode, you can use guided editing options.

If you turn constraints off, you can insert or delete any element or attribute regardless of the DTD rules. You may find it faster to work in this mode, but you may also introduce errors, so you should explicitly validate your XML file from time to time.

The CDTitle.xml file is constrained by the rules in the CDTitle.dtd file.

You are now going to edit the CDTitle.xml file in the Design view, at first with constraints on, and then constraints off:

  1. If necessary, open CDTitle.xml in the XML editor (to do so, right-click it in the Navigator view and click Open With > XML Editor.
  2. In the Design view, select the CDLib element and expand it.
  3. Edit the contents of the CD element. Change the artist value to John Brown, the description to Opera and the price to 15.00.
  4. CDTitle.xml currently only contains one CD. You are going to add another:
    1. Right-click CDLib and select Add Child > CD.
    2. A new CD element is automatically created, and it contains three elements: artist, description, and price.
    3. Change the artist value to Jane Smith, the description to Classical and the price to 20.00.
    4. Click File > Save to save your changes.
  5. Try to add and remove certain elements and attributes from the first CD element:
    1. Right-click artist. The menu option Remove is not available because artist is a required element. (You specified this in your DTD file.)
    2. Right-click description and price. The menu option Remove is not available because they are also required items.
    3. Right-click CD and click Add Child. You have the option to add another artist, but not another description or price. The option to add another description or price is not available, because in CDTitle.dtd, you specified that these items could only occur once inside a CD element. However, you specified that you could have one or more artist elements, which is why you can select to add another artist.
  6. You are now going to turn the grammar constraints off, and continue to edit CDTitle.xml. Click Design > Turn Grammar Constraints Off.
  7. Right-click artist in the first CD element and click Remove. You can remove this required element because the grammar constraints are off.
  8. Right-click description in the first CD element and click Remove.
  9. Right-click the first CD element and select Add Child. You now have the option to add another price. Click price.
  10. Save your file. Note that you are allowed to save it even with all the syntactical errors. This is because you turned the constraints off.
  11. In the Navigator view, right-click the XML file and click Validate. The errors you have introduced into the file are listed in the Problems view.

Edit in the Source view

You are now going to edit the CDTitle.xml file in the Source view and fix the errors that you introduced into the file:
  1. Select your XML file, and click Design > Turn Grammar Constraints On to turn the grammar constraints back on.
  2. Click the Source tab to switch to the Source view.
  3. Place your cursor after the opening tag for the first CD element: <cd>
  4. Press Ctrl + Space to activate code assist. A pop-up list of available choices, which is based on the context, is displayed.
  5. Double-click artist.
  6. Type John Brown between the <artist> tags.
  7. Now place your cursor after the artist tags and active code assist. Double-click description.
  8. Type Opera between the description tags.
  9. Select the code for the price element you have not modified and delete it.
  10. Save your file.
  11. In the Navigator view, right-click the XML file and select Validate. The file is now valid and no errors appear in the Problems view for it.
The source code for your file should appear similar to the following:
Source View

Lesson checkpoint

The Design and Source views within the XML editor allow you to easily make changes to you XML file based on the constraints of the associated DTD. The grammar constraints option allows you to choose whether to apply the rules to the file at any given time.
Now that you have completed this lesson, you should be able to:
  • Generate an XML file based on your DTD.
  • Edit the file in the Design view with grammar constraints on and off.
  • Edit the file in the Source view.
< Previous | Next >

Feedback