skip to main content
Maps > Programmer's documentation > Developing with the JViews Diagrammer SDK > Using and writing data models > Handling XML data files in Java
 
Handling XML data files in Java
Explains how the class IlvSDMEngine allows you to read, write, and transform XML data.
*Reading data
*Shows several methods of reading the data from an XML file.
*Writing data
*Shows several methods of writing the contents of the data model to an XML file.
*Reading and writing custom XML formats
*Describes how to read and write data using a custom XML format.
Reading data
There are several ways available to read an XML file:
*Call the method setXMLFile.
*Call the method readXML to read XML data from an input stream.
*Call the method readDOM to read data directly from a DOM tree.
Writing data
There are several ways available to write the contents of the data model to an XML file:
*Call the method writeXML, specifying the filename:
 
myEngine.writeXML("output.xml");
*Call the method writeXML to write XML data to an output stream.
*Call the method writeDOM to write data directly to a DOM tree.
Reading and writing custom XML formats
It is possible to read and write data in any XML format. To do this, you have two solutions:
*Transform the XML format to the Rogue Wave® JViews Diagrammer SDM format using XSLT transformations
*Write a subclass of the utility class IlvXMLConnector
Using XSLT
XSLT is the simplest solution for reading and writing custom XML formats, because it requires no Java™ coding. This solution is easier to apply when the structure of the custom XML format is relatively close to the required structure.
For documentation and resources on XSLT, see the W3C website (http://www.w3c.org). Proceed as follows:
1. Write two XSLT files: one that will convert an XML document from the custom format to the SDM format (for reading), and another one that will convert a document from the SDM format to the custom format (for writing).
2. Create an instance of the class IlvXSLConnector (a subclass of IlvXMLConnector), specifying the two XSLT files. To do this through the style sheet add a rule with the following structure:
 
XMLConnector{
    class : "ilog.views.sdm.util.IlvXSLConnector";
    inputTemplates : "url(custom2sdm.xsl)";
    outputTemplates : "url(sdm2custom.xsl)";
}
where the two XSLT files are custom2sdm.xsl (for reading), and sdm2custom.xsl for writing.
Note that the two XSLT files must be written so that no information is lost, that is, the two transformations should be the exact inverse of one another. If the custom XML format contains information that is not useful for Rogue Wave JViews Diagrammer purposes or that cannot be translated easily, this information can be stored as metadata (see Metadata in the XML data file in Using the Designer).
It is possible to specify only one transformation. For example, you may specify only the input templates file if you read only custom XML files, but never modify and write them back.
Subclassing the XML connector
There may be cases when you will not be able to write XSL transformations between the custom XML format and the SDM format, usually because the structures are too different and the XSLT language does not allow you to implement the transformation.
In such cases, proceed as follows:
1. Write a subclass of IlvXMLConnector.
2. Create an instance of your custom XML connector in the style sheet, by writing a rule with the following structure:
 
XMLConnector{
    class : "my.package.MyConnector";
}

Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.