| Business Objects and Data Sources > Data Sources > Adding Business Objects from XML |
Adding Business Objects from XML |
INDEX
PREVIOUS
NEXT
|
The default implementation of the data source can create IlpObject instances from XML files. These XML files can also contain dynamic class definitions that will be added as IlpClass instances to the class manager of the data source context. For details about the XML schema used to describe dynamic classes, see Defining a Dynamic Class in XML.
The following sample code reads an XML file into a data source.
try {
dataSource.parse("XMLFileExample.xml");
} catch (Exception e) {
e.printStackTrace();
}
Following is an example of an XML file.
<cplData xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "ilog/cpl/schema/data.xsd"> <cplData> <classes> <class> <name>Alarm</name> <attribute> <name>ID</name> <javaClass>java.lang.String</javaClass> </attribute> <attribute> <name>severity</name> <javaClass>itest.table.datasourcexmltable.MainFrame$Severity</javaClass> <defaultValue>Warning</defaultValue> </attribute> <attribute> <name>acknowledged</name> <javaClass>java.lang.Boolean</javaClass> <defaultValue>false</defaultValue> </attribute> </class> </classes> <addObject id="alarm1" initializeDefaultValue="true"> <class>Alarm</class> <attribute name="ID">alarm1</attribute> </addObject> <addObject id="alarm2" initializeDefaultValue="false"> <class>Alarm</class> <attribute name="ID">alarm2</attribute> <attribute name="severity">Major</attribute> <attribute name="acknowledged">false</attribute> </addObject > <addObject id="alarm3" initializeDefaultValue="false"> <class>Alarm</class> <attribute name="ID">alarm3</attribute> <attribute name="severity">Major</attribute> <attribute name="acknowledged">true</attribute> </addObject > <addObject id="alarm4" initializeDefaultValue="false"> <class>Alarm</class> <attribute name="ID">alarm4</attribute> </addObject > <updateObject id="alarm2"> <attribute name="acknowledged">true</attribute> </updateObject> </cplData>
In this example, the <classes> XML element delimits the business model definition. This model can also be defined in a separate file. For details, see Defining a Dynamic Class in XML.
When defining your business model in a data source file, please note that the business class inheritance (defined through the tag "superClass" ) is resolved at the end of each <classes> element. As a consequence, you cannot define within the first <classes> element a class that inherits from a class defined within another <classes> element later in the file. For example, the following model definition is incorrect:
<cplData xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "ilog/cpl/schema/data.xsd">
<classes>
<class>
<name>Domain</name>
<superClass>NetworkElement</superClass>
</class>
</classes>
<classes>
<class>
<name>NetworkElement</name>
<attribute>
<name>name</name>
<javaClass>java.lang.String</javaClass>
</attribute>
</class>
</classes>
</cplData>
To get a correct business model definition, you can either define the whole business class hierarchy inside the same <classes> element as follows:
<cplData xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "ilog/cpl/schema/data.xsd">
<classes>
<class>
<name>NetworkElement</name>
<attribute>
<name>name</name>
<javaClass>java.lang.String</javaClass>
</attribute>
</class>
<class>
<name>Domain</name>
<superClass>NetworkElement</superClass>
</class>
</classes>
</cplData>
or, make sure that the business class needed is already present in the class manager of the current application context. To do so, you load a separate business model XML file directly in the class manager. Please refer to Class Manager in the Application Context and Deployment Descriptor documentation.
The following table describes the elements that you can use to define actions to be performed on the business model data in XML format. Mandatory attributes appear in boldface. You can also find a description of this format in the XML schema file data.xsd.
XML Elements |
Attributes |
Default |
Description |
|---|---|---|---|
<cplData> |
None |
Delimits data definition. This element is required. The file containing XML business data should necessarily start and end with this element. | |
<classes> |
None |
Delimits the model definition, that is, dynamic class definition. For details, see Defining a Dynamic Class in XML. | |
<class> |
None |
Within an <updateState> element, defines the class of the added object. | |
<attribute> |
Within an <updateState> or <updateObject> element, defines the values of the attributes. | ||
name |
This attribute is mandatory. The name of the attribute as defined in the object class. | ||
null | false |
This attribute is optional. When true, it indicates that the value of the attribute is null. | |
javaClass |
The attribute Java class as defined in the IlpClass |
This attribute is optional. It specifies the Java class name of the attribute value. It becomes mandatory if the attribute value class is an abstract class. See Note 3. | |
<updateState> |
Within an <updateObject> element, defines incremental updates to attribute objectState for predefined business objects. See Defining States in XML. | ||
<batch> |
Group data source updates. This element may contain any number of <updateState>, <removeObject>, <updateObject>. <batch> elements can be nested. | ||
<addObject> |
Adds an object to the data source. This element contains a mandatory <class> element. It may contain structural elements and <attribute> elements. | ||
id |
This attribute is mandatory and should be unique. | ||
idClass | java.lang.String |
This attribute is optional. It can be used to specify a Java class name for the id. See Note 2. | |
initializeDefaultValue | true |
This attribute is optional. When true, it initializes attributes to their default values. | |
container |
This attribute is optional. It allows you to specify whether the object is a container. An object is considered to be a container if this attribute is set to true, if it contains <children>, or if another object is declared as its <parent>. | ||
<updateObject> |
Updates the value of an existing object. It may contain structural elements, <attribute> and <updateState> elements. | ||
id |
This attribute is mandatory. | ||
idClass | java.lang.String |
This attribute is optional. It can be used to specify a Java class name for the id. See Note 2. | |
container |
This attribute is optional. It allows you to specify whether the object is a container. An object is considered to be a container if this attribute is set to true, if it contains <children>, or if another object is declared as its <parent>. | ||
<removeObject> |
Removes an existing object. It should not contain any other data or elements. | ||
id |
This attribute is mandatory. | ||
idClass | java.lang.String |
This attribute is optional. It can be used to specify a Java class name for the id. See Note 2. | |
childrenToo | true |
This attribute is optional. It specifies whether all the children of the object should be removed from the data source. | |
<children> |
None |
Structural element indicating that an object is a potential container, even if <children> does not contain any other subelements. This element can contain <child> subelements. | |
<link> | |||
idClass | java.lang.String |
This attribute is optional. It can be used to specify a Java class name for the id. See Note 2. | |
<parent>
See Note 1 |
Structural element. It contains the id of the parent object. | ||
idClass | java.lang.String |
This attribute is optional. It can be used to specify a Java class name for the id. See Note 2. | |
<child>
See Note 1 |
Contains the ID of the child. | ||
idClass | java.lang.String |
This attribute is optional. It can be used to specify a Java class name for the id. See Note 2. | |
<to>
See Note 1 |
Contains the id of the to extremity of a link. | ||
idClass | java.lang.String |
This attribute is optional. It can be used to specify a Java class name for the id. See Note 2. | |
<from>.
See Note 1 |
Contains the id of the from extremity of a link. | ||
idClass | java.lang.String |
This attribute is optional. It can be used to specify a Java class name for the id. See Note 2. |
| Notes |
|
The following sample illustrates the use of the XML elements <cplData>, <classes>, <class> and <attribute>, which define the business model:
The following sample illustrates the use of the XML element <addObject> to create a new business object of the business class "Domain":
The following sample shows how to create containment relationships between business objects using the XML element <parent>. The example creates a new business object "Server1" which is a child of business object "Domain1".
The following sample shows how to specify a link relationship using the XML elements <link>, <to> and <from>. In this example, you create the link "Link1" which connects two objects "Domain1" and "Domain2".
The following sample shows how to update values of an existing object using the XML element <updateObject>. You can update simple attributes, such as "name", and you can also change structural information such as changing the end point of the link:
The following sample shows how to remove an object from the data soure using the XML element <removeObject>:
You can also write the content of the data source to an XML file like this:
datasource.output("network.xml");
You can also use a java.io.Writer. The following example prints the data source to the default output:
Writer writer = new PrintWriter(System.out); dataSource.output(writer); writer.flush();
See also Advanced Parsing and Writing of a Data Source for details on more advanced functionality.
Almost all predefined business objects can be added to a data source. This is the case for:
(See Loading an Alarm Defined in XML.)
(See Loading a Link Defined in XML.)
(See Loading a Group Defined in XML.)
(See Loading a Network Element Defined in XML.)
(See Loading a Shelf Defined in XML.)
(See Loading a Card Defined in XML.)
Refer to section Introducing Business Objects and Data Sources in this documentation for an overview of these objects.
For information on how to add these objects to a data source through XML or through the API, refer to the corresponding sections further in this documentation.
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. All rights reserved. Legal terms. | PREVIOUS NEXT |