|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.gnu.xml.ezparse.SimpleParser
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>
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 |
public SimpleParser()
| Method Detail |
public void addElementListener(java.lang.String path,
ElementListener listener)
path - xpath like expression -- not null, not emptylistener - the listener to call when path is encountered
listener must not be null
public void parse(java.lang.String uri,
Context ctx)
throws java.io.IOException,
org.xml.sax.SAXException,
javax.xml.parsers.ParserConfigurationException
uri - ctx - the context passed to ElementListeners
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
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||