Reading an XML file into a data source

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

How to read 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, located at <installdir> /data/ilog/cpl/schema/data.xsd.
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
 
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> . Also, <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>
   
Structural element that lets you specify the extremities of a link. It contains one <to> and one <from> subelement.
 
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.
Note
  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 IlpDefaultTypeConverter for more information.
  3. The javaClass attribute of the <attribute> element behaves like the javaClass attribute of the <defaultValue> element. See Attribute types.

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">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 source using the XML element <removeObject> :
<removeObject id="Link1"/>