2010-09-01 82 views
3

我有一個Element類的數據。我想它的值寫入一個文件,但我有麻煩:使用java將元素對象寫入文件

< Some process to acquire values into the variable "fieldData" > 

// Prepare file output 
FileWriter fstream = new FileWriter("C:/output.txt"); 
BufferedWriter out = new BufferedWriter(fstream); 

Element field = fieldData.getElement(i); 

out.write(field); // DOESN'T WORK: The method write(int) in the type BufferedWriter is not applicable for the arguments (Element) 
out.write(field.getValueAsString()); // DOESN'T WORK: Cannot convert SEQUENCE to String 

,我應該如何處理這種情況下,有什麼建議?另外,對於我來說,看到(即打印到屏幕上)與對象關聯的可用靜態變量和方法的最佳方式是什麼?謝謝。

更多的代碼片段來幫助調試:

private static final Name SECURITY_DATA = new Name("securityData"); 
private static final Name FIELD_DATA = new Name("fieldData"); 

Element securityDataArray = msg.getElement(SECURITY_DATA); // msg is a Bloomberg desktop API object 
Element securityData = securityDataArray.getValueAsElement(0); 
Element fieldData = securityData.getElement(FIELD_DATA); 
Element field = fieldData.getElement(0) 
out.write(field); // DOESN'T WORK: The method write(int) in the type BufferedWriter is not applicable for the arguments (Element) 
out.write(field.getValueAsString()); // DOESN'T WORK: Cannot convert SEQUENCE to String 
+0

「getValueAsString」方法的返回類型是什麼?它接縫這個返回不是一個字符串,而是一個SEQUENCE。 – 2010-09-01 21:22:49

+0

如何檢查「退貨類型」? – Zhang18 2010-09-01 21:46:04

+0

基本上,'getValueAsString'方法*不適用於'field'對象(!)而錯誤代碼就是我在評論中顯示的內容。我試圖打印'field.getClass()'並得到'com.bloomberglp.blpapi.impl.aB'這是否意味着它是一個彭博專有類,即使它被稱爲Element,它的行爲不像Java中的泛型Element類? – Zhang18 2010-09-01 21:53:06

回答

5

事實證明,這種彭博道具數據結構是囉嗦,至少可以說:

private static final Name SECURITY_DATA = new Name("securityData"); 
private static final Name FIELD_DATA = new Name("fieldData"); 

Element securityDataArray = msg.getElement(SECURITY_DATA); // msg is a Bloomberg desktop API object 
Element securityData = securityDataArray.getValueAsElement(0); 
Element fieldData = securityData.getElement(FIELD_DATA); 
Element field = fieldData.getElement(0); 

/* the above codes were known at the time of the question */ 
/* below is what I was shown by a bloomberg representative */ 

Element bulkElement = field.getValueAsElement(0); 
Element elem = bulkElement.getElement(0); 
out.write(elem.name() + "\t" + elem.getValueAsString() + "\n"); 

哇......我不認爲他們試圖讓它變得容易!我也很好奇,是否有一種方法可以通過讓Java打印出正確的方法來查找數據結構來解決這個問題?

0

這聽起來像你要打印一個輸入框元素的值?

如果是這樣,那麼嘗試:

out.write(field.getAttribute("value")); 
+0

試過。它說'方法getAttribute(字符串)是未定義的類型Element' ... – Zhang18 2010-09-01 21:22:39

+0

是您使用的元素對象org.w3c.dom.Element? – 2010-09-01 21:27:38

+0

真的不知道。只是增加了一些代碼片段供您參考。 – Zhang18 2010-09-01 21:46:21

1
Element element = msg.GetElement("securityData"); 
for (int i = 0; i < element.NumValues; i++) 
{ 

    Element security = element.GetValueAsElement(i); //ie: DJI INDEX 
    Element fields = security.GetElement("fieldData");//ie: INDX_MEMBERS 

    for (int j = 0; j < fields.NumElements; j++) 
    { 
    Element field = fields.GetElement(j); //a list of members 

    for (int k = 0; k < field.NumValues; k++) 
    { 
     //print field.GetValueAsElement(k); //print members name    
    } 
    } 

}