1. 编写Schema

    1. 模型

    图书馆管理书(Book)和作者(Writer),书有书的目录(BookCategory)。

    1. 代码

    <?xml version="1.0" encoding="UTF-8"?>

    <xsd:schema xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:lib="http://www.example.eclipse.org/Library" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.eclipse.org/Library">

        <xsd:complexType name="Book">

            <xsd:sequence>

                <xsd:element name="title" type="xsd:string"/>

                <xsd:element name="pages" type="xsd:int"/>

                <xsd:element name="category" type="lib:BookCategory"/>

                <xsd:element name="author" type="xsd:anyURI"/>

            </xsd:sequence>

        </xsd:complexType>

        <xsd:complexType name="Writer">

            <xsd:sequence>

                <xsd:element name="name" type="xsd:string"/>

                <xsd:element name="books" type="xsd:anyURI" minOccurs="0" maxOccurs="unbounded"/>

            </xsd:sequence>

        </xsd:complexType>

        <xsd:complexType name="Library">

            <xsd:sequence>

                <xsd:element name="name" type="xsd:string"/>

                <xsd:element name="writers" type="lib:Writer" minOccurs="0" maxOccurs="unbounded"/>

                <xsd:element name="books" type="lib:Book" minOccurs="0" maxOccurs="unbounded"/>

            </xsd:sequence>

        </xsd:complexType>

        <xsd:simpleType name="BookCategory">

            <xsd:restriction base="xsd:NCName">

                <xsd:enumeration value="Mystery"/>

                <xsd:enumeration value="ScienceFiction"/>

                <xsd:enumeration value="Biography"/>

            </xsd:restriction>

        </xsd:simpleType>

    </xsd:schema>

    1. Schema表述

  2. 导入EMF

    1. 新建EMF项目

    1. 选择导入器

    1. 选择导入的包

    1. 查看模型

    1. 修改模型属性

    1. 生成模型代码

    1. 查看模型代码

    1. Book接口和Writer接口

    1. BookCategory枚举

    1. Library接口

    1. LibraryFactory工厂

    1. EPackage接口

    1. 查看模型实现的代码

    1. 查看模型操作方式

  3. 编写Java代码操作模型

    1. 新建程序主入口类

    1. 创建Library实例

      LibraryFactory factory = LibraryFactory.eINSTANCE;

    2. 创建Library、Book和Writer实例

              Library library = factory.createLibrary();

       

              Book book = factory.createBook();

       

              Writer writer = factory.createWriter();

    3. 对模型进行操作

      1. 写模型

                library.setName("UESTC Library");

         

                writer.setName("William Shakespeare");

         

                book.setTitle("King Lear");

                book.getAuthor().add(writer);

                

                library.getBooks().add(book);

                library.getWriters().add(writer);

      2. 读模型

                System.out.println("Shakespeare books:");

                for (Iterator<Book> iter = writer.getBooks().iterator(); iter.hasNext();)

                {

                    Book shakespeareBook = (Book) iter.next();

                    System.out.println(" title: " + shakespeareBook.getTitle());

             }

    4. 把模型保存到XML文件中

      1. 创建资源集

                // Create a resource set.

                ResourceSet resourceSet = new ResourceSetImpl();

         

                // Register the default resource factory -- only needed for stand-alone!

                resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()

                        .put(Resource.Factory.Registry.DEFAULT_EXTENSION,

                                new LibraryResourceFactoryImpl());

         

                // Get the URI of the model file.

                URI fileURI = URI.createFileURI(new File("mylibrary.xml")

                        .getAbsolutePath());

         

                // Create a resource for this file.

                Resource resource = resourceSet.createResource(fileURI);

      2. 把模型添加到资源集中

                // Add the book and writer objects to the contents.

                resource.getContents().add(library);

      3. 保存资源集到XML文件中

                try

                {

                    resource.save(Collections.EMPTY_MAP);

                }

                catch (IOException e)

                {

                }

      4. 查看XML文件

        <?xml version="1.0" encoding="ASCII"?>

        <library:Library xmlns:library="http://www.example.eclipse.org/Library">

        <name>UESTC Library</name>

        <writers>

        <name>William Shakespeare</name>

        <books>#//@books.0</books>

        </writers>

        <books>

        <title>King Lear</title>

        <author>#//@writers.0</author>

        </books>

        </library:Library>

    5. 从XML文件中读取资源

      1. 创建资源集

                // Create a resource set.

                ResourceSet resourceSet2 = new ResourceSetImpl();

         

                // Register the default resource factory -- only needed for stand-alone!

                resourceSet2.getResourceFactoryRegistry().getExtensionToFactoryMap()

                        .put(Resource.Factory.Registry.DEFAULT_EXTENSION,

                                new LibraryResourceFactoryImpl());

         

                // Register the package -- only needed for stand-alone!

                LibraryPackage libraryPackage = LibraryPackage.eINSTANCE;

         

                // Get the URI of the model file.

                URI fileURI2 = URI.createFileURI(new File("mylibrary.xml")

                        .getAbsolutePath());

      2. 读取资源

                // Demand load the resource for this file.

                Resource resource2 = resourceSet2.getResource(fileURI2, true);

      3. 获得需要的内容

                Library library2 = (Library)resource2.getContents().get(0);

                Book book2 = library2.getBooks().get(0);

                System.out.println("title:" + book2.getTitle());

  4. 自动生成资源编辑器

    1. 编辑器

    左边是XML树,右边是节点属性。

posted on 2010-03-16 21:10  chrihop  阅读(1818)  评论(1编辑  收藏  举报