DEIMOS
Earth Observation Mission CFI Software
Usage Guide for Object Oriented Software
ESA

WRITING XML FILES

The XmlFile class in the FileHandling library can be used to write or modify XML files:

Writing a new file

In order to create an XML file, the following calling sequence should be followed:


Modifying an existing file

It is possible to modify an existing XML file and save to disk with the same name or a different one. The following calling sequence should be followed:


Adding elements in an XML file

Elements may be added using as input:

The following possibilities are available:

After a new element has been added to the in-memory representation of the XML file, the iterator is pointing to the last added element. Therefore, relative XPath expressions used just after an element insertion are relative to the inserted element.

Removing elements in an XML file

Existing elements are removed using XmlFile::removeNode with an XPath expression addressing them. If the requested element has children, all of them will be removed recursively.

Setting and/or modifying element values

When elements and attributes are created, they have no value. Element and attribute values may be set and/or modified with XmlFile::setValue (overcharged methods using an XPath expression for addressing the requested element or attribute).


Example

// ------------------------------------------------------------------------
//
//                           JAVA Example Program.
//                           WRITING XML FILES: Random reading
//
// ------------------------------------------------------------------------

import EECFI.*;

import java.util.Vector;
import java.lang.String;
import java.lang.Long;

// Main program
public class Example
{
  public static void main(String[] args)
  {
    //-------------------------------------------------
    // Put all the code inside try-catch statements
    // so, if something fails, an exception is thrown
    //-------------------------------------------------
    
    try
    {
      XmlFile myXml = new XmlFile();
      String writtenXmlFile = new String("output_file.xml");
      
      
      //The name to the file is given when we write it to disk
      myXml.create();
      myXml.createRoot("Earth_Explorer_File");
      
      // Creating a child after another 
      myXml.addChild( ".", "Data_Block");
      myXml.addChild( ".", "List_of_Orbit_Changes");
      myXml.addChild( ".", "Orbit_Change");
      myXml.addChild( ".", "Orbit");
      myXml.addChild( ".", "Absolute_Orbit");
      
      // Adding a value to the current node 
      myXml.setValue( ".", "10", "%s");
      
      // Adding a sibling next to the current node, and then its value 
      // Creating and setting value can be done in 2 steps...
      myXml.addNext( ".", "Relative_Orbit");
      myXml.setValue( ".", "15", "%s");
      
      //... or only in one
      myXml.addChild( "..", "Phase_Number", "1", "%s");
      
      // Adding a sibling before the current node, and then its value 
      myXml.addPrevious( ".", "Cycle_Number", "1", "%s");
      
      // Another child, with a different path 
      myXml.addChild( "/Earth_Explorer_File/Data_Block/List_of_Orbit_Changes/Orbit_Change[1]", "Cycle");
      
      myXml.addChild( ".", "Repeat_Cycle", "3", "%s");
      myXml.addAttribute( ".", "units", "day", "%s");
      myXml.addNext( ".", "Cycle_Length");
      
      // Adding an attribute to the current node, and then its value 
      myXml.addAttribute( ".", "unit");
      myXml.setValue( "@unit", "orbit", "%s");
      
      myXml.setValue ( ".", "43" , "%s");
      myXml.addNext( ".", "ANX_Longitude");
      
      // Adding a value as double. Please, pay attention to the format
      myXml.setValue( ".", 0.1335, "%06.4lf" );
      myXml.addAttribute( ".", "unit", "deg", "%s");
      
      // Removing an attribute, and then the current node 
      myXml.removeNode( "@unit" );
      myXml.removeNode( "../Repeat_Cycle" );
      
      myXml.addPrevious( "Cycle_Length", "Repeat_Cycle");
      
      // Adding a value as long. Please, pay attention to the format
      myXml.setValue( ".", 5L, "%ld" );
      myXml.setValue( "@unit", "day", "%s");
      myXml.setValue( "../Cycle_Length", "57", "%s");
      myXml.addChild( "..",  "MLST", "22:00:00.45545", "%s");
      myXml.addNext( ".", "MLST_Drift", "011.010101", "%s" );
      myXml.addChild( "../..", "Time_of_ANX");
      myXml.addChild( ".", "TAI_of_ANX", "TAI=2002-03-01T03:01:09.01345", "%s" );
      myXml.addNext( ".", "UTC_of_ANX", "UTC=2002-03-01T03:01:10.01345", "%s" );
      myXml.addNext( ".", "UT1_of_ANX", "UT1=2002-03-01T03:01:11.01345", "%s"); 
      
      //Write XML file to disk
      myXml.write( writtenXmlFile );
      
    } //end try
    catch (CfiError cfiError)
    {
      //If an exception is thrown, there have been errors during execution
      Vector<String> errorMessages = new Vector<String>();
      
      // Get error messages
      errorMessages = cfiError.getMsg(errorMessages);
      
      // Pring Error messages
      for (int i = 0; i < errorMessages.size(); i++)
      {
        System.out.println( errorMessages.elementAt(i) );
      }
    }
  }
  
}

Generated on Mon Dec 11 2023 14:48:38 for by  doxygen 1.7.1