XCO
1. XCO簡介
XCO(XSON common object)是一種通用的數(shù)據(jù)對象, 底層采用一種類似Map的數(shù)據(jù)結(jié)構(gòu)進(jìn)行數(shù)據(jù)的存儲訪問,能夠方便的以XML方式對數(shù)據(jù)對象進(jìn)行序列化和反序列化,適合同構(gòu)、異構(gòu)系統(tǒng)之間的數(shù)據(jù)傳輸和交換。
2. 支持的數(shù)據(jù)類型
a. 8種基本類型(byte, boolean, short, int, long, float, double, char)
b. 8種基本類型數(shù)組
e. String, String數(shù)組, String集合
c. Date, sql.Date, sql.Time
d. BigInteger, BigDecimal
e. XCO, XCO數(shù)組, XCO集合
3. XML格式
<?xml version="1.0" encoding="UTF-8"?>
<X>
<B K="byteVal" V="3"/>
<O K="booleanVal" V="true"/>
<H K="shortVal" V="5"/>
<I K="intVal" V="2"/>
<L K="longVal" V="2"/>
<F K="floatVal" V="2.0"/>
<D K="doubleVal" V="-0.3"/>
<C K="charVal" V="x"/>
<S K="stringVal" V="hello world"/>
<A K="dateTimeVal" V="2016-09-02 16:58:25"/>
<E K="dateVal" V="2016-09-02"/>
<G K="TimeVal" V="16:58:25"/>
<K K="bigIntegerVal" V="1380000"/>
<M K="bigDecimal" V="1380000.9999"/>
<X K="xcoVal"/>
<IA K="intArray" V="1,3,5,8"/>
<SA K="stringArray">
<S V="aa"/>
<S V="bb"/>
<S V="cc"/>
</SA>
<SL K="stringList"/>
<SS K="stringSet"/>
</X>
說明
a. 以此為例:<H K="shortVal" V="5"/>
H: 數(shù)據(jù)類型標(biāo)識,當(dāng)前標(biāo)示short類型
K: key
V: 具體數(shù)值
b. 數(shù)據(jù)類型標(biāo)識說明
B: byte
H: short
I: int
L: Long
F: float
D: double
C: char
O: boolean
S: String
X: xco
A: date
E: sql.date
G: sql.time
J: sql.timestamp
K: bigInteger
M: bigDecimal
..
其他詳見:org.xson.common.object.DataType
4. 常用方法
a. 賦值
public final void setIntegerValue(String field, int var)
設(shè)置一個int類型的值, field為key
public final void setStringValue(String field, String var)
設(shè)置一個String類型的值, field為key
//setXxx
b. 取值
public final int getIntegerValue(String field)
獲取一個int類型的值, field為key
public final String getStringValue(String field)
獲取一個String類型的值, field為key
//getXxx
c. 序列化
public String toXMLString()
把XCO對象以XML方式進(jìn)行序列化
public static XCO fromXML(String xml)
從一個XML字符串反序列化為XCO對象
public String toJSON()
把XCO對象以JSON方式進(jìn)行序列化
5. 使用示例
XCO xco = new XCO();
// 設(shè)置基本類型
xco.setByteValue("byteVal", (byte) 3);
xco.setBooleanValue("booleanVal", true);
xco.setShortValue("shortVal", (short) 5);
xco.setIntegerValue("intVal", 2);
xco.setLongValue("longVal", 2L);
xco.setFloatValue("floatVal", 2.0F);
xco.setDoubleValue("doubleVal", -0.3D);
xco.setCharValue("charVal", 'x');
// 設(shè)置對象類型
xco.setStringValue("stringVal", "hello world");
xco.setDateTimeValue("dateTimeVal", new java.util.Date());
xco.setDateValue("dateVal", new java.sql.Date(System.currentTimeMillis()));
xco.setTimeValue("TimeVal", new java.sql.Time(System.currentTimeMillis()));
xco.setBigIntegerValue("bigIntegerVal", new BigInteger("1380000"));
xco.setBigDecimalValue("bigDecimal", new BigDecimal("1380000.9999"));
xco.setXCOValue("xcoVal", new XCO());
// 設(shè)置數(shù)組
xco.setIntegerArrayValue("intArray", new int[] { 1, 3, 5, 8 });
xco.setStringArrayValue("stringArray", new String[] { "aa", "bb", "cc" });
// 設(shè)置集合
List<String> list = new ArrayList<String>();
xco.setStringListValue("stringList", list);
Set<String> set = new TreeSet<String>();
xco.setStringSetValue("stringSet", set);
// XML序列化
String xml = xco.toXMLString();
// 反序列化
XCO newXco = XCO.fromXML(xml);
// 取值
byte byteVal = xco.getByteValue("byteVal");
boolean booleanVal = xco.getBooleanValue("booleanVal");
short shortVal = xco.getShortValue("shortVal");
int intVal = xco.getIntegerValue("intVal");
long longVal = xco.getLongValue("longVal");
float floatVal = xco.getFloatValue("floatVal");
double doubleVal = xco.getDoubleValue("doubleVal");
String stringVal = xco.getStringValue("stringVal");
XCO xcoVal = xco.getXCOValue("xcoVal");
6. 設(shè)計圖

XCO設(shè)計圖
7. 聯(lián)系交流
- QQ群:518522232 *請備注關(guān)注的項目
- 郵箱:xson_org@126.com
- 項目地址: https://github.com/xsonorg/xco