Question: Parsing Dom Document From Omim Restful Web Services
gravatar for partha
5.7 years ago by
partha20 wrote:

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. ( ) 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">
<preferredTitle>AORTIC ANEURYSM, FAMILIAL ABDOMINAL, 1; AAA1</preferredTitle>
<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>

Please find my code below:

String path="  mimNumber="+"100070"+"&include=clinicalSynopsis&format=xml&apiKey="+"<< xxxxx private key xxxxxxxxxx >> ";

              URL url = new URL(path);

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


              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 • 2.4k views
ADD COMMENTlink modified 5.7 years ago by Pierre Lindenbaum103k • written 5.7 years ago by partha20

cross posted on SO:

ADD REPLYlink written 5.7 years ago by Pierre Lindenbaum103k
gravatar for Pierre Lindenbaum
5.7 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum103k wrote:

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 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 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();

        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=""+omimId+
        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));
public static void main(String[] args) throws Exception
    new Biostar44705().get(100070);
ADD COMMENTlink written 5.7 years ago by Pierre Lindenbaum103k

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

ADD REPLYlink modified 5.7 years ago • written 5.7 years ago by Jeremy Leipzig17k

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

ADD REPLYlink written 5.7 years ago by Pierre Lindenbaum103k

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 REPLYlink written 5.7 years ago by Pierre Lindenbaum103k
gravatar for Damian Kao
5.7 years ago by
Damian Kao14k
Damian Kao14k wrote:

I would look into xslt ( Is XML the only output this service generates?

ADD COMMENTlink written 5.7 years ago by Damian Kao14k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 644 users visited in the last hour