简体   繁体   English

不使用数据库存储表数据的最佳方法?

[英]Best way to store table data without the use of a database?

I need to store data from parsed XML , I have no problem with the parsing, that I can handle. 我需要存储来自解析的XML数据,解析没有问题,我可以处理。 The problem is that I receive a table data basically where each time the column names, columns number and the data it self are different. 问题在于,我基本上收到的表数据中,列名,列号及其自身的数据每次都不同。 Now I don't want to use SQLite Database as I have a small number of entries and I don't see it feet for this situation. 现在,我不想使用SQLite数据库,因为我的条目数量很少,因此我不认为这种情况很合适。 I even think I can't use it as I will have to create a new table for each data set I will receive. 我什至认为我无法使用它,因为我将不得不为要接收的每个数据集创建一个新表。 Now to be more clear here is some examples: 现在更清楚一些示例:

 <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>

or another XML : 或其他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>

Now you can notice that the XML tags are different: Details1_Collection and Details_Collection , Details1 and Details and I will receive a new tag set almost each time. 现在,你可以看到,XML标记是不同的: Details1_CollectionDetails_CollectionDetails1Details ,我会收到一个新的标签,几乎每次都设定。 But that is not a problem for me, because I receive them in a sooner call to the server so I know what tags I should parse. 但这对我来说不是问题,因为我会在较早的服务器呼叫中收到它们,因此我知道应该解析哪些标签。 I don't need to store the Attributes as well names as I already have those too. 我也不需要存储属性以及名称,因为我已经有了它们。 I need to store only the values. 我只需要存储值。

The Question: 问题:

How should I store the Attributes data which basically is a table data but It's a different table with different column names and number each time? 我应该如何存储属性数据,该数据基本上是一个表数据,但是它是一个不同的表,每次具有不同的列名和编号? I would prefer to not store this data as is as a String/Dom element inside of it's parent object but ratter to parse it into some java data structure that can be accessed easily on the UI thread. 我宁愿不将这些数据作为String/Dom元素原样存储在其父对象内部,而倾向于将其解析为一些Java数据结构,可以在UI线程上轻松访问该数据结构。

In Short: 简而言之:

This data is a part of a bigger object and my goal is to parse it into some kind of data structure and attach it as part of the parent class. 这些数据是更大对象的一部分,我的目标是将其解析为某种数据结构,并将其作为父类的一部分进行附加。

Any help with that would be appreciated. 任何帮助,将不胜感激。

I have stumbled on the following forum post: 我偶然发现了以下论坛帖子:

http://www.javaprogrammingforums.com/java-programming-tutorials/696-multi-dimension-arraylist-example.html http://www.javaprogrammingforums.com/java-programming-tutorials/696-multi-dimension-arraylist-example.html

There is an implementation of ArrayList of ArrayLists, this is the code: 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();
}
}

This is the best way I found by this moment to store the data I want to store. 这是此时此刻我发现要存储的数据的最佳方法。 What do you think, Java experts? Java专家,您如何看待?

Thanks. 谢谢。

Is there a reason you cannot perform the typical Java Serialization? 您有理由无法执行典型的Java序列化吗? As in, create classes that represent your data, let them implement Serializable , and just read and write the whole object graph to a file using Object(Input|Output)Stream s? 像在其中一样,创建代表您的数据的类,让它们实现Serializable ,然后使用Object(Input|Output)Stream将整个对象图读写到一个文件中?

Keep in mind this only works if your whole object graph will fit into memory. 请记住,这仅在整个对象图都适合内存时才起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM