[MOBY-l] java xml parsing
Paul Gordon
gordonp at cbr.nrc.ca
Tue May 25 14:45:41 UTC 2004
If you are using MobyRequest, the getOutput will return a Vector with
one element (corresponding to the one response). This element is a
MobySimpleDataInstance array of length one. The MobySimpleDataInstance
will just contain the genbank-flatfile tag and its nested contents.
Unfortunately, for now this is as far as the autoparsing goes.
Hopefully soon
MobySimpleDataInstance will soon give you its constituent members as
other MobySimpleDataInstances, down to the primitives of the ontology.
But for now, you can parse the XML response in an XML parser (determined
by Java using the runtime libraries available) and run an XPath. I
haven't tried to compile the following code, but it should give you an
idea of how to parse robustly (given that your XML object
genbank-flatfile is not attributed to the MOBY namespace):
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.xpath.XPathAPI; // xpath statement execution env
try{
// Insert your code to get the XML string
// Get a parser and parse the document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document=docBuilder.parse(new
java.io.StringBufferInputStream(xmlString));
Element mobyPayload = document.getDocumentElement();
// Now run the XPath statement that will be used fetch data from the
server response
Node idAttr = XPathAPI.selectSingleNode(mobyPayload,
"//genbank-flatfile/@id");
if(idAttr != null){
System.out.println("Output of XPath was " + idAttr.getNodeValue());
}
}
catch(TransformerException te){
// Insert your favourite death scene here
}
catch(ParserConfigurationException){
// Ditto
}
catch(org.xml.sax.SAXException saxe){
// Ditto
}
catch(java.io.IOException ioe){
//Ditto
}
Benjamin Good wrote:
> Say I have a Query response like this
>
> "<?xml version='1.0' encoding='UTF-8'?>"+
> "<moby:MOBY xmlns:moby='http://www.biomoby.org/moby'
> xmlns='http://www.biomoby.org/moby'>"+
> "<moby:mobyContent
> moby:authority='illuminae.com'><moby:serviceNotes>This is provided by
> SeqHound</moby:serviceNotes>"+
> "<moby:mobyData moby:queryID='1'>"+
> "<moby:Simple moby:articleName=''>" +
> "<genbank-flatfile namespace='NCBI_gi'
> id='111076'><![CDATA[LOCUS D31461 32 aa linear ROD 30-MAY-1997"+
> "DEFINITION T-cell receptor delta chain BDN7, thymus - mouse
> (fragment)."+
> "ACCESSION D31461"+
> "PID g111076"+
> "ORIGIN"+
> " 1 atyfcalmer vsrrgapdkl vfgqgtqvtv ep"+
> "//]]></genbank-flatfile></moby:Simple>"+
> "</moby:mobyData>"+
> "</moby:mobyContent></moby:MOBY>";
> Any suggestions regarding the best way to extract the gi id or the
> CDATA ? It seems there are many options, just wondering if the Java
> group has already settled on an approach.
>
> thanks
> -Ben
>
> _______________________________________________
> moby-l mailing list
> moby-l at biomoby.org
> http://biomoby.org/mailman/listinfo/moby-l
>
More information about the moby-l
mailing list