[英]Best way to store table data without the use of a database?
我需要存儲來自解析的XML
數據,解析沒有問題,我可以處理。 問題在於,我基本上收到的表數據中,列名,列號及其自身的數據每次都不同。 現在,我不想使用SQLite
數據庫,因為我的條目數量很少,因此我不認為這種情況很合適。 我什至認為我無法使用它,因為我將不得不為要接收的每個數據集創建一個新表。 現在更清楚一些示例:
<Details1_Collection>
<Details1 ProductName="Bicycle" Units="2341" DefectedUnits="125" />
<Details1 ProductName="Television" Units="3154" DefectedUnits="75" />
<Details1 ProductName="Keyboard" Units="2413" DefectedUnits="12" />
...
</Details1_Collection>
或其他XML
:
<Details_Collection>
<Details HireDate="2003-02-15T00:00:00" JobTitle="Chief Executive Officer" BirthDate="1963-03-02T00:00:00" />
<Details HireDate="2002-03-03T00:00:00" JobTitle="Vice President of Engineering" BirthDate="1965-09-01T00:00:00" />
<Details HireDate="2001-12-12T00:00:00" JobTitle="Engineering Manager" BirthDate="1968-12- 13T00:00:00" />
...
<Details_Collection>
現在,你可以看到,XML標記是不同的: Details1_Collection
和Details_Collection
, Details1
和Details
,我會收到一個新的標簽,幾乎每次都設定。 但這對我來說不是問題,因為我會在較早的服務器呼叫中收到它們,因此我知道應該解析哪些標簽。 我也不需要存儲屬性以及名稱,因為我已經有了它們。 我只需要存儲值。
問題:
我應該如何存儲屬性數據,該數據基本上是一個表數據,但是它是一個不同的表,每次具有不同的列名和編號? 我寧願不將這些數據作為String/Dom
元素原樣存儲在其父對象內部,而傾向於將其解析為一些Java數據結構,可以在UI線程上輕松訪問該數據結構。
簡而言之:
這些數據是更大對象的一部分,我的目標是將其解析為某種數據結構,並將其作為父類的一部分進行附加。
任何幫助,將不勝感激。
我偶然發現了以下論壇帖子:
ArrayLists有一個ArrayList的實現,這是代碼:
import java.util.ArrayList;
public class ArrayList2d<Type>
{
ArrayList<ArrayList<Type>> array;
public ArrayList2d()
{
array = new ArrayList<ArrayList<Type>>();
}
/**
* ensures a minimum capacity of num rows. Note that this does not guarantee
* that there are that many rows.
*
* @param num
*/
public void ensureCapacity(int num)
{
array.ensureCapacity(num);
}
/**
* Ensures that the given row has at least the given capacity. Note that
* this method will also ensure that getNumRows() >= row
*
* @param row
* @param num
*/
public void ensureCapacity(int row, int num)
{
ensureCapacity(row);
while (row < getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).ensureCapacity(num);
}
/**
* Adds an item at the end of the specified row. This will guarantee that at least row rows exist.
*/
public void Add(Type data, int row)
{
ensureCapacity(row);
while(row >= getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).add(data);
}
public Type get(int row, int col)
{
return array.get(row).get(col);
}
public void set(int row, int col, Type data)
{
array.get(row).set(col,data);
}
public void remove(int row, int col)
{
array.get(row).remove(col);
}
public boolean contains(Type data)
{
for (int i = 0; i < array.size(); i++)
{
if (array.get(i).contains(data))
{
return true;
}
}
return false;
}
public int getNumRows()
{
return array.size();
}
public int getNumCols(int row)
{
return array.get(row).size();
}
}
這是此時此刻我發現要存儲的數據的最佳方法。 Java專家,您如何看待?
謝謝。
您有理由無法執行典型的Java序列化嗎? 像在其中一樣,創建代表您的數據的類,讓它們實現Serializable
,然后使用Object(Input|Output)Stream
將整個對象圖讀寫到一個文件中?
請記住,這僅在整個對象圖都適合內存時才起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.