CDO cdo1= new CDO(); cdo1.setIntegerValue("classid", 1); cdo1.setStringValue("classname", "一年級一班"); cdo1.setStringArrayValue("alias",new String[]{"尖子班級","優秀班級"} ); cdo1.setStringArrayValue("students", new String[]{"張三","李四"}); CDO scdo1= new CDO(); scdo1.setStringValue("username", "userone"); scdo1.setStringValue("password", "......."); scdo1.setIntegerValue("age", 18); cdo1.setCDOValue("scdo1", scdo1);
若是將它轉化爲XML表示以下:
<?xml version="1.0" encoding="UTF-8"?> <CDO> <NF N="classid" V="1"/> <STRF N="classname" V="一年級一班"/> <STRAF N="alias"> <STR>尖子班級</STR> <STR>優秀班級</STR> </STRAF> <STRAF N="students"> <STR>張三</STR> <STR>李四</STR> </STRAF> <CDOF N="scdo1"> <CDO> <STRF N="username" V="userone"/> <STRF N="password" V="..........."/> <NF N="age" V="18"/> </CDO> </CDOF> </CDO>
若是將這個對象轉化爲Json,將會有以下的表示:
{ "classid":1, "classname":"一年級一班", "alias":[ "尖子班級", "優秀班級" ], "students":[ "張三", "李四" ], "scdo1":{ "username":"userone", "password":"...........", "age":18 } }
能夠看出CDO這個數據結構仍是設計的是很是優秀的。java
在咱們以往的開發中,若是你們稍加思索的話,你們都在寫好多好多的bean,每一個bean對應一個實體Entity,這樣在開發大型的項目的過程當中會感受不合理,經過CDO這個結構,不須要針對每一個entity都編寫一個java bean類,倘若咱們有學生和班級兩類實體,若是是按照以往的設計,可能會先設計出兩個bean class:
數組
package com.google.beans; public class Student { private Integer id; private String username; private String address; private boolean sex; private double height; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } }
package com.google.beans; public class Classes { private Integer classId; private String className; private Student[] students; public Integer getClassId() { return classId; } public void setClassId(Integer classId) { this.classId = classId; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public Student[] getStudents() { return students; } public void setStudents(Student[] students) { this.students = students; } }
每一個實體bean對應一個Class,若是實體多的話維護起來會很不方便,經過CDO,省去了這些代碼:
CDO cdoStudent1 = new CDO(); cdoStudent1.setIntegerValue("id", 1); cdoStudent1.setStringValue("username", "userone"); cdoStudent1.setStringValue("address", "上海市。。。。。。"); cdoStudent1.setBooleanValue("sex", true); cdoStudent1.setDoubleValue("height", 1.74); CDO cdoStudent2 = new CDO(); cdoStudent2.setIntegerValue("id", 2); cdoStudent2.setStringValue("username", "usertwo"); cdoStudent2.setStringValue("address", "深圳市"); cdoStudent2.setBooleanValue("sex", false); cdoStudent2.setDoubleValue("height", 1.67); CDO cdoClasses = new CDO(); cdoClasses.setIntegerValue("classId", 1); cdoClasses.setStringValue("className", "一年級一班"); cdoClasses.setCDOArrayValue("students", new CDO[]{cdoStudent1,cdoStudent2});
它們的XML表示分別爲:
<?xml version="1.0" encoding="UTF-8"?> <CDO> <NF N="id" V="1"/> <STRF N="username" V="userone"/> <STRF N="address" V="上海市。。。。。。"/> <BF N="sex" V="true"/> <DBLF N="height" V="1.74"/> </CDO>
<?xml version="1.0" encoding="UTF-8"?> <CDO> <NF N="id" V="2"/> <STRF N="username" V="usertwo"/> <STRF N="address" V="深圳市"/> <BF N="sex" V="false"/> <DBLF N="height" V="1.67"/> </CDO>
<?xml version="1.0" encoding="UTF-8"?> <CDO> <NF N="classId" V="1"/> <STRF N="className" V="一年級一班"/> <CDOAF N="students"> <CDO> <NF N="id" V="1"/> <STRF N="username" V="userone"/> <STRF N="address" V="上海市。。。。。。"/> <BF N="sex" V="true"/> <DBLF N="height" V="1.74"/> </CDO> <CDO> <NF N="id" V="2"/> <STRF N="username" V="usertwo"/> <STRF N="address" V="深圳市"/> <BF N="sex" V="false"/> <DBLF N="height" V="1.67"/> </CDO> </CDOAF> </CDO>
很是的清晰明瞭!數據結構
CDO不但能方便地轉化爲XML表示,並且能很方便的把XML文本串轉化爲CDO對象,假設有以下的XML文本串:
app
<?xml version="1.0" encoding="UTF-8"?> <CDO> <CDOF N="cdoReturn"> <CDO> <NF N="nCode" V="0"/> <STRF N="strText" V="OK"/> <STRF N="strInfo" V="OK"/> </CDO> </CDOF> <CDOF N="cdoResponse"> <CDO> <CDOAF N="cdosMember"> <CDO> <NF N="uid" V="5"/> <STRF N="username" V="wanghui"/> <STRF N="password" V="e4994c81a0c0db8e1ab137f9fdf7d5ae"/> <STRF N="secques" V=""/> </CDO> <CDO> <NF N="uid" V="353"/> <STRF N="username" V="bainiuwang"/> <STRF N="password" V="35b36dd98779303e3cdc84ff3951be9e"/> <STRF N="secques" V=""/> </CDO> <CDO> <NF N="uid" V="636"/> <STRF N="username" V="wangli3849"/> <STRF N="password" V="e3b2b5b80d57c3254c0ed1a83059d7ce"/> <STRF N="secques" V=""/> </CDO> <CDO> <NF N="uid" V="1006"/> <STRF N="username" V="wangle520208"/> <STRF N="password" V="059e5d9a6347d61859d7e2b740ef721b"/> <STRF N="secques" V=""/> </CDO> <CDO> <NF N="uid" V="1087"/> <STRF N="username" V="wangyuepeng"/> <STRF N="password" V="a5c74d745f45875d62fb418ed7e65ebb"/> <STRF N="secques" V=""/> </CDO> </CDOAF> <NF N="count" V="5"/> </CDO> </CDOF> </CDO>
經過以下的代碼即可轉化爲CDO對象:
StringBuilder builder = new StringBuilder(); builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); builder.append("<CDO>"); builder.append(" <CDOF N=\"cdoReturn\">"); builder.append(" <CDO>"); builder.append(" <NF N=\"nCode\" V=\"0\"/>"); builder.append(" <STRF N=\"strText\" V=\"OK\"/>"); builder.append(" <STRF N=\"strInfo\" V=\"OK\"/>"); builder.append(" </CDO>"); builder.append(" </CDOF>"); builder.append(" <CDOF N=\"cdoResponse\">"); builder.append(" <CDO>"); builder.append(" <CDOAF N=\"cdosMember\">"); builder.append(" <CDO>"); builder.append(" <NF N=\"uid\" V=\"5\"/>"); builder.append(" <STRF N=\"username\" V=\"wanghui\"/>"); builder.append(" <STRF N=\"password\" V=\"e4994c81a0c0db8e1ab137f9fdf7d5ae\"/>"); builder.append(" <STRF N=\"secques\" V=\"\"/>"); builder.append(" </CDO>"); builder.append(" <CDO>"); builder.append(" <NF N=\"uid\" V=\"353\"/>"); builder.append(" <STRF N=\"username\" V=\"bainiuwang\"/>"); builder.append(" <STRF N=\"password\" V=\"35b36dd98779303e3cdc84ff3951be9e\"/>"); builder.append(" <STRF N=\"secques\" V=\"\"/>"); builder.append(" </CDO>"); builder.append(" <CDO>"); builder.append(" <NF N=\"uid\" V=\"636\"/>"); builder.append(" <STRF N=\"username\" V=\"wangli3849\"/>"); builder.append(" <STRF N=\"password\" V=\"e3b2b5b80d57c3254c0ed1a83059d7ce\"/>"); builder.append(" <STRF N=\"secques\" V=\"\"/>"); builder.append(" </CDO>"); builder.append(" <CDO>"); builder.append(" <NF N=\"uid\" V=\"1006\"/>"); builder.append(" <STRF N=\"username\" V=\"wangle520208\"/>"); builder.append(" <STRF N=\"password\" V=\"059e5d9a6347d61859d7e2b740ef721b\"/>"); builder.append(" <STRF N=\"secques\" V=\"\"/>"); builder.append(" </CDO>"); builder.append(" <CDO>"); builder.append(" <NF N=\"uid\" V=\"1087\"/>"); builder.append(" <STRF N=\"username\" V=\"wangyuepeng\"/>"); builder.append(" <STRF N=\"password\" V=\"a5c74d745f45875d62fb418ed7e65ebb\"/>"); builder.append(" <STRF N=\"secques\" V=\"\"/>"); builder.append(" </CDO>"); builder.append(" </CDOAF>"); builder.append(" <NF N=\"count\" V=\"5\"/>"); builder.append(" </CDO>"); builder.append(" </CDOF>"); builder.append("</CDO>"); CDO cdoResponse = CDO.fromXML(builder.toString()); System.out.println(cdoResponse.toJSON());
這樣即可打印出以下的Json字符串:
{ "cdoReturn":{ "nCode":0, "strText":"OK", "strInfo":"OK" }, "cdoResponse":{ "cdosMember":[ { "uid":5, "username":"wanghui", "password":"e4994c81a0c0db8e1ab137f9fdf7d5ae", "secques":"" }, { "uid":353, "username":"bainiuwang", "password":"35b36dd98779303e3cdc84ff3951be9e", "secques":"" }, { "uid":636, "username":"wangli3849", "password":"e3b2b5b80d57c3254c0ed1a83059d7ce", "secques":"" }, { "uid":1006, "username":"wangle520208", "password":"059e5d9a6347d61859d7e2b740ef721b", "secques":"" }, { "uid":1087, "username":"wangyuepeng", "password":"a5c74d745f45875d62fb418ed7e65ebb", "secques":"" } ], "count":5 } }
確實是很是的方便!框架
CDO數據結構是整個CDO框架的核心,理解並掌握了這個數據接口,能夠說掌握了CDO框架的百分之六十!ui