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,
cross posted on SO: http://stackoverflow.com/questions/10551202