Note: Running this code could get you a NullPointerException or schema FileNotFoundException with running on a 1.4 Sun JVM. This is due to the ever popular Crimsom DOM bug. Please refer to the following link to find a workaround to this problem.Manifestation of Crimsom DOM Bug

For these loaded Ecores to be available, we need to register them to the global package registry.

Note: The XML file should contain an xsi:schemaLocation. EMF provides this a built-in feature wherein if the XML knows its schema, EMF would build the model from the schema and load the XML without having to perform Step 1.Convert XSD into Schema.

If no schemaLocation is specified, at least the namespace should be specified so that it may search for the package in the package registry registered with the same namespace and load the XML according to that Ecore model.

If neither a schemaLocation nor namespace are not provided, XML would be loaded with a default schema provided by EMF which would consider all the nodes as xsd:anyType.

Since some of the XML Schema specific contructs are not represented directly in Ecore, they are represented as Ecore Annotations. These annotations are termed as ExtendedMetaData and to query them we need to use the ExtendedMetaData APIs. Therefore to load an XML which contains the schemaLocation we need to provide the loading option XMLResource.OPTION_EXTENDED_META_DATA
as true as shown in the code above.