Tutorial 02 - Generate a schema from an xml instance.

Top  Previous  Next

12/30/2016 - The current version of the software is 3.4.1

Please upgrade if you are not using the latest version of the software.

 

Last Updated: 11-18-2016

 

In this tutorial, we will use the SchemaGenerator to generate a schema from the books.xml instance.

 

What is a schema?

 

The schema language, developed for this module, is novel for it’s simplicity and expressive power. Anyone familiar with XML should be able to pick it up quickly and easily. This is because a schema is nothing more than an XML instance document annotated with meta-information (in the form of XML attributes). These attributes are then used by the code generator to produce Java code that conforms to the schema language.

 

Let's begin with a simple example.

 

We start with the following XML instance document.

 

<root>
    <container attribute="4">
        <value>12</value>
    </container>
</root>

 

Creating a schema is as simple as wrapping the instance document in a schema root element and then annotating the instance document with the appropriate meta-data.

 

<schema packageScope="com.wickedfastsolutions.demo.domain">
    <root>
        <container>
            <attribute parent="container" type="positiveInteger">4</attribute>
            <value type="integer" minValue="0" maxValue="255">12</value>
        </container>
    </root>
</schema>

 

So imagine you have the following xml document with a list of books in it.

 

<Books>

    <Book>

        <ISBN10>1781390681</ISBN10>

        <ISBN13>978-1781390689</ISBN13>

        <Title>A Christmas Carol</Title>

        <Author>

            <FirstName>Charles</FirstName>

            <LastName>Dickens</LastName>

        </Author>

        <PublishYear>1843</PublishYear>

        <ImagePath>images/a-christmas-carol.png</ImagePath>

    </Book>

    <Book>

        <ISBN10>1477581855</ISBN10>

        <ISBN13>978-1477581858</ISBN13>

        <Title>Alice's Adventures In Wonderland</Title>

        <Author>

            <FirstName>Lewis</FirstName>

            <LastName>Carroll</LastName>

        </Author>

        <PublishYear>1865</PublishYear>

        <ImagePath>images/alices-adventures-in-wonderland.png</ImagePath>

    </Book>

    <Book>

        <ISBN10>0142418218</ISBN10>

        <ISBN13>978-0142418215</ISBN13>

        <Title>Charlie and the Chocolate Factory</Title>

        <Author>

            <FirstName>Roald</FirstName>

            <LastName>Dahl</LastName>

        </Author>

        <PublishYear>1964</PublishYear>

        <ImagePath>images/charlie-and-the-chocolate-factory.png</ImagePath>

    </Book>

 

...

 

How would you go about using the wickedfast solutions tools to access the data in it.

 

First, we need a schema describing the format of the xml document.

 

We can generate a schema from the instance document using the SchemaGenerator.

 

Step 1

 

Save the file http://www.wickedfastsolutions.com/resources/books-instance.xml to your desktop.

 

Step 2

 

Type the following text into the console:

 

SchemaGenerator

 

You should now see the following UI.

 

sg1

 

Step 3

 

Select the XML Instance tab at the top of the window.

 

sg2

 

Step 4

 

Click the add button to add the books-instance.xml document.

 

sg3

 

Step 5

 

Click on the generate schema button to generate a schema from the supplied instance documents.

 

sg5

 

 

The following schema document should of been generated for you.

 

<?xml version="1.0"?>

<schema packageScope="com.wickedfastsolutions.domain.books">

    <Books>

        <Book maxOccurs="unbounded" minOccurs="0">

            <!-- 0394800168, 1477581855, 0061124958, 039480001X, 0142418218 -->

            <ISBN10 example="0142418218" length="10">ISBN10</ISBN10>

            <!-- 978-1477581858, 978-0060775858, 978-0723263920, 978-0399226908, 978-1781390689 -->

            <ISBN13 example="978-1781390689" length="14" minOccurs="0"></ISBN13>

            <!-- Charlotte's Web, Alice's Adventures In Wonderland, Charlie and the Chocolate Factory -->

            <Title example="Green Eggs and Ham" maxLength="36">Title</Title>

            <Author>

                <!-- Charles, L., Eric, E., Beatrix -->

                <FirstName example="Beatrix" maxLength="8">FirstNam</FirstName>

                <!-- Seuss, B., Wise, Frank, A. -->

                <MiddleName example="A." maxLength="5" minOccurs="0"></MiddleName>

                <!-- Dahl, Brown, Dickens, Geisel, Baum -->

                <LastName example="Baum" maxLength="11">LastName</LastName>

            </Author>

            <!-- 1908, 1843, 1865, 1964, 1952 -->

            <PublishYear example="1952" type="integer">0</PublishYear>

            <!-- images/alices-adventures-in-wonderland.png, images/the-wind-in-the-willows.png -->

            <ImagePath example="images/the-cat-in-the-hat.png" maxLength="46">ImagePath</ImagePath>

        </Book>

    </Books>

</schema>

 

Step 6

 

You can now use this newly generated schema to complete tutorial 01.

 

Take Away.

 

While it is possible and sometimes preferable to write schemas by hand. It is often times better to generate the schemas using the ScemaGenerator. The SchemaGenerator can already automatically generate a Wickedfast schema from W3C XML Schema's, Relational Database's, Delimited Text files, XML Instance files and from properties files.

 

This can save you a lot of time when trying to model these types of documents.

 

Congratulations, you have just finished Tutorial-02!