2010年7月10日 星期六

gcp_refine 使用 xml parser 和 handler 與 java test

//////////////////////////// 說明 /////////////////////////////
此範例的重點是用到 xml parse 的觀念, 而此文件的 parse
選用 DOM 方式來 parse 一份 xml 文件, Parse 的過程是從
root 開始然後列出它的下一層 Node, 此下一層 Node 再列出下一層 Node,
就可以走到每個節點, 而後讀出它們的值.


一份 xml 文件長的像是如下,
每個 Tag 下的內容都可以自己寫出自己要的名稱,
例如: XXX , 可以是 R, AutoDock4...



R


RScript
R description








///////////////////////// 程式///////////////////////////
GcpXMLParser.java :
{{{
package net.asgc.gap.portal.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class GcpXMLParser {
public final static String FILE_NAME = "gcp.xml";
private Document doc;

public GcpXMLParser() throws ParserConfigurationException, SAXException, IOException {

//Create instance of DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Get the DocumentBuilder
DocumentBuilder parser = factory.newDocumentBuilder();
//Create blank DOM Document
doc = parser.parse(FILE_NAME);

}

public Map getAppInfo(String appName){
Map appInfo = new HashMap();

NodeList appNameList = doc.getElementsByTagName("appName"); // Get all app Name tage

// for loop to search appName
for(int i = 0; i < appNameList.getLength(); i++){
Node appNameNode = appNameList.item(i);
String name = appNameNode.getTextContent().trim();
System.out.println(name);

if(name.equals(appName)){
System.out.println("haha");

appInfo.put("appName", name);
// Get father to star application root search
Element appNode = (Element)appNameNode.getParentNode();

// Get input field
NodeList inputFilesNodeList = appNode.getElementsByTagName("inputFiles");
for(int j = 0; j < inputFilesNodeList.getLength(); j++){
Node inputFilesNode = inputFilesNodeList.item(i);
NodeList inputNodeList = inputFilesNode.getChildNodes();

// Get Input tag
for(int k = 0; k < inputNodeList.getLength(); k++){
Node inputNode = inputNodeList.item(k);
//System.out.println( "inputNode: " + k + " "+ inputNode.getTextContent());
if(inputNode.getNodeType() == Node.TEXT_NODE)
continue;

Element inputElem = (Element)inputNode;

// Get inptName
NodeList inputNameList = inputElem.getElementsByTagName("inputName");
String inputName = inputNameList.item(0).getTextContent().trim();
System.out.println(inputName);
//System.out.println(inputNameList.getLength());
NodeList descriptionList = inputElem.getElementsByTagName("description");
String description = descriptionList.item(0).getTextContent().trim();
appInfo.put(inputName, description);

}
//System.out.println(inputFilesNode);

}

}

}

return appInfo;

}

public Node getDocumentElement() {
return doc.getDocumentElement();
}

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

GcpXMLParser xmlparse = new GcpXMLParser();


System.out.println(xmlparse.getAppInfo("AutoDock 4"));


}


}




}}}


gcp.xml
{{{



AutoDock 4


Target
Need the *.tar.gz target file consisting of *.maps, *fld, *.xyz, target.pdbqt


Ligand
Need the *.pdbqt file. (i.e ligand.pdbqt)


DPF
Need *.dpf file (Docking Parameter File)





R


RScript
R description




}}}






//////////////////////// 參考 /////////////////////////////
1. Sun java:
Document class
Node class
Element class
NodeList class

2. XML parser tutorial
XML tutorial
DOM XML element

沒有留言:

張貼留言