Business Objects and Data Sources > Data Sources > Adding Business Objects from XML

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.

Reading an XML File into a Data Source

The following sample code reads an XML file into a data source.

How to Read in 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:

Incorrect Example
<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:

Correct Example
<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.

Table 3.1 Elements in an XML Data File 
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 

 
<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 
<updateState> 

 

 
Within an <updateObject> element, defines incremental updates to attribute objectState for predefined business objects. See Defining States in XML
<batch> 

 

 
<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 

 
initializeDefaultValue 
true 
This attribute is optional. When true, it initializes attributes to their default values.  

 
container 

 
<updateObject> 

 

 

 
id 

 
This attribute is mandatory.  

 
idClass 
java.lang.String 

 
container 

 
<removeObject> 

 

 
Removes an existing object. It should not contain any other data or elements.  

 
id 

 
This attribute is mandatory. 

 
idClass 
java.lang.String 

 
childrenToo 
true 
This attribute is optional. It specifies whether all the children of the object should be removed from the data source.  
<children> 
None 

 
<link> 

 

 

 
idClass 
java.lang.String 
<parent> 

 

 
Structural element. It contains the id of the parent object.  

 
idClass 
java.lang.String 
<child> 

 

 
Contains the ID of the child.  

 
idClass 
java.lang.String 
<to>  

 

 
Contains the id of the to extremity of a link.  

 
idClass 
java.lang.String 
<from>

 

 
Contains the id of the from extremity of a link.  

 
idClass 
java.lang.String 

Notes
  1. Whether the object referred to by the ID in <parent>, <child>, <to>, and <from> elements exists or not is not important. When the corresponding object is created, either with the <addObject> XML element or through a call to a data source method, the adapter that makes use of the structural information will perform any necessary action to connect the representation objects matching the business objects specified in the structural element.
  2. The supported Java classes are those handled by the type converter. See IlpTypeConverter and IltDefaultTypeConverter for more information.
  3. The javaClass attribute of the <attribute> element behaves like the javaClass attribute of the <defaultValue> element. See AttributeTypes.

XML File Samples

How to Define the Business Model

The following sample illustrates the use of the XML elements <cplData>, <classes>, <class> and <attribute>, which define the business model:

<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>
    <attribute>
      <name>site</name>
      <javaClass>java.lang.String</javaClass>
    </attribute>
    <attribute>
     <name>position</name>
     <javaClass>ilog.cpl.graphic.IlpPoint</javaClass>
    </attribute>
  </class>
  <class>
    <name>Domain</name>
    <superClass>NetworkElement</superClass>
  </class>
  <class>
    <name>LinkElement</name>
    <attribute>
      <name>name</name>
      <javaClass>java.lang.String</javaClass>
    </attribute>
  </class>
</classes>
</cplData>
How to Create a New Business Object

The following sample illustrates the use of the XML element <addObject> to create a new business object of the business class "Domain":

<addObject id="Domain1">
    <class>Domain</class>
    <attribute name="name">Domain 1</attribute>
    <attribute name="site">ILOG Gentilly</attribute>
    <attribute name="position" javaClass="ilog.cpl.graphic.IlpPoint">
      <x>100</x>  <y>100</y>
    </attribute>
</addObject>
How to Create Containment Relationships Between Business Objects

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".

<addObject id="Server1">
    <class>Server</class>
    <parent>Domain1</parent>
    <attribute name="name">S1</attribute>
    <attribute name="site">Montreuil</attribute>
    <attribute name="position" javaClass="ilog.cpl.graphic.IlpPoint">
      <x>100</x>  <y>50</y>
    </attribute>
</addObject>
How to Specify a Link Relationship

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".

<addObject id="Link1">
    <class>LinkElement</class>
    <attribute name="name">InterDomain</attribute>
    <link>
      <from>Domain1</from>
      <to>Domain2</to>
    </link>
</addObject>
How to Update the Values of an Object

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:

<updateObject id="Link1">
    <attribute name="name">Server1-Domain2</attribute>
    <link>
      <from>Server1</from>
      <to>Domain2</to>
    </link>
</updateObject>
How to Remove an Object from the Data Source

The following sample shows how to remove an object from the data soure using the XML element <removeObject>:

<removeObject id="Link1"/>

Writing the Data Source Content to XML

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.

Adding Predefined Business Objects

Almost all predefined business objects can be added to a data source. This is the case for:

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.