XML Binding With EMF

What This Example Is About

The attached source code is the accompanying material to the Long Talk written and presented by Ed Merks at EclipseCon 2006, entitled XML Binding With The Eclipse Modeling Framework. It is presented as an exercise for the reader, showing complete code where the presentation shows only excerpts.


Required Materials

General Advice / Warnings

Exercise Instructions

This exercise is carried out entirely using the Eclipse Software Development Kit (SDK) version 3.2 with the Eclipse Modeling Framework (EMF) 2.2 installed into it. The exercise instructions refer to this product as either Eclipse or as "the workbench".


Directions

Step A: Verify workbench configuration

  1. Ensure Eclipse is running, and that EMF is properly installed.
    1. Bring up the Help -> About Eclipse SDK dialog.
    2. Click on Feature Details. Click the Feature Id column heading to order the features by that field. Then, check that at least the following EMF features are listed:
  1. Ensure that the JDK used by Eclipse is valid.
    1. Bring up the Help -> About Eclipse SDK dialog.
    2. Click on Configuration Details. Scroll down to where eclipse.vm is shown. This should be something like:
      eclipse.vm=c:\ibm-java2-1.4\bin\java,
      eclipse.vm=/opt/ibm-java2-1.4/bin/java, or similar
    3. If your Eclipse install has defaulted to using the Sun JDK 1.4, you must use a different one to avoid a bug in the Crimson DOM when importing from XSD. The following JDKs will work:

Step B: Import the project into the workspace

  1. Switch to the Java perspective, if not already there.
    1. Select Window -> Open Perspective -> Java.
  2. Select File -> Import....
  3. Expand the twisty next to General and select Existing Projects into Workspace. Hit Next >.
  4. Keep Select root directory selected, enter the full path of the org.eclipse.emf.example.dom folder located in this folder, and hit Enter. You can also use the Browse... button to navigate up to this folder.
  5. Make sure org.eclipse.emf.example.dom is selected in the Projects list and select Finish.

Step C: Exploration

  1. You can run any of the three main classes in org.eclipse.emf.example.dom/src/org.eclipse.emf.example.dom to see sample output for them.
  2. To run a java main class, select the java file in the Package Explorer.
  3. Hit SHIFT-ALT-X (eXecute). Wait one second for the context menu to open, then then hit J (Java Application).
  4. For TreeExample.java, You should see the output similar to the following (line breaks & spacing added):
  5. DOM Create And Save
    <?xml version="1.0" encoding="UTF-8"?><tree:rootNode 
        xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" label="root">
      <tree:childNode label="text">text</tree:childNode>
      <tree:childNode label="comment"><!--comment--></tree:childNode>
      <tree:childNode label="cdata"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    DOM Load And Traverse
    <?xml version="1.0" encoding="UTF-8"?><tree:rootNode 
        xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" label="root">
      <tree:childNode label="text">text</tree:childNode>
      <tree:childNode label="comment"><!--comment--></tree:childNode>
      <tree:childNode label="cdata"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    Traversing DOM Element
    {http://www.eclipse.org/emf/example/dom/Tree}rootNode
      label=root
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=text
        'text'
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=comment
        <!--comment-->
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=cdata
        <![CDATA[<cdata>]]>
      '\n'
    EMF DOM Create And Save
    <?xml version="1.0" encoding="UTF-8"?>
    <tree:rootNode xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" label="root">
      <tree:childNode label="text">text</tree:childNode>
      <tree:childNode label="comment"><!--comment--></tree:childNode>
      <tree:childNode label="cdata"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    EMF DOM Load And Traverse
    <?xml version="1.0" encoding="UTF-8"?>
    <tree:rootNode xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" label="root">
      <tree:childNode label="text">text</tree:childNode>
      <tree:childNode label="comment"><!--comment--></tree:childNode>
      <tree:childNode label="cdata"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    Traversing EMF AnyType
    {http://www.eclipse.org/emf/example/dom/Tree}rootNode
      label=root
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=text
        'text'
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=comment
        <!--comment-->
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=cdata
        <![CDATA[<cdata>]]>
      '\n'
    Result of EMF to DOM conversion
    <?xml version="1.0" encoding="UTF-8"?><tree:rootNode 
        xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" label="root">
      <tree:childNode label="text">text</tree:childNode>
      <tree:childNode label="comment"><!--comment--></tree:childNode>
      <tree:childNode label="cdata"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    XSD Create Schema
    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" 
        xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.eclipse.org/emf/example/dom/Tree">
    <xsd:complexType mixed="true" name="TreeNode">
    <xsd:sequence>
    <xsd:element ecore:name="childNodes" form="qualified" maxOccurs="unbounded"
        name="childNode" type="tree:TreeNode"/>
    </xsd:sequence>
    <xsd:attribute name="label" type="xsd:ID"/>
    <xsd:attribute ecore:reference="tree:TreeNode" name="references">
    <xsd:simpleType>
    <xsd:list itemType="xsd:anyURI"/>
    </xsd:simpleType>
    </xsd:attribute>
    </xsd:complexType>
    <xsd:element name="rootNode" type="tree:TreeNode"/>
    </xsd:schema>
    XSD to Ecore Mapping
    package http://www.eclipse.org/emf/example/dom/Tree
      class DocumentRoot
        attribute mixed:EFeatureMapEntry
        reference xMLNSPrefixMap:EStringToStringMapEntry
        reference xSISchemaLocation:EStringToStringMapEntry
        reference rootNode:TreeNode
      class TreeNode
        attribute mixed:EFeatureMapEntry
        reference childNodes:TreeNode
        attribute label:ID
        reference references:TreeNode
    <?xml version="1.0" encoding="UTF-8"?>
    <tree:rootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree"
        xsi:schemaLocation="http://www.eclipse.org/emf/example/dom/Tree ../model/DOMEMFTreeNode.xsd" 
        label="root">
      <tree:childNode label="text">text</tree:childNode>
      <tree:childNode label="comment"><!--comment--></tree:childNode>
      <tree:childNode label="cdata"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    Traversing EMF EObject
    {http://www.eclipse.org/emf/example/dom/Tree}rootNode
      label=root
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=text
        'text'
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=comment
        <!--comment-->
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=cdata
        <![CDATA[<cdata>]]>
      '\n'
    EMF Dynamic Load Schema
    <?xml version="1.0" encoding="UTF-8"?>
    <tree:rootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree"
        xsi:schemaLocation="http://www.eclipse.org/emf/example/dom/Tree ../model/DOMEMFTreeNode.xsd" 
        label="root">
      <tree:childNode label="text">text</tree:childNode>
      <tree:childNode label="comment"><!--comment--></tree:childNode>
      <tree:childNode label="cdata"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    Traversing EMF EObject
    {http://www.eclipse.org/emf/example/dom/Tree}rootNode
      label=root
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=text
        'text'
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=comment
        <!--comment-->
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=cdata
        <![CDATA[<cdata>]]>
      '\n'
    Showing References set by traversal
    <?xml version="1.0" encoding="UTF-8"?>
    <tree:rootNode xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" label="root"
        references="#root">
      <tree:childNode label="text" references="#root">text</tree:childNode>
      <tree:childNode label="comment" references="#root"><!--comment--></tree:childNode>
      <tree:childNode label="cdata" references="#root"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    EMF Static Generated Load and Traverse
    <?xml version="1.0" encoding="UTF-8"?>
    <tree:rootNode xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" 
        label="root" references="#root">
      <tree:childNode label="text" references="#root">text</tree:childNode>
      <tree:childNode label="comment" references="#root"><!--comment--></tree:childNode>
      <tree:childNode label="cdata" references="#root"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    Traversing EMF TreeNode
    {http://www.eclipse.org/emf/example/dom/Tree}rootNode
      label=root
      reference=#root
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=text
        reference=#root
        'text'
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=comment
        reference=#root
        <!--comment-->
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=cdata
        reference=#root
        <![CDATA[<cdata>]]>
      '\n'
    Showing the references set in the copy
    <?xml version="1.0" encoding="ASCII"?>
    <tree:rootNode xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" label="root" 
        references="#root EMFDOMTreeNodeWithReferences.xml#root">
      <tree:childNode label="text" 
          references="#root EMFDOMTreeNodeWithReferences.xml#text">text</tree:childNode>
      <tree:childNode label="comment" 
          references="#root EMFDOMTreeNodeWithReferences.xml#comment"><!--comment--></tree:childNode>
      <tree:childNode label="cdata" 
          references="#root EMFDOMTreeNodeWithReferences.xml#cdata"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    EMF Multi Resource Static Generated Load and Traverse
    <?xml version="1.0" encoding="ASCII"?>
    <tree:rootNode xmlns:tree="http://www.eclipse.org/emf/example/dom/Tree" label="root" 
        references="#root EMFDOMTreeNodeWithReferences.xml#root">
      <tree:childNode label="text" 
          references="#root EMFDOMTreeNodeWithReferences.xml#text">text</tree:childNode>
      <tree:childNode label="comment" 
          references="#root EMFDOMTreeNodeWithReferences.xml#comment"><!--comment--></tree:childNode>
      <tree:childNode label="cdata" 
          references="#root EMFDOMTreeNodeWithReferences.xml#cdata"><![CDATA[<cdata>]]></tree:childNode>
    </tree:rootNode>
    Traversing EMF TreeNode file:/E:/workspace/org.eclipse.emf.example.dom/data/TreeNodeMap.xml
    {http://www.eclipse.org/emf/example/dom/Tree}rootNode
      label=root
      reference=TreeNodeMap.xml#root
      reference=EMFDOMTreeNodeWithReferences.xml#root
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=text
        reference=TreeNodeMap.xml#root
        reference=EMFDOMTreeNodeWithReferences.xml#text
        'text'
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=comment
        reference=TreeNodeMap.xml#root
        reference=EMFDOMTreeNodeWithReferences.xml#comment
        <!--comment-->
      '\n  '
      {http://www.eclipse.org/emf/example/dom/Tree}childNode
        label=cdata
        reference=TreeNodeMap.xml#root
        reference=EMFDOMTreeNodeWithReferences.xml#cdata
        <![CDATA[<cdata>]]>
      '\n'
      
  6. Refer to the presentation slides for additional information.