Skip to main content
Skip table of contents

Classes - Lesson 3.1

In the following step, the XML data source is analyzed to infer the required data structures of the Web service.

The Data Source

Below, the XML File catalog.xml containing product data is displayed. The Web service will read from this data source and send the filtered data to the caller.

HTML/XML
 <catalog> 
 <product> 
 <title>Pirates of the Caribbean - The Curse of the Black Pearl</title> 
 <category>DVD</category> 
 <manufacturer>Buena Vista Home Video</manufacturer> 
 <priceUSD>17.99</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos/tg/detail/-/B00005JM5E/qid=1075478699//ref=pd_ka_1/104-2088971-5001520?v=glance&amp;s=dvd&amp;n=507846</link> 
 </product> 
 <product> 
 <title>Finding Nemo</title> 
 <category>DVD</category> 
 <manufacturer>Walt Disney Home Video</manufacturer> 
 <priceUSD>17.99</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos/tg/detail/-/B00005JM02/qid=1075478699//ref=pd_ka_2/104-2088971-5001520?v=glance&amp;s=dvd&amp;n=507846</link> 
 </product> 
 <product> 
 <title>Once Upon a Time in Mexico (2003)</title> 
 <category>DVD</category> 
 <manufacturer>Columbia Tristar Hom</manufacturer> 
 <priceUSD>20.27</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos/tg/detail/-/B0000WN140/qid=1076055328/sr=1-2/ref=sr_1_2/102-9079676-4243362?v=glance&amp;s=dvd</link> 
 </product> 
 <product> 
 <title>The Lord of the Rings - The Two Towers (Platinum Series Special Extended Edition) (2002)</title> 
 <category>DVD</category> 
 <manufacturer>New Line Home Video</manufacturer> 
 <priceUSD>25.99</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos/tg/detail/-/B00009TB5G/ref=pd_ts_d_8/102-9079676-4243362?v=glance&amp;s=dvd&amp;n=404276</link> 
 </product> 
 <product> 
 <title>Lost In Translation (Widescreen Edition) (2003)</title> 
 <category>DVD</category> 
 <manufacturer>Universal Studios</manufacturer> 
 <priceUSD>18.89</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos/tg/detail/-/B00005JMJ4/ref=pd_ts_d_2/102-9079676-4243362?v=glance&amp;s=dvd&amp;n=404276</link> 
 </product> 
 <product> 
 <title>Panasonic SC-HT700 5-Disc Progressive-Scan DVD Home Theater System</title> 
 <category>Audio & Video</category> 
 <manufacturer>Panasonic</manufacturer> 
 <priceUSD>264.99</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos/tg/detail/-/B00008XL1I/qid=1079949505/br=1-1/ref=br_lf_etk_ce_av__1//104-1167198-1926310?v=glance&amp;s=electronics&amp;n=172593</link> 
 </product> 
 <product> 
 <title>Zenith XBS344 Progressive Scan DVD-VCR Home Theater System (Silver)</title> 
 <category>Audio & Video</category> 
 <manufacturer>Zenith</manufacturer> 
 <priceUSD>249.99</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos/tg/detail/-/B0000A2UAT/qid=1079949505/br=1-13/ref=br_lf_etk_ce_av__13//104-1167198-1926310?v=glance&amp;s=electronics&amp;n=172593</link> 
 </product> 
 <product> 
 <title>Sony DVD/VHS Home Theater System (HT-V1000DP)</title> 
 <category>Audio & Video</category> 
 <manufacturer>Sony</manufacturer> 
 <priceUSD>499.87</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos/tg/detail/-/B0000CBCO6/qid=1079950507/br=1-5/ref=br_lf_etk_ce_av__5//104-1167198-1926310?v=glance&amp;s=electronics&amp;n=172593</link> 
 </product> 
 </catalog> 

Later, you will model a data structure that stores the data of this file.

The XML file is well structured.

  • The first line of the XML file is the header, which states what XML version and what character set are used.

  • The element catalog contains some product elements.

  • Each product element is containing seven further elements that store product data.

HTML/XML
 <catalog> 
 <product> 
 <title>Pirates of the Caribbean - The Curse of the Black Pearl</title> 
 <category>DVD</category> 
 <manufacturer>Buena Vista Home Video</manufacturer> 
 <priceUSD>17.99</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos/tg/detail/-/B00005JM5E/qid=1075478699//ref=pd_ka_1/104-2088971-5001520?v=glance&amp;s=dvd&amp;n=507846</link> 
 </product> 
   
 [...] 
   
 </catalog> 

In this example, the XML structure is built up from XML elements without using attributes. It would also be possible to use attributes within the elements as shown in the example below.

HTML/XML
<product title="Pirates of the Caribbean - The Curse of the Black Pearl" category="DVD" manufacturer="Buena Vista Home Video" priceUSD="17.99" seller="amazon.com" sellerType="web" link="http://www.amazon.com/exec/obidos/tg/detail/-/B00005JM5E/qid=1075478699//ref=pd_ka_1/104-2088971-5001520?v=glance&s=dvd&n=507846"/>

Defining Classes

Now, you are going to model a class diagram that describes the data source file. The class diagram contains classes that represent the input and output structure of the service.

Output

You are going to start with the output classes according to the structure of the XML file. First, create a new service structure for the extended Web service.

Create a new package in the package Data / Services by choosing Package <<Respository>> from the context menu. Assign the name QueryProductsService:

To the newly created package QueryProductsService, the stereotype <<Repository>> has been applied automatically:

In package QueryProductsService, create the new packages Classes and Ports with stereotype <<Repository>> using the same menu option:

Create a new class in the package Data / Services / QueryProductsService / Classes that, as a query result, will store a list of products:

To find an appropriate name, look at the file structure of the XML file that was introduced at the beginning of this chapter. The first element in the file after the header is called catalog. You find the opening tag of catalog at the beginning of the XML structure, and the closing tag at the end of it. Choose the same name for the class and, as class names should begin with a capital letter, type Catalog. An instance of this class will serve as output object in order to return the query result:

Next, create a new class diagram in the package Classes to visualize the data structures:

Assign the name Product Query Classes:

Drag and drop the class Catalog onto the class diagram in the diagram pane:

Now, create a class to store the data of a single product. Select a Class icon from the diagram toolbar and place it on the diagram pane. Double-click the class symbol to open the class specification dialog.

To find an appropriate name, look at the file structure of the XML file again.

HTML/XML
<product> 
 <title>Pirates of the Caribbean - The Curse of the Black Pearl</title> 
 <category>DVD</category> 
 <manufacturer>Buena Vista Home Video</manufacturer> 
 <priceUSD>17.99</priceUSD> 
 <seller>amazon.com</seller> 
 <type>web</type> 
 <link>http://www.amazon.com/exec/obidos
 /tg/detail//B00005JM5E/qid=1075478699/- 
 /ref=pd_ka_1/104-2088971 
 -5001520?v=glance&s=dvd& 
 n=507846</link> 
 </product>

The element in the file that represents a single product, and is repeated eight times, is called product. You find the opening tag of product at the beginning of each XML structure, and the closing tag at the end of it.

Choose the same name for the class (Product). Add the name Product in the Specification dialog and click Close:

In order to map the XML structure to the data structure in the class diagram later on, you need to associate the class Product to the class Catalog:

XML data is mapped to UML classes using an Action Language operation. As a rule, XML elements are mapped to association ends in a class diagram.

An association is a relationship between instances of two classes. At least one of the association ends must have a name. They play a similar role as attribute names. Actually, all associations having an association end name can be represented as an attribute as well. Click the class Catalog and select the Association icon from the smart manipulation toolbar. If the association symbol is not visible on the toolbar, click the black arrow on the bottom of the toolbar to expand it to full length.

Draw the association from the class Catalog  to the class Product:

As in the XML file there are stored several products, all Product elements have to be stored in an array. The array can be defined by configuring the association between the classes Product and Catalog.
An association can be named, and the ends of an association (also called roles) can be adorned with role names, ownership indicators, multiplicity, visibility, and other properties.

Double-click the association to open the Association specification dialog:

Enter contains as name of the association, as a Catalog contains Products. This increases readability. The small arrow floating next to the association line indicates the reading direction:

Enter the name products in the section Role of Product (which represents the association end belonging to class Catalog):

The roles (or association ends) are regarded as attributes of the corresponding class. Assigning them names, they can be accessed within E2E Action Script (as you will see later on).
In the next step, you will define products as an array containing zero to infinite Products.

By choosing 0..* in the multiplicity field, you define that the new attribute products is an array. This array can have zero to infinite elements of type Product as a Catalog contains zero to infinite Products:

The array products must be defined as public. Public attributes can be read and modified in activities outside the class context:

In the section Role of Catalog (which belongs to class Product) specify multiplicity 1 as exactly one instance of Catalog can have an array containing 0..* Products:

In the association properties tree on the left, expand the node Roles and select role products:

As mentioned before, you will map the XML structure to the class structure, after reading the XML file containing the product catalog.

For a better readability of the model, you named the association end products, instead of product in singular. But actually, the name of the corresponding association end has to match the XML element's name to perform the mapping. On account of this, you have to specify the external name of products.

This is done by assigning the stereotype <<E2EAttribute>> to the association end products. Click into the field Applied Stereotype, and then on the small edit button 

to open a list of suggested stereotypes:

Filter the list by entering e2e into the text field and select the stereotype <<E2EAttribute>>. Click Apply to assign the stereotype:

Assigning the stereotype <<E2EAttribute>> led to the properties list being appended by several additional properties. They are tagged values belonging to the stereotype. If necessary, scroll down to the end of the properties list to see the additional properties:

Specify the external name of products as product, the corresponding name from the XML file. Click Close:

Now, the class diagram should look as shown below: One Catalog contains zero to infinite products. The external name of products is product. Association end products constitutes an attribute of class Catalog. It is an array that contains elements of type Product.

Comparing the association to the XML structure, you will notice the analogy when saying that instances of class Catalog contain Products. The data of all product XML elements will be mapped to the association end products (products is an array attribute of class Catalog).

Class attributes and association ends are different notations for the same issue. You could drag and drop the association end products to class catalog to change the notation:

In the next steps, you will map each XML element that is included in an XML element product to new attributes of the class Product. For instance, the XML element manufacturer will be mapped to attribute manufacturer of class Product. Double-click class Product to open the Specification Dialog and apply the stereotype <<XML>>:

Switch to the Attributes section and add the attributes listed in the table below using Create > Property <<XMLElement >>:

Attribute Name

Attribute Type

Visibility

Stereotype

title

String

public

<<XMLElement>>

category

String

public

<<XMLElement>>

manufacturer

String

public

<<XMLElement>>

priceUSD

Float

public

<<XMLElement>>

seller

String

public

<<XMLElement>>

type

String

public

<<XMLElement>>

link

String

public

<<XMLElement>>

You use stereotypes like <<XMLElement>> to control how XML data is mapped to UML classes. For instance, using this stereotype for class attribute manufacturer, the attribute does not need to be defined on an association end. You will find more detailed information about data mapping between XML documents and UML classes in the xUML Services Reference Guide.

To make the stereotype <<XMLElement>> visible on the diagram pane, right-click class Product and select Symbol Properties from the context menu:

In the search field start typing show a to filter the list. Set the parameter Show Attributes Stereotypes to true. Click Close:

If the option Show Attributes Stereotypes is not displayed, change the drop-down list on top of the dialog from Standard to All:

Now, your class diagram should look like this:

In more complex examples that require to map complex XML data to UML classes and vice versa, you would import an XML schema into the model. The E2E XSD Importer creates a UML model with all classes having all required stereotypes and associations.

You modeled the output data according to the structure of the XML file by creating the classes Catalog and Product. The XML elements of each product record were modeled as stereotyped attributes of class Product. Save the UML model.

Input

Now, define the structure of the input data. Create a new class in the package Data / Services / QueryProductsService / Classes that will contain the title and the search keywords.
Assign the name SearchParameters:

Double-click the class to open the class specification dialog and change to the tab Attributes. Now, define the attributes of the new class according to the input data. Add all attributes listed in the table below:

Attribute Name

Attribute Type

Multiplicity

Visibility

title

String

public

keywords

String

0..*

public

After all attributes have been defined, click Close:

Drag and drop the class SearchParameters onto the class diagram Product Query Classes:

Save the UML model.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.