Parsing Dom Document From Omim Restful Web Services
2
1
Entering edit mode
11.9 years ago
partha ▴ 20

I am a beginner to web services can any one with experience please help me with the following:

I am writing a client trying to get information from OMIM RESTful web services. I am using a key OMIM provideds after registration. ( http://omim.org/help/api ) I am successful at getting connection to client. Also with the GET method I am able to fetch the required data into a DOM document. Further, I could successfuly write the entire DOM into a file on the local disk. However, I am unable to work with the DOM using the standard parsing functions available for DOM.

For example: I am able to get the root node with NodeList nl=doc.getDocumentElement()and print onto the console. But when I try to print the first child of the root node it returns null instead of expected child node.

Sample XML form: webservices -> DOM -> file

<?xml version="1.0" encoding="UTF-8" standalone="no"?><omim version="1.0">
<clinicalSynopsisList>
<clinicalSynopsis>
<mimNumber>100070</mimNumber>
<prefix>%</prefix>
<preferredTitle>AORTIC ANEURYSM, FAMILIAL ABDOMINAL, 1; AAA1</preferredTitle>
<oldFormat>
<Vascular>Abdominal aortic aneurysm; Generalized dilating diathesis;</Vascular>
<Misc>Estimated 11.6-fold increase among persons with an affected first-degree relative;</Misc>
<Inheritance>Autosomal dominant vs. recessive at an autosomal major locus or multifactorial; COL3A1 gene (120180.0004) mutations cause about 2%;</Inheritance>
</oldFormat>
</clinicalSynopsis>
</clinicalSynopsisList>
</omim>

Please find my code below:

String path="http://api.omim.org:8000/api/clinicalSynopsis?  mimNumber="+"100070"+"&include=clinicalSynopsis&format=xml&apiKey="+"<< xxxxx private key xxxxxxxxxx >> ";

              URL url = new URL(path);

              HttpURLConnection conn=(HttpURLConnection)url.openConnection();

              conn.setRequestMethod("GET");

              InputStream is = conn.getInputStream();

              DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

              Document doc = docBuilder.parse(is);

              Source src= new DOMSource(doc);

              File file = new File("d:/text.xml");

              Result rs = new StreamResult(file);

              TransformerFactory tmf = TransformerFactory.newInstance();

              Transformer trnsfrmr = tmf.newTransformer();

              trnsfrmr.transform(src, rs);

              System.out.println("XML file is created successfully");

              System.out.println("The root element is :: "+doc.getDocumentElement().getNodeName());


              NodeList nl=doc.getDocumentElement().getChildNodes();

              System.out.println("child nodelist length::"+nl.getLength());


              System.out.println("First child node name :: "+doc.getDocumentElement().getFirstChild().getNodeName());

              System.out.println("Last child node name :: "+doc.getDocumentElement().getLastChild().getNodeName());

Output I got:- XML file is created successfully The root element is :: omim child nodelist length::3 First child node name :: #text Last child node name :: #text

In the output got the root node is “omim” and it has 3 children. but returns null when tried printing the first and last child name. Similarly getParent(), getChild(), getSibling() methods are not working for me.

Any help will be highly appreciated.

Thank you,

web java • 3.9k views
ADD COMMENT
0
Entering edit mode
ADD REPLY
2
Entering edit mode
11.9 years ago

I cannot currently use the OMIM API , but the following java code should do the work. I think your problem is that you assume that the first child of an XML node is an ELEMENT, which is wrong, it seems to be a TEXT node containing a carriage return.

import java.net.URLEncoder;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


public class Biostar44705
    {
    private static final String API_KEY="XXXXXXXX"; 
    private DocumentBuilder builder;
    private Transformer echoTransformer=null;

    private Biostar44705()throws Exception
        {
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        factory.setCoalescing(true);
        factory.setIgnoringComments(true);
        factory.setNamespaceAware(false);
        builder=factory.newDocumentBuilder();

        TransformerFactory trf=TransformerFactory.newInstance();
        this.echoTransformer =trf.newTransformer();
        this.echoTransformer .setOutputProperty(OutputKeys.INDENT, "yes");
        this.echoTransformer .setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
    void get(int omimId)throws Exception
        {
        String uri="http://api.omim.org:8000/api/clinicalSynopsis?mimNumber="+omimId+
                "&include=clinicalSynopsis&format=xml&apiKey="+
                URLEncoder.encode(API_KEY,"UTF-8");
        Document dom=builder.parse(uri);
        Element root=dom.getDocumentElement();
        if(root==null) return;
        for(Node n1=root.getFirstChild();n1!=null;n1=n1.getNextSibling())
            {
            if(n1.getNodeType()!=Node.ELEMENT_NODE) continue;
            echoTransformer.transform(new DOMSource(n1),new StreamResult(System.out));
            break;
            }
        }
public static void main(String[] args) throws Exception
    {
    new Biostar44705().get(100070);
    }
}
ADD COMMENT
0
Entering edit mode

good lord Java is verbose. When are you going to switch to Groovy?

ADD REPLY
0
Entering edit mode

@Jeremy, well, for that simple problem I would use XSLT, not java

ADD REPLY
0
Entering edit mode

I'm sure Groovy is great, but if I use an exotic language, I'm afraid less people will be able to use/read it. Moreover, my code is standard and don't need any other depedencies but java itself.

ADD REPLY
0
Entering edit mode
11.9 years ago

I would look into xslt (http://en.wikipedia.org/wiki/XSLT). Is XML the only output this service generates?

ADD COMMENT

Login before adding your answer.

Traffic: 1505 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6