求解ArrayList转化为xml文件输出?

SWQQ- 2011-06-09 02:06:14
UserGameInfo 实体代码

@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;
}

}


输出的xml 格式为以下形式

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


不知道各位大侠都怎样实现?求解对xml这不是很熟练,如果对一个一个的节点赋值组合太多垃圾代码

自己实现了一些但是在赋值的时候总是有问题 以下是实现代码段

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()));
}
}
}


在对节点赋值的时候应该怎样具体操作 求解? 谢谢
...全文
263 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Spring源码解析 2011-06-09
  • 打赏
  • 举报
回复
不错,学习了
SWQQ- 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hy158753228 的回复:]

我也来一个:
Java code

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.dom4……
[/Quote]

ele.setText(BeanUtils.getSimpleProperty(obj, fieldName));

这段代码就是我需要的,一行代码代替了10行代码。一直不知道还有个BeanUtils 类看样我out了,
谢谢了。
SWQQ- 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 inhibitory 的回复:]

XStream非常简单
Java code
import java.io.Serializable;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class XmlTest {
public static void main(S……
[/Quote]

6楼的答案却是我要的,不过还要感谢你。
hy158753228 2011-06-09
  • 打赏
  • 举报
回复
我也来一个:

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();
}
}
}
Inhibitory 2011-06-09
  • 打赏
  • 举报
回复
XStream非常简单
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;
}
}
huntor 2011-06-09
  • 打赏
  • 举报
回复
xstream / jaxb
SWQQ- 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 huntor 的回复:]

simple
[/Quote]

想自己构建不知道有什么可行的方法?
huntor 2011-06-09
  • 打赏
  • 举报
回复
SWQQ- 2011-06-09
  • 打赏
  • 举报
回复
那帮忙给点提示撒~不要应代码的一个一个赋值。除了这个方法还有其他更方便的方法吗?
ArrayList 转换为MapArrayList> 弄了两三个小时,终于解决了。 实现功能: Object对象包括两变量,id和name 将ArrayList中的id转变为Map中的key值,将与id对应的name放入Map的ArrayList中 使用的方法:遍历Map,使用for (Map.EntryArrayList> entry : nodeMap.entrySet())需要注意的地方: 最后将li

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧