org.gnu.xml.ezparse
Class SimpleParser

java.lang.Object
  extended byorg.gnu.xml.ezparse.SimpleParser

public class SimpleParser
extends java.lang.Object

Given an XML file:

	<? xml version=1.0" ?>
	<states>
		<state code="CA" name="California">
			<capital>Sacremento</capital>
		</state>
		<state code="NY" name="New York">
			<capital>Albany</capital>
		</state>
	</states>
 

Here is the sample code which is used to parse the file:

 package test;

 import org.gnu.xml.ezparse.SimpleParser;
 import org.gnu.xml.ezparse.ElementAdapter;
 import org.gnu.xml.ezparse.Context;
 import org.gnu.xml.ezparse.Element;

 import java.util.List;
 import java.util.LinkedList;

 public class SimpleParserTest
 {
	public static void main(String[] args)
		throws Exception
	{
		SimpleParser simpleParser = new SimpleParser();
		Context ctx = new Context();
		ctx.set("stateList", new LinkedList());
		
		//add listeners

		simpleParser.addElementListener("/states/state",
				new ElementAdapter()
				{
					public void startElement( Element element, Context ctx )
					{
						StateEssence essence = new StateEssence(); 
						ctx.set("essence", essence);
					}

					public void endElement( Element element, Context ctx )
					{
						StateEssence essence = 
							(StateEssence) ctx.get("essence");
						essence.name = element.getAttribute("name");
						essence.code = element.getAttribute("code");

						List states = (List) ctx.get("stateList");
						states.add( new State( essence ) );
					}
				}
		);

		simpleParser.addElementListener("/states/state/capital",
				new ElementAdapter()
				{
					public void endElement( Element element, Context ctx )
					{
						StateEssence essence = 
							(StateEssence) ctx.get("essence");
						essence.capital = element.getValue();
					}
				}
		);

		
		simpleParser.addElementListener("/states/state/capital",
				new ElementAdapter()
				{
					public void endElement( Element element, Context ctx )
					{
						State state = (State) ctx.get("currentState");
						state.setCapital( element.getValue() );
					}
				}
		);

		//parse uri
		simpleParser.parse(args[0], ctx);
		printStates( ctx );
	}

	//print state data
	private static void printStates( Context ctx )
	{
		List list = (List) ctx.get("stateList");
		State[] states = (State[]) list.toArray( new State[ list.size() ] );
		
		for(int i=0; i
 The parser also supports RegularExpressions for mapping paths to
 Listeners.  For example:
 

		simpleParser.addElementListener("/states/state/c.*",
				new ElementAdapter()
				{
					public void endElement( Element element, Context ctx )
					{
						State state = (State) ctx.get("currentState");
						state.setCapital( element.getValue() );
					}
				}
		);
 
will match "/states/state/capital" from the XML file.


Constructor Summary
SimpleParser()
          Constructor
 
Method Summary
 void addElementListener(java.lang.String path, ElementListener listener)
          Adds the specified listener for the element defined by path.
 void parse(java.lang.String uri, Context ctx)
          Parses the XML document specified by the URI.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleParser

public SimpleParser()
Constructor

Method Detail

addElementListener

public void addElementListener(java.lang.String path,
                               ElementListener listener)
Adds the specified listener for the element defined by path.

Parameters:
path - xpath like expression -- not null, not empty
listener - the listener to call when path is encountered listener must not be null

parse

public void parse(java.lang.String uri,
                  Context ctx)
           throws java.io.IOException,
                  org.xml.sax.SAXException,
                  javax.xml.parsers.ParserConfigurationException
Parses the XML document specified by the URI.

Parameters:
uri -
ctx - the context passed to ElementListeners
Throws:
java.io.IOException - thrown by the parser
org.xml.sax.SAXException - thrown by the parser
javax.xml.parsers.ParserConfigurationException - thrown by the SAX parser factory