com.ricebridge.xmlman
Class XmlManager

java.lang.Object
  extended bycom.ricebridge.xmlman.XmlManager

public class XmlManager
extends Object

This is the main class for loading XML files.

Quick Start:

To load an XML file from disk as a List of String[] arrays:

    XmlManager xmlman  = new XmlManager();
    List       records = xmlman.load( "/path/to/file.xml", 
      new RecordSpec( "/xpath/to/record/element",
        new String[] { "xpath/for/field-one", 
                       "xpath/for/field-two",
                       "xpath/for/field-three" } ) );

    for( int rI = 0; rI < records.size(); rI++ ) {
      String[] record = (String[]) records.get( rI );
      // do stuff with record
    }
  
The best way to learn how to use XML Manager is to start with the Getting Started guide, which shows you the most important details, and has lots of example code.

Loading XML Data

The XmlManager class provides the following ways to load XML data:

Saving XML Data

The XmlManager class also provides the following ways to save XML data:

Input

XML Data can be read from the following types of input:

All the load methods accept these input types as their first parameter. Note that for ease-of-use, a String parameter is normally considered to be a file path (for example, load(String,RecordSpec). Use the loadFromString methods to load XML content contained in String variables.

Output

XML Data can be written to the following types of output:

All the save methods accept these output types as their first parameter. Note that for ease-of-use, a String parameter is normally considered to be a file path (for example, save(String,RecordSpec,List). Use the saveToString methods to save data into a String variable as XML content.

How to Find the Method You Need

XmlManager provides many loading and saving shortcut methods, but it's still easy to find the method you need. Just think of two things: where does the XML data come from, and what form do you want it in? For example, if the data comes from a File and you want it as a List, use load(File,RecordSpec). Or if it comes from an InputStream and you want it as a TableModel then use loadTableModel(InputStream,RecordSpec).

To save XML data, apply the same questions; where does the data go and what form is it in now? For example, if you have the data as a ResultSet and you want to save it to a File use saveResultSet(File,RecordSpec,ResultSet).

More formally, the loading methods are organised according to the following pattern:
[type] load[type]( [source], RecordSpec )
Where [type] is one of

and [source] is one of

When [source] is a String containing XML data as text, append FromString to the method name, like so: loadResultSetFromString(String,RecordSpec). This prevents the String data from being interpreted as a file path.

When [source] is a URI indicating the location of the XML content append FromURI to the method name, like so: loadAsListsFromURI(String,RecordSpec).

When loading Java Beans, you will also need to include a BeanSpec parameter, see loadBeans(File,RecordSpec,BeanSpec) for an example.

The saving methods are organised according to the following pattern:
save[type]( [destination], [data] )
Where [type] is one of

and [destination] is one of and [data] is an object of type

When you want to save XML data to a String variable append ToString to the method name, like so: saveToString(RecordSpec,List).

When saving Java Beans, you will also need to include a BeanSpec parameter, see saveBeans(File,RecordSpec,BeanSpec,List) for an example.

Using RecordSpecs

A RecordSpec is a record specification class. This is an immutable class that specifies the XPath expression for the data record, and the XPath expressions for each data field.

Throughout this API documentation, we use a standard reference block of XML for most of the examples. This block of XML looks like so:

  <?xml version='1.0' encoding='UTF-8'?>
  <root>
    <record name="r1">
      <foo>f1</foo>
      <bar>b1</bar>
    </record>
    <record name="r2">
      <foo>f2</foo>
      <bar>b2</bar>
    </record>
    <record name="r3">
      <foo>f3</foo>
      <bar>b3</bar>
    </record>
  </root>
  

If we apply the XPath expression /root/record to this XML, we select all the record elements. For each of these elements, if we further apply the XPath expressions @name and foo (with each record element as the context node), we get the values r1 and f1 for the first record, the values values r2 and f2 for the second record, and finally r3 and f3 for the last record.

The RecordSpec class allows us to specify this set of XPath expressions, like so:

  RecordSpec rs = new RecordSpec( "/root/record", new String[] {"@name", "foo"} );
  

This declaration creates a new RecordSpec, with the first parameter, a String specifying the record XPath expression, and the second parameter, a String[] array, specifying the list of field XPath expressions.

XML Manager uses RecordSpecs both for reading and writing XML, and thus all save and load methods require access to a RecordSpec object. You can either supply this directly as a method parameter, as with the method load(File,RecordSpec), or you can use a prepared RecordSpec, as with the method load(File)

Prepared RecordSpecs

It is often the case that you need to apply the same RecordSpec many times to multiple input files. It's wasteful of computing resources to constantly reparse the XPath expressions, so XML Manager allows you to prepare a RecordSpec for subsequent repeated use. You can prepare a RecordSpec as follows:

  RecordSpec rs = new RecordSpec( "/root/record", new String[] {"@name", "foo"} );

  // prepared via the XmlManager constructor
  XmlManager xmlman = new XmlManager( rs );
  
  // OR prepared via the setRecordSpec method
  xmlman.setRecordSpec( rs );
  

Once you have prepared a RecordSpec, you can then use the load and save methods that do not have a RecordSpec parameter, such as load(File) and save(File,List). For all the load and save methods that take a RecordSpec parameter, there are equivalent methods that do not require a RecordSpec. See the setRecordSpec(RecordSpec) method for more details.

You can call the load and save methods repeatedly, and the prepared RecordSpec is always used. XML Manager has been carefully designed to be thread-safe, so you can use prepared RecordSpecs in multi-threaded applications very easily. You can call the load and save methods on the same XmlManager instance in more than one thread, and everything will still work just fine.

Saving Data with RecordSpecs

The XPath expressions in a RecordSpec are used for both loading and saving XML data. When loading, the XPath expressions specify the element paths to follow when extracting data. XPath predicates (record[@name='r1']) can also be used. When saving, the XPath expressions define the exact format of the output XML. Predicates are ignored and so is the // syntax. What you get is direct translation of the XPath element steps into XML elements. Thus /root/record will create a root element containing record elements.

Callback Interfaces

If the predefined methods of input and output are not suitable, you can implement your own access to your XML data. The RecordListener interface can be used via the load(*,RecordSpec,RecordListener) methods to get direct access to the XML data as it is loaded. Using an implementation of this interface means that you can load XML data and place it in any data structure that you require.

If you want to provide data to save in a XML format, you can use the RecordProvider interface to provide the data by calling the save(*,RecordSpec,RecordProvider) methods.

Please note: to use the RecordListener interface, you should extend the support class RecordListenerSupport, as this will provide you with future-compatibility and error-handling support. The RecordProviderSupport support class is also provided for this purpose.

Options for XML format

For a full description of all the available options, refer to the XmlSpec class. The options used by XmlManager are stored in a XmlSpec object and you can access the current XmlSpec with getXmlSpec().

Frequently used options:

Background Processing

It is possible to perform the loading and saving operations in a background Thread. To do this, set XmlSpec.setBackground(boolean) to true. This creates a daemon Thread (which halts automatically if the main Thread halts) where the loading or saving operation is performed. You can call the isLoadFinished() and isSaveFinished() methods to monitor the process.

Statistics

XML Manager provides a number of statistics on the loading and saving process. To access these statistics, call the getStats() method to get the current Stats object. This object provides the following information:

For a quick summary of these statistics, use the Stats.getSummary() method.

Error Messages and Exception Handling

Normally an XmlManagerException is thrown as soon as an error occurs, and processing halts. If you set XmlSpec.setIgnoreBadRecords(boolean) to true then errors are collected and processing continues until all data records are processed. This allows you to load as much valid data as possible. Note that an XML syntax error always causes processing to halt, as per the XML Specification. You can get the collected error messages (as BadRecord objects) from the getBadRecords() method.

When an error occurs this class throws a XmlManagerException which is a subclass of RuntimeException Since RuntimeExceptions need not be declared, none of the methods in this class declare that that they throw any Exceptions. You can choose to catch these XmlManagerExceptions directly or let them pass up to other exception handling within your code. Each XmlManagerException contains an explanation of the error and a set of error values which give more information about the cause of the error. Calling XmlManagerException.toString() or XmlManagerException.getUserMessage() on a XmlManagerException will return a user-friendly description of the problem. Calling XmlManagerException.getMessage() will return a technical description of the problem, suitable for debugging purposes. See XmlManagerException for a more detailed description of how to work with XmlManagerExceptions.

See Also:
RecordSpec, XmlSpec, Stats, XmlManagerException

Constructor Summary
XmlManager()
          Create a new instance of XmlManager, with default settings.
XmlManager(RecordSpec pRecordSpec)
          Create a new instance of XmlManager using the specified RecordSpec data record specification.
XmlManager(XmlSpec pXmlSpec)
          Create a new instance of XmlManager using the specified XmlSpec XML format specification.
XmlManager(XmlSpec pXmlSpec, RecordSpec pRecordSpec)
          Create a new instance of XmlManager using the specified XmlSpec XML format and RecordSpec data record specification.
 
Method Summary
 void finishSave()
          Special method to complete final XML elements of streamed documents.
 List getBadRecords()
          Get a list of descriptions of any semantically incorrect data records encountered.
 Stats getStats()
          Get statistics on the number of records processed and processing time.
 XmlManagerStore getXmlManagerStore()
          XmlManagerStore is a utility class for creating RecordListener and RecordProvider instances.
 XmlSpec getXmlSpec()
          Get the current XmlSpec, which specifies the settings for the interpretation and generation of XML.
 boolean isLoadFinished()
          Returns true when the loading process is finished.
 boolean isSaveFinished()
          Returns true when the saving process is finished.
 List load(File pXmlFile)
          Load XML data from a file as a List of String[] arrays, using the default RecordSpec.
 void load(File pXmlFile, RecordListener pRecordListener)
          Load XML data from a file as a stream using your own RecordListener, using the default RecordSpec.
 List load(File pXmlFile, RecordSpec pRecordSpec)
          Load XML data from a file as a List of String[] arrays.
 void load(File pXmlFile, RecordSpec pRecordSpec, RecordListener pRecordListener)
          Load XML data from a file as a stream using your own RecordListener.
 List load(InputSource pInputSource)
          Load XML data from an InputSource as a List of String[] arrays, using the default RecordSpec.
 void load(InputSource pInputSource, RecordListener pRecordListener)
          Load XML data from an InputSource as a stream using your own RecordListener, using the default RecordSpec.
 List load(InputSource pInputSource, RecordSpec pRecordSpec)
          Load XML data from an InputSource as a List of String[] arrays.
 void load(InputSource pInputSource, RecordSpec pRecordSpec, RecordListener pRecordListener)
          Load XML data from an InputSource as a stream using your own RecordListener.
 List load(InputStream pInputStream)
          Load XML data from an InputStream as a List of String[] arrays, using the default RecordSpec.
 void load(InputStream pInputStream, RecordListener pRecordListener)
          Load XML data from an InputStream as a stream using your own RecordListener, using the default RecordSpec.
 List load(InputStream pInputStream, RecordSpec pRecordSpec)
          Load XML data from an InputStream as a List of String[] arrays.
 void load(InputStream pInputStream, RecordSpec pRecordSpec, RecordListener pRecordListener)
          Load XML data from an InputStream as a stream using your own RecordListener.
 List load(String pXmlFilePath)
          Load XML data from a specified file path as a List of String[] arrays, using the default RecordSpec.
 void load(String pXmlFilePath, RecordListener pRecordListener)
          Load XML data from a specified file path as a stream using your own RecordListener, using the default RecordSpec.
 List load(String pXmlFilePath, RecordSpec pRecordSpec)
          Load XML data from a specified file path as a List of String[] arrays.
 void load(String pXmlFilePath, RecordSpec pRecordSpec, RecordListener pRecordListener)
          Load XML data from a specified file path as a stream using your own RecordListener.
 List loadAsLists(File pXmlFile)
          Load XML data from a file as a List of Lists of Strings, using the default RecordSpec.
 List loadAsLists(File pXmlFile, RecordSpec pRecordSpec)
          Load XML data from a file as a List of Lists of Strings.
 List loadAsLists(InputSource pInputSource)
          Load XML data from an InputSource as a List of Lists of Strings, using the default RecordSpec.
 List loadAsLists(InputSource pInputSource, RecordSpec pRecordSpec)
          Load XML data from an InputSource as a List of Lists of Strings.
 List loadAsLists(InputStream pInputStream)
          Load XML data from an InputStream as a List of Lists of Strings, using the default RecordSpec.
 List loadAsLists(InputStream pInputStream, RecordSpec pRecordSpec)
          Load XML data from an InputStream as a List of Lists of Strings.
 List loadAsLists(String pXmlFilePath)
          Load XML data from a specified file path as a List of Lists of Strings, using the default RecordSpec.
 List loadAsLists(String pXmlFilePath, RecordSpec pRecordSpec)
          Load XML data from a specified file path as a List of Lists of Strings.
 List loadAsListsFromString(String pXmlString)
          Load XML data from a String variable as a List of Lists of Strings, using the default RecordSpec.
 List loadAsListsFromString(String pXmlString, RecordSpec pRecordSpec)
          Load XML data from a String variable as a List of Lists of Strings.
 List loadAsListsFromURI(String pURI)
          Load XML data specified by a Uniform Resource Identifier (URI) as a List of Lists of Strings, using the default RecordSpec.
 List loadAsListsFromURI(String pURI, RecordSpec pRecordSpec)
          Load XML data specified by a Uniform Resource Identifier (URI) as a List of Lists of Strings.
 List loadBeans(File pXmlFile, BeanSpec pBeanSpec)
          Load XML data from a file as a List of Java Beans, using the default RecordSpec.
 List loadBeans(File pXmlFile, RecordSpec pRecordSpec, BeanSpec pBeanSpec)
          Load XML data from a file as a List of Java Beans.
 List loadBeans(InputSource pInputSource, BeanSpec pBeanSpec)
          Load XML data from an InputSource as a List of Java Beans, using the default RecordSpec.
 List loadBeans(InputSource pInputSource, RecordSpec pRecordSpec, BeanSpec pBeanSpec)
          Load XML data from an InputSource as a List of Java Beans.
 List loadBeans(InputStream pInputStream, BeanSpec pBeanSpec)
          Load XML data from an InputStream as a List of Java Beans, using the default RecordSpec.
 List loadBeans(InputStream pInputStream, RecordSpec pRecordSpec, BeanSpec pBeanSpec)
          Load XML data from an InputStream as a List of Java Beans.
 List loadBeans(String pXmlFilePath, BeanSpec pBeanSpec)
          Load XML data from a specified file path as a List of Java Beans, using the default RecordSpec.
 List loadBeans(String pXmlFilePath, RecordSpec pRecordSpec, BeanSpec pBeanSpec)
          Load XML data from a specified file path as a List of Java Beans.
 List loadBeansFromString(String pXmlString, BeanSpec pBeanSpec)
          Load XML data from a String variable as a List of Java Beans, using the default RecordSpec.
 List loadBeansFromString(String pXmlString, RecordSpec pRecordSpec, BeanSpec pBeanSpec)
          Load XML data from a String variable as a List of Java Beans.
 List loadBeansFromURI(String pURI, BeanSpec pBeanSpec)
          Load XML data specified by a Uniform Resource Identifier (URI) as a List of Java Beans, using the default RecordSpec.
 List loadBeansFromURI(String pURI, RecordSpec pRecordSpec, BeanSpec pBeanSpec)
          Load XML data specified by a Uniform Resource Identifier (URI) as a List of Java Beans.
 List loadFromString(String pXmlString)
          Load XML data from a String variable as a List of String[] arrays, using the default RecordSpec.
 void loadFromString(String pXmlString, RecordListener pRecordListener)
          Load XML data from a String variable as a stream using your own RecordListener, using the default RecordSpec.
 List loadFromString(String pXmlString, RecordSpec pRecordSpec)
          Load XML data from a String variable as a List of String[] arrays.
 void loadFromString(String pXmlString, RecordSpec pRecordSpec, RecordListener pRecordListener)
          Load XML data from a String variable as a stream using your own RecordListener.
 List loadFromURI(String pURI)
          Load XML data specified by a Uniform Resource Identifier (URI) as a List of String[] arrays, using the default RecordSpec.
 void loadFromURI(String pURI, RecordListener pRecordListener)
          Load XML data specified by a Uniform Resource Identifier (URI) as a stream using your own RecordListener, using the default RecordSpec.
 List loadFromURI(String pURI, RecordSpec pRecordSpec)
          Load XML data specified by a Uniform Resource Identifier (URI) as a List of String[] arrays.
 void loadFromURI(String pURI, RecordSpec pRecordSpec, RecordListener pRecordListener)
          Load XML data specified by a Uniform Resource Identifier (URI) as a stream using your own RecordListener.
 ResultSet loadResultSet(File pXmlFile)
          Load XML data from a file as a ResultSet, using the default RecordSpec.
 ResultSet loadResultSet(File pXmlFile, RecordSpec pRecordSpec)
          Load XML data from a file as a ResultSet.
 ResultSet loadResultSet(InputSource pInputSource)
          Load XML data from an InputSource as a ResultSet, using the default RecordSpec.
 ResultSet loadResultSet(InputSource pInputSource, RecordSpec pRecordSpec)
          Load XML data from an InputSource as a ResultSet.
 ResultSet loadResultSet(InputStream pInputStream)
          Load XML data from an InputStream as a ResultSet, using the default RecordSpec.
 ResultSet loadResultSet(InputStream pInputStream, RecordSpec pRecordSpec)
          Load XML data from an InputStream as a ResultSet.
 ResultSet loadResultSet(String pXmlFilePath)
          Load XML data from a specified file path as a ResultSet, using the default RecordSpec.
 ResultSet loadResultSet(String pXmlFilePath, RecordSpec pRecordSpec)
          Load XML data from a specified file path as a ResultSet.
 ResultSet loadResultSetFromString(String pXmlString)
          Load XML data from a String variable as a ResultSet, using the default RecordSpec.
 ResultSet loadResultSetFromString(String pXmlString, RecordSpec pRecordSpec)
          Load XML data from a String variable as a ResultSet.
 ResultSet loadResultSetFromURI(String pURI)
          Load XML data specified by a Uniform Resource Identifier (URI) as a ResultSet, using the default RecordSpec.
 ResultSet loadResultSetFromURI(String pURI, RecordSpec pRecordSpec)
          Load XML data specified by a Uniform Resource Identifier (URI) as a ResultSet.
 TableModel loadTableModel(File pXmlFile)
          Load XML data from a file as a TableModel, using the default RecordSpec.
 TableModel loadTableModel(File pXmlFile, RecordSpec pRecordSpec)
          Load XML data from a file as a TableModel.
 TableModel loadTableModel(InputSource pInputSource)
          Load XML data from an InputSource as a TableModel, using the default RecordSpec.
 TableModel loadTableModel(InputSource pInputSource, RecordSpec pRecordSpec)
          Load XML data from an InputSource as a TableModel.
 TableModel loadTableModel(InputStream pInputStream)
          Load XML data from an InputStream as a TableModel, using the default RecordSpec.
 TableModel loadTableModel(InputStream pInputStream, RecordSpec pRecordSpec)
          Load XML data from an InputStream as a TableModel.
 TableModel loadTableModel(String pXmlFilePath)
          Load XML data from a specified file path as a TableModel, using the default RecordSpec.
 TableModel loadTableModel(String pXmlFilePath, RecordSpec pRecordSpec)
          Load XML data from a specified file path as a TableModel.
 TableModel loadTableModelFromString(String pXmlString)
          Load XML data from a String variable as a TableModel, using the default RecordSpec.
 TableModel loadTableModelFromString(String pXmlString, RecordSpec pRecordSpec)
          Load XML data from a String variable as a TableModel.
 TableModel loadTableModelFromURI(String pURI)
          Load XML data specified by a Uniform Resource Identifier (URI) as a TableModel, using the default RecordSpec.
 TableModel loadTableModelFromURI(String pURI, RecordSpec pRecordSpec)
          Load XML data specified by a Uniform Resource Identifier (URI) as a TableModel.
 RecordSpec nullcheck(RecordSpec pRecordSpec)
          Check that RecordSpec is not null.
 void save(File pXmlFile, List pData)
          Save XML data to a file from a List of String[] arrays, using the default RecordSpec.
 void save(File pXmlFile, RecordProvider pRecordProvider)
          Save XML data to a file using your own RecordProvider, using the default RecordSpec.
 void save(File pXmlFile, RecordSpec pRecordSpec, List pData)
          Save XML data to a file from a List of String[] arrays.
 void save(File pXmlFile, RecordSpec pRecordSpec, RecordProvider pRecordProvider)
          Save XML data to a file using your own RecordProvider.
 void save(OutputStream pOutputStream, List pData)
          Save XML data to an OutputStream from a List of String[] arrays, using the default RecordSpec.
 void save(OutputStream pOutputStream, RecordProvider pRecordProvider)
          Save XML data to an OutputStream using your own RecordProvider, using the default RecordSpec.
 void save(OutputStream pOutputStream, RecordSpec pRecordSpec, List pData)
          Save XML data to an OutputStream from a List of String[] arrays.
 void save(OutputStream pOutputStream, RecordSpec pRecordSpec, RecordProvider pRecordProvider)
          Save XML data to an OutputStream using your own RecordProvider.
 void save(String pXmlFilePath, List pData)
          Save XML data to a specified file path from a List of String[] arrays, using the default RecordSpec.
 void save(String pXmlFilePath, RecordProvider pRecordProvider)
          Save XML data to a specified file path using your own RecordProvider, using the default RecordSpec.
 void save(String pXmlFilePath, RecordSpec pRecordSpec, List pData)
          Save XML data to a specified file path from a List of String[] arrays.
 void save(String pXmlFilePath, RecordSpec pRecordSpec, RecordProvider pRecordProvider)
          Save XML data to a specified file path using your own RecordProvider.
 void saveAsLists(File pXmlFile, List pData)
          Save XML data to a file from a List of Lists of Strings, using the default RecordSpec.
 void saveAsLists(File pXmlFile, RecordSpec pRecordSpec, List pData)
          Save XML data to a file from a List of Lists of Strings.
 void saveAsLists(OutputStream pOutputStream, List pData)
          Save XML data to an OutputStream from a List of Lists of Strings using the default RecordSpec.
 void saveAsLists(OutputStream pOutputStream, RecordSpec pRecordSpec, List pData)
          Save XML data to an OutputStream from a List of Lists of Strings.
 void saveAsLists(String pXmlFilePath, List pData)
          Save XML data to a specified file path from a List of Lists of Strings, using the default RecordSpec.
 void saveAsLists(String pXmlFilePath, RecordSpec pRecordSpec, List pData)
          Save XML data to a specified file path from a List of Lists of Strings.
 String saveAsListsToString(List pData)
          Save XML data to a String variable from a List of Lists of Strings using the default RecordSpec.
 String saveAsListsToString(RecordSpec pRecordSpec, List pData)
          Save XML data to a String variable from a List of Lists of Strings.
 void saveBeans(File pXmlFile, BeanSpec pBeanSpec, List pBeans)
          Save Java Beans as XML data to a file, using the default RecordSpec.