Hi All, I am newbie in using Biomart. I want to access the COSMIC Biomart using the Martservices and Java. The documentation says that I should POST the XML file to there link and that is it. I tried with the following code but it returns nothing.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
public class accessDB {
public static void saveDbData(String xmlName)throws Exception{
try {
URL url = new URL("http://www.biomart.org/martservice");
String fileContents=readFileAsString(xmlName);
URLConnection urlc = url.openConnection();
urlc.setDoOutput(true);
urlc.setDoInput(true);
PrintWriter pw = new PrintWriter(urlc.getOutputStream());
pw.write(fileContents);
pw.close();
BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch (Exception e) {
}
}
private static String readFileAsString(String filePath)
throws java.io.IOException{
StringBuilder fileData = new StringBuilder(1000);
BufferedReader reader = new BufferedReader(
new FileReader(filePath));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1){
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
return fileData.toString();
}
}
When I change the pw and input a the file buffer without a string it gives a general information page. Can somebody help me please. What is missing here?
Thank you
Oh, About the XML file, it's just a simple file saved using XML button in the Biomart Web Interface.
can you show use this XML file please.
you don't need to 'new char[1024];' for each loop; flush your ouputstream, don't ignore the exception, you don't need this PrintWriter (write the bytes in the output sream), etc...
you should put a main() method in your code so people can actually test it