XMLData-Library sax-tree


Why this library? There are much other ones.

This library has been created to use the Java integrated XML-Api (sax/saxproject).The problem with it is the flat implementation which does not hold on the concept of data abstraction. This library represents a wrapper/workaround which fetches all events of the XML-Api, writes it into a data structure and allows on this way to use the object orientated data abstraction (Tree-based API). This allows to use real object oriented programming with data abstraction like Java does and does not need all using objects to parse their own XML input. With its 3 classes and 2 interfaces, it is small and quick.

Used classes / interfaces

  • XMLDataElement:
  • Object to build a tree which saves all elements of the XML structure.
  • XMLDataReader:
  • Class to read XML informations out of a stream.
  • XMLDataWriterImpl:
  • Class to write XML informations into a stream.
  • XMLDataWriter:
  • Interface to allow an own implementation of writing the XML output.
  • XMLDataIO:
  • Interface to give objects the possibility to import/export their data.

    Where to download the XMLData-Library?

    The library - which stands under the LGPL-License - can be found at sourceforge in the download area

    XMLData-Library Javadoc files

    The current Javadoc files can be found here.

    How to Use the XMLData-Library?

    This library contains three classes and two interfaces. A data class which stores the read data, a reader and a writer. An interface is made to give the possibility to implement an own way to write the XML data and an interface which contains methods to export/import data from the own data structur into the XML data structur and backwards.

    First, a little example what using classes will need to do/implement.
     
    class MyClass implements XMLDataIO
    {
    	private final Vector m_vecChildren = new Vector();
    
    	public void importFromXML(XMLDataElement inputData)
    	{
    		Attributes atts = inputData.getAttributes();
    		iValue1 = atts.getValue("value1");
    		iValue2 = atts.getValue("value2");
    
    		Iterator iterator = inputData.getAllDataForKey("MyTreeNode");
    		while(iterator.hasNext()) {
    			MyTreeNode node = new MyTreeNode(iterator.next());
    			addNode(node);
    		}
    	}
    
    	public XMLDataElement exportToXML()
    	{
    		// no Attributes, no parent element, I am root!
    		XMLDataElement me = new XMLDataElement(null, "MyClass", null);
    		
    		// Add some Attributes
    		me.addAttribute("value1", iValue1);
    		me.addAttribute("value2", iValue2);
    
    		// Get the data of my DayTime object
    		XMLDataElement timeValues = m_DayTime.exportToXML();
    		if (timeValues != null)
    		{
    			// add the fetched data into the tree
    			me.addDataElement(timeValues);
    		}
    
    		// go through all my child elements to get the data of the whole tree
    		for (int i = 0; i < m_vecChildren.size(); i++)
    		{
    			// get next child
    			MyClass mcChild = m_vecChildren.elementAt(i);
    			// ask child to get the data
    			me.addDataElement(mcChild.exportToXML());
    		}
    
    		return me;
    	}
    }
    

    Writing the data into an output file is simple. A little example:
    (The temporarly created variable "toBeWritten" is not really needed and could be directly written into the "writeStream" method call)
     
    public static void writeFile(MyClass outputData, String strOutputFile)
    {
    	XMLDataElement toBeWritten = outputData.exportToXML();
    
    	try
    	{
    		// create an OutputStream to write a file
    		OutputStream stream = new FileOutputStream(strOutputFile);
    		// create the XMLWriter
    		XMLDataWriter writer = new XMLDataWriterImpl();
    		// write the data into the stream
    		writer.writeStream(toBeWritten, stream);
    		// close the stream/file
    		stream.close();
    	}
    	catch (IOException e1)
    	{
    		// error while writing into the stream!
    	}
    	catch (SAXException e2)
    	{
    		// error while creating XML output!
    	}
    }
    

    The concept behind reading a file is rather simple too. There is an XMLDataReader needed which will read a stream/source when calling the corresponding method. This method will return an XMLDataElement which contains the whole data. That only one object is returned is no problem because the XML standard allows only one root element.  
    To read an XML file, a little example of a working java code:
     
    public static MyClass loadFile(String strInputFile)
    {
    	// try to open and read the file
    	XMLDataElement outputData = null;
    	try
    	{
    		// create the reader
    		XMLDataReader reader = new XMLDataReader();
    		// read the given file
    		outputData = reader.readSource(new InputSource(strInputFile));
    	}
    	catch (IOException e1)
    	{
    		// error while reading InputSource!
    	}catch (SAXException e2)
    	{
    		// error while parsing Input!
    	}
    
    	// were we able to read the file with correct XML data?
    	if (outputData != null)
    	{
    		// some temp. variables
    		XMLDataElement next;
    		// the final data in the form of the needed, own, data structur
    		MyClass root = null;
    
    		// work with all elements having the name "MyClass"
    		Iterator  iterator = outputData.getAllDataForKey("MyClass");
    		while (iterator.hasNext())
    		{
    			// Give the new object its data, what there is in does not interrest me!
    			MyClass element = new MyClass(iterator.next());
    			root.add(element);
    		}
    
    		// find the first, if there is one, element with the name "element2"
    		next = outputData.getDataForKey("element2");
    		if (next != null)
    		{
    			// give the root its own data - he will know what to do
    			root.importFromXML(next);
    		}
    		return root;
    	}
    	return null;
    }
    

     
    If there are any questions or suggestions, a wish or criticism, something to tell or a feedback about the usage please contact:

    Thomas Duif -


    Copyright© 2005 by Thomas Duif, Nico Hamacher