Skip to content

Instantly share code, notes, and snippets.

@jawher
Created December 27, 2010 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jawher/756241 to your computer and use it in GitHub Desktop.
Save jawher/756241 to your computer and use it in GitHub Desktop.
Immanix sample
<root>
<entry>
<value>[...]</value>
</entry>
<entry>
<value>[...]</value>
<extra>[...]</extra>
</entry>
<entry>
<value>[...]</value>
</entry>
:
:
</root>
StaxMatcher<String> value = start("value").thenr(chars()).thenl(end("value"));
StaxMatcher<String> extra = start("extra").thenr(chars()).thenl(end("extra"));
StaxMatcher<Tuple2<String, String>> entry = start("entry").thenr(value).then(extra).thenl(end("entry"));
Mapper<Void> execEntry = new Mapper<Tuple2<String, String>, T>(entry) {
public Void process(Tuple2<String, String> data) {
// insert data into the database
return null;
}
}
StaxMatcher<Void> entries = execEntry.zeroOrMore();
StaxMatcher<Void> almighty = startDoc().then(start("root")).thenr(entries).thenl(end("root")).thenl(endDoc());
XMLInputFactory inputFactory = createInputFactory();
XMLEventReader evr = inputFactory.createXMLEventReader(new FileInputStream("/some/file.xml"));
MatcherResult<Void> result = almighty.match(evr);
if(result.isFailure()){
//handle errors
} else {
//everything went well
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment