62,615
社区成员
发帖
与我相关
我的任务
分享
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="UserGameInfoService")
public class UserGameInfo implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructors UserGameInfo
*
*/
public UserGameInfo() {
// TODO Auto-generated constructor stub
}
public int id;
public String username;
public String gamescore;
public String gamespendtime;
public String currentdate;
public String grade;
public String rowcount;
public String colcount;
public String gamestatus;
public String spycount;
public String getEntityAttribute()
{
return null;
}
/**
* id Getter
*
* @return id
*/
public int getId() {
return id;
}
/**
* id Setter
*
* @param id Set id
*/
public void setId(int id) {
this.id = id;
}
/**
* username Getter
*
* @return username
*/
public String getUsername() {
return username;
}
/**
* username Setter
*
* @param username Set username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* gamescore Getter
*
* @return gamescore
*/
public String getGamescore() {
return gamescore;
}
/**
* gamescore Setter
*
* @param gamescore Set gamescore
*/
public void setGamescore(String gamescore) {
this.gamescore = gamescore;
}
/**
* gamespendtime Getter
*
* @return gamespendtime
*/
public String getGamespendtime() {
return gamespendtime;
}
/**
* gamespendtime Setter
*
* @param gamespendtime Set gamespendtime
*/
public void setGamespendtime(String gamespendtime) {
this.gamespendtime = gamespendtime;
}
/**
* currentdate Getter
*
* @return currentdate
*/
public String getCurrentdate() {
return currentdate;
}
/**
* currentdate Setter
*
* @param currentdate Set currentdate
*/
public void setCurrentdate(String currentdate) {
this.currentdate = currentdate;
}
/**
* grade Getter
*
* @return grade
*/
public String getGrade() {
return grade;
}
/**
* grade Setter
*
* @param grade Set grade
*/
public void setGrade(String grade) {
this.grade = grade;
}
/**
* rowcount Getter
*
* @return rowcount
*/
public String getRowcount() {
return rowcount;
}
/**
* rowcount Setter
*
* @param rowcount Set rowcount
*/
public void setRowcount(String rowcount) {
this.rowcount = rowcount;
}
/**
* colcount Getter
*
* @return colcount
*/
public String getColcount() {
return colcount;
}
/**
* colcount Setter
*
* @param colcount Set colcount
*/
public void setColcount(String colcount) {
this.colcount = colcount;
}
/**
* gamestatus Getter
*
* @return gamestatus
*/
public String getGamestatus() {
return gamestatus;
}
/**
* gamestatus Setter
*
* @param gamestatus Set gamestatus
*/
public void setGamestatus(String gamestatus) {
this.gamestatus = gamestatus;
}
/**
* spycount Getter
*
* @return spycount
*/
public String getSpycount() {
return spycount;
}
/**
* spycount Setter
*
* @param spycount Set spycount
*/
public void setSpycount(String spycount) {
this.spycount = spycount;
}
}
<root>
<user>
<id>0</id>
<username>gaoxin</username>
<gamescore>0</gamescore>
<gamespendtime>00:00:01</gamespendtime>
<currentdate>2010-12-17 13:48:12</currentdate>
<grade>Primary</grade>
<rowcount>12</rowcount>
<colcount>10</colcount>
<gamestatus>UNSUCCESSFUL</gamestatus>
<spycount>15</spycount>
</user>
</root>
Element root = document.createElement("root");
// 添加到文档中
document.appendChild(root);
//创建子跟元素user
for (int i = 0; i < list.size(); i++) {
root.appendChild(document.createElement("user"));
}
//添加属性
NodeList nodeList = document.getElementsByTagName("user");
//添加子节点
int size = nodeList.getLength();
for (int k = 0; k < size; k++) {
Node node = nodeList.item(k);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element elementNode = (Element) node;
//循环添加子节点
for(int s=0; s<UserGameInfo.class.getFields().length; s++)
{
Field field = UserGameInfo.class.getFields()[s];
elementNode.appendChild(document.createElement(field.getName()));
}
}
}
package csdn.impulsehu.june;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class XMLUtil {
public static void main(String[] args) {
UserGameInfo ugi = new UserGameInfo();
ugi.setId(0);
ugi.setUsername("gaoxin");
ugi.setColcount("10");
ugi.setCurrentdate("2010-12-17 13:48:12");
ugi.setGamescore("0");
ugi.setGamespendtime("00:00:01");
ugi.setGamestatus("UNSUCCESSFUL");
ugi.setGrade("Primary");
ugi.setRowcount("12");
ugi.setSpycount("15");
createXMLDoc(ugi, "usergameinfo.xml");
}
public static void createXMLDoc(Object obj, String fileName) {
Document doc = null;
XMLWriter writer = null;
try {
doc = DocumentHelper.createDocument();
writer = new XMLWriter(new FileOutputStream(new File(fileName)));
Element root = DocumentHelper.createElement("root");
doc.setRootElement(root);
Element user = root.addElement("user");
Field[] properties = obj.getClass().getFields();
for(Field field : properties) {
String fieldName = field.getName();
Element ele = user.addElement(fieldName);
ele.setText(BeanUtils.getSimpleProperty(obj, fieldName));
}
writer.write(doc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.Serializable;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class XmlTest {
public static void main(String[] args) {
XStream xstream = new XStream(new DomDriver());
UserGameInfo info = new UserGameInfo();
info.setUsername("Biao");
String xml = xstream.toXML(info);
System.out.println(xml);
}
}
class UserGameInfo implements Serializable {
private static final long serialVersionUID = 1L;
public UserGameInfo() {
}
public int id;
public String username;
public String gamescore;
public String gamespendtime;
public String currentdate;
public String grade;
public String rowcount;
public String colcount;
public String gamestatus;
public String spycount;
public String getEntityAttribute() {
return null;
}
/**
* id Getter
*
* @return id
*/
public int getId() {
return id;
}
/**
* id Setter
*
* @param id
* Set id
*/
public void setId(int id) {
this.id = id;
}
/**
* username Getter
*
* @return username
*/
public String getUsername() {
return username;
}
/**
* username Setter
*
* @param username
* Set username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* gamescore Getter
*
* @return gamescore
*/
public String getGamescore() {
return gamescore;
}
/**
* gamescore Setter
*
* @param gamescore
* Set gamescore
*/
public void setGamescore(String gamescore) {
this.gamescore = gamescore;
}
/**
* gamespendtime Getter
*
* @return gamespendtime
*/
public String getGamespendtime() {
return gamespendtime;
}
/**
* gamespendtime Setter
*
* @param gamespendtime
* Set gamespendtime
*/
public void setGamespendtime(String gamespendtime) {
this.gamespendtime = gamespendtime;
}
/**
* currentdate Getter
*
* @return currentdate
*/
public String getCurrentdate() {
return currentdate;
}
/**
* currentdate Setter
*
* @param currentdate
* Set currentdate
*/
public void setCurrentdate(String currentdate) {
this.currentdate = currentdate;
}
/**
* grade Getter
*
* @return grade
*/
public String getGrade() {
return grade;
}
/**
* grade Setter
*
* @param grade
* Set grade
*/
public void setGrade(String grade) {
this.grade = grade;
}
/**
* rowcount Getter
*
* @return rowcount
*/
public String getRowcount() {
return rowcount;
}
/**
* rowcount Setter
*
* @param rowcount
* Set rowcount
*/
public void setRowcount(String rowcount) {
this.rowcount = rowcount;
}
/**
* colcount Getter
*
* @return colcount
*/
public String getColcount() {
return colcount;
}
/**
* colcount Setter
*
* @param colcount
* Set colcount
*/
public void setColcount(String colcount) {
this.colcount = colcount;
}
/**
* gamestatus Getter
*
* @return gamestatus
*/
public String getGamestatus() {
return gamestatus;
}
/**
* gamestatus Setter
*
* @param gamestatus
* Set gamestatus
*/
public void setGamestatus(String gamestatus) {
this.gamestatus = gamestatus;
}
/**
* spycount Getter
*
* @return spycount
*/
public String getSpycount() {
return spycount;
}
/**
* spycount Setter
*
* @param spycount
* Set spycount
*/
public void setSpycount(String spycount) {
this.spycount = spycount;
}
}