com.marringtons.xml
Class XMLtoMap

java.lang.Object
  extended bycom.marringtons.xml.XMLparser
      extended bycom.marringtons.xml.XMLtoMap

public class XMLtoMap
extends XMLparser

Normally XMLparser is used to dynamically process an XML stream. Sometimes XML documents are used as another form of properties file - containing static information that needs to be looked up when needed. Here we load an XML file once and retrieve information on it at will, given the path to the node we require. Note that as with properties file, order is not important and there should only be one path to a leaf value. Unlike a properties file, XML data can be retrieved to an intermediate note and all nodes can have attributes. Sometimes (but not often) XML provides a better property container. Retrieving values from an XMLtoMap is not as efficient as Properties since it mush walk the tree with one hasmap retrieval per depth of nodes.

 map = new XMLtoMap( new StringReader( sampleXML));
 Node node = map.get( "OuterElement.InnerElement");
 check( node.name.equals( "InnerElement"));
 check( node.children.size() == 0);
 check( node.attributes.size() == 1);
 check( node.cData.equals( "inner data");
 

Author:
Paul Marrington
See Also:
Properties, XMLparser

Nested Class Summary
static class XMLtoMap.Node
          Class wrapping a tree node for an XML tree
 
Field Summary
 XMLtoMap.Node root
          Root node for the XML tree.
 
Fields inherited from class com.marringtons.xml.XMLparser
elementStack, lineNumber, problems
 
Constructor Summary
XMLtoMap()
          Default constructor to be used with parse(...).
XMLtoMap(Reader reader)
          Instantiate XMLtoMap and feed a reader stream to it.
 
Method Summary
 XMLtoMap.Node get(String path)
          Retrieve a Node entry given a path separated by dots map = new XMLtoMap( new StringReader( sampleXML)); Node node = map.get( "OuterElement.InnerElement"); check( node.name.equals( "InnerElement"));
 
Methods inherited from class com.marringtons.xml.XMLparser
parse, parse, parse, reportProblem, reportProblem, reportProblem, throwException
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

root

public XMLtoMap.Node root
Root node for the XML tree. Use if you want to manually process the tree
 map = new XMLtoMap( new StringReader( sampleXML));
 Node node = map.root;
 check( node.name.equals( "root"));
 check( node.children.size() == 1);
 check( node.children.get( "OuterElement") != null);
 

Constructor Detail

XMLtoMap

public XMLtoMap()
Default constructor to be used with parse(...).
 map = new XMLtoMap();
 map.parse( sampleXML);
 


XMLtoMap

public XMLtoMap(Reader reader)
Instantiate XMLtoMap and feed a reader stream to it.

Parameters:
reader - stream of XML
 map = new XMLtoMap( new StringReader( sampleXML));
 
Method Detail

get

public XMLtoMap.Node get(String path)
Retrieve a Node entry given a path separated by dots
 map = new XMLtoMap( new StringReader( sampleXML));
 Node node = map.get( "OuterElement.InnerElement");
 check( node.name.equals( "InnerElement"));
 

Parameters:
path - dot separated path to required entry
Returns:
Node represented by path or null if path is invalid.


Copyright © 2005 Paul Marrington http://library.marringtons.com