# Generating an XML file from an XML schema

You can generate an XML file from an XML schema file. You can then further customize that file in the XML editor.

Generating an XML file from an XML schema can be useful if you want to quickly create an XML file that is based on your XML schema.

The following instructions were written for the Resource perspective, but they will also work in many other perspectives.

To generate an XML file from a schema file follow these steps:

1. In the Navigator view, right-click the XML schema file that you want to work with.
2. From its pop-up menu click Generate > XML File.
3. Select a project or folder to contain the XML file and type a name for it. The name of the file must end in`.xml.` .
4. Click Next.
5. Click the Root element of the XML file. The root element of an XML file is the element that contains all other elements in that file. All of the global elements you have defined in the XML schema will be included in the Root Element list. If you do not have any global elements in your XML schema, you cannot create an XML file from it.
6. Select from the following content options:
   - Create optional attributes - Both mandatory and optional attributes will be generated
   - Create optional elements - Both mandatory and optional elements will be generated.
   - Create first choice of required choice - The first option of a required choice will be generated in your XML file.
   - Fill elements and attributes with data - any elements and attributes generated will be filled with sample data.
   If you do not select any of these options, then only the minimum amount of content required for the XML file will be created.
7. The Namespace information section contains information about the target namespace of the XML schema, its prefix, and the schema location. For more information about namespaces and namespace prefixes, refer to the Related reference below.
8. Select the entry you want to edit and click Edit.
9. The value in the Namespace Name field is the target namespace of the XML schema. Your XML file should be associated with the same namespace as its XML schema is associated with.
10. All qualified elements and attributes in the XML file associated with the namespace will be prefixed with the Prefix value.
11. The Location Hint field contains the location of the XML schema.
12. Click OK, then Finish.

The XML file appears in the Navigator view and, by default, automatically opens in the XML source page editor. The XML source page editor offers limited functionality compared to the XML editor, so before editing the file, you should close it, then right-click the file in the Navigator view and click Open With > XML Editor to open the file in the XML editor.

To make the XML editor the default editor for XML files, select Window > Preferences > Workbench > File Associations, then select *.xml and XML Editor from the Associated Editors list. Click OK.

The XML file will contain the selected root element and any elements or attributes contained in the root element. It also contains information about the XML file namespace and location. You can now add elements and attributes to the XML file.

Note: In certain cases, when an XML file is generated from a complex XML schema file, the XML file may not be valid. If this occurs, you can open the generated file in the XML editor and correct any errors that occur. Usually, only a few errors need to be fixed. The following XML schema constructs may present problems:

1. Restrictions. Sometimes restricted elements are erroneously generated.
2. Facets: Default generated data values may not conform to complex facets (for example, patterns).
3. Abstract elements. abstract elements are sometimes erroneously generated.

Related concepts
XML editor

Related tasks
Creating an XML file from scratch

(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.