Lesson 1.3: Create and edit your XML file
Before you begin
Generate an XML file based on your DTD
About this task
Procedure
- In the Navigator view, right-click the CDTitle.dtd that you created earlier, and from its pop-up menu click .
- Select your project and accept the default name. Click Next.
- 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.
- Click Finish.
Results
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
About this task
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:
Procedure
Edit in the Source view
Procedure
- Select your XML file, and click to turn the grammar constraints back on.
- Click the Source tab to switch to the Source view.
- Place your cursor after the opening tag for the first CD element: <cd>
- Press Ctrl + Space to activate code assist. A pop-up list of available choices, which is based on the context, is displayed.
- Double-click artist.
- Type John Brown between the <artist> tags.
- Now place your cursor after the artist tags and active code assist. Double-click description.
- Type Opera between the description tags.
- Select the code for the price element you have not modified and delete it.
- Save your file.
- 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.
Results

Lesson checkpoint
- 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.