Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EMF/Generating Dynamic Ecore from XML Schema"

< EMF
Line 1: Line 1:
<b>Generating Ecore using from XML Schema</b><br>
+
<b>EMF and XML - An Introduction</b><br>
In this Wiki document, i have tried to document the steps involved in Generating Ecore using XML Schema and loading a appropriate XML File into the Model.<br>
+
In this Wiki document, i have tried to document the steps involved in Generating Ecore using XML Schema, loading an XML File into an Ecore Model etc.<br>
 
<ol>
 
<ol>
 
<li>Convert a given XSD into Ecore</li>
 
<li>Convert a given XSD into Ecore</li>
Line 31: Line 31:
 
     resource.load(options);
 
     resource.load(options);
 
</pre>
 
</pre>
Note: The XML File should contain xsi:schemaLocation. EMF provides this inbuild 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.<br><br>
+
Note: The XML File should contain xsi:schemaLocation. EMF provides this inbuild 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.
 +
<br>
 +
If a Schema name is not provided, XML would be loaded with a default Schema provided by EMF which would consider all the nodes as xsd:anyType.
 +
<br>
 +
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.
 +
<br>
 +
<br>
 
<b>Display using a TreeViewer</b><br>
 
<b>Display using a TreeViewer</b><br>
 
<pre>
 
<pre>

Revision as of 06:38, 13 February 2007

EMF and XML - An Introduction
In this Wiki document, i have tried to document the steps involved in Generating Ecore using XML Schema, loading an XML File into an Ecore Model etc.

  1. Convert a given XSD into Ecore
  2. Load an XML into the generated Ecore
  3. Display the loaded Model using a TreeViewer

For this code to execute, you need to include the following plugins as dependencies

  • org.eclipse.emf.ecore
  • org.eclipse.xsd
  • org.eclipse.emf.ecore.xmi
  • org.eclipse.emf.edit.ui

Convert a XSD into Ecore

The following code snippet would enable you to load an existing XSD and convert it into an Ecore.

    XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
    ResourceSet resourceSet = new ResourceSetImpl();
    Collection eCorePackages = xsdEcoreBuilder.generate(URI.createFileURI("D:/model/example.xsd"));

Note: Running this code could get you a nullPointerException or schemaFileNotFoundException. This is due to a ever popular Crimsom DOM bug. Please refer to the following link to find a workaround to this problem. Manifestation of Crimsom DOM Bug

Load an XML into the Ecore

    HashMap options = new HashMap();
    options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
    Resource resource = resourceSet.createResource(URI.createFileURI("D:/model/example.xml"));
    resource.load(options);

Note: The XML File should contain xsi:schemaLocation. EMF provides this inbuild 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 a Schema name is 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.

Display using a TreeViewer

    List factories = new ArrayList();
    factories.add(new ResourceItemProviderAdapterFactory());
    factories.add(new ReflectiveItemProviderAdapterFactory());
    
    ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory();

    TreeViewer tViewer = new TreeViewer(parent);
    tViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory);
    tViewer.setLabelProvider(new AdapterFactoryContentProvider(adapterFactory);
    tViewer.setInput(resourceSet);

Further Reading
Discover the Eclipse Modeling Framework (EMF) and Its Dynamic Capabilities
Newsgroup Msg11109

--Annamalai.chockalingam.gmail.com 18:06, 10 February 2007 (EST)

Back to the top