22 June 2009

Event-driven XML parsing (SAX) with Java+JavaScript

I just wrote SAXScript, an event-driven SAX parser java program invoking some javascript callbacks. It can be used to quickly write a piece of code to parse a huge XML file.


Download saxscript.jar from http://code.google.com/p/lindenb/downloads/list


java -jar saxscript.jar (options) [file|url]s


-h (help) this screen
-f read javascript script from file
-e 'script' read javascript script from argument
-D add a variable (as string) in the scripting context.
__FILENAME__ is the current uri.
-n SAX parser is NOT namespace aware (default true)
-v SAX parser is validating (default false)


function startDocument()
{println("Start doc");}
function endDocument()
{println("End doc");}
function startElement(uri,localName,name,atts)
print(""+__FILENAME__+" START uri: "+uri+" localName:"+localName);
for(var i=0;atts!=undefined && i< atts.getLength();++i)
print(" @"+atts.getQName(i)+"="+atts.getValue(i));
function characters(s)
{println("Characters :" +s);}
function endElement(uri,localName,name)
{println("END: uri: "+uri+" localName:"+localName);}

Source Code


The following shell script invokes NCBI/ESearch to retrieve a key to get all the bibliographic references about the Rotaviruses (8793 references).
This key is then used to download each pubmed entry and we then count the number of time each journal (tag is "MedlineTA") was cited.
WEBENV=`${JAVA} -jar saxscript.jar \
-e '
var WebEnv=null;
function startElement(uri,localName,name,atts)
if(name=="WebEnv") WebEnv="";

function characters(s)
if(WebEnv!=null) WebEnv+=s;

function endElement(uri,localName,name)
' \

${JAVA} -jar saxscript.jar -e '
var content=null;
var hash=new Array();
function startElement(uri,localName,name,atts)
if(name=="MedlineTA") content="";

function characters(s)
if(content!=null) content+=s;

function endElement(uri,localName,name)
var c=hash[content];
function endDocument()
for(var content in hash)
println(content+"\t"+ hash[content]);
' "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&query_key=1&WebEnv=${WEBENV}&retmode=xml" |\
sort -t ' ' -k2n


Acta Gastroenterol Latinoam 1
Acta Histochem Suppl 1
Acta Microbiol Acad Sci Hung 1
Acta Microbiol Hung 1
Acta Microbiol Immunol Hung 1
Acta Pathol Microbiol Scand C 1
Acta Vet Acad Sci Hung 1
Adv Neonatal Care 1
Adv Nurse Pract 1
Adv Ther 1
Adv Vet Med 1
Afr J Med Med Sci 1
Age Ageing 1
AIDS Res Hum Retroviruses 1
AJNR Am J Neuroradiol 1
AJR Am J Roentgenol 1
Akush Ginekol (Sofiia) 1
Appl Environ Microbiol 87
J Pediatr Gastroenterol Nutr 97
J Virol Methods 130
Lancet 130
Vaccine 158
Pediatr Infect Dis J 177
J Gen Virol 217
Arch Virol 254
J Med Virol 262
J Infect Dis 265
Virology 278
J Virol 460
J Clin Microbiol 514

That's it !

1 comment:

Anonymous said...

You might also want to look at vtd-xml, the next generation XML processing model that is far more powerful than DOM and SAX