返回首页
当前位置: 主页 > 编程语言 > XML教程 >

struts 利用Jdom 生成xml字符串代码

时间:2015-06-07 22:17来源:电脑教程学习网 www.etwiki.cn 编辑:admin

1、写在action里的代码

//获取servletResponse
HttpServletResponse response=ServletActionContext.getResponse();
Format format = Format.getPrettyFormat();//利用format格式化xml
format.setEncoding("utf-8");//设置编码格式

//获取数据
List<FileDetail> list= fileManagerService.getRecentFile(this.getSessionUserId(), 7, null);
Element datatable = new Element("datatable");//xml根节点
//生成head子节点
Element head = new Element("head");
head.addContent(new Element("col").setText("文档名").setAttribute("type", "String"));
head.addContent(new Element("col").setText("下载url").setAttribute("type", "String"));
head.addContent(new Element("col").setText("上传时间").setAttribute("type", "String"));
head.addContent(new Element("col").setText("文件后缀名").setAttribute("type", "String"));
head.addContent(new Element("col").setText("下载次数").setAttribute("type", "String"));

Element body= new Element("body"); //生成body子节点
for (int i=0;i<list.size();i++)
{
Element rows = new Element("rows");// 创建节点 rows;
// 给 rows 节点添加子节点并赋值;
// new Element("name")中的 "name" 替换成表中相应字段,setText("内容")
rows.addContent(new Element("cell").setText(list.get(i).getFileEntity().getFilename()));
rows.addContent(new Element("cell").setText(list.get(i).getFileInfo().getPresenttimestr()));
rows.addContent(new Element("cell").setText("/docsManager/lookupFileDetailInfo.action?fileid="+list.get(i).getFileEntity().getPid()));
rows.addContent(new Element("cell").setText(list.get(i).getFileEntity().getFiletype()));
rows.addContent(new Element("cell").setText(String.valueOf(list.get(i).getFileEntity().getDownloadcount())));
// 给父节点body添加rows子节点;
body.addContent(rows);
}
//给datatable添加head、body节点
datatable.addContent(head);
datatable.addContent(body);

Document doc = new Document(datatable);
XMLOutputter docWriter = new XMLOutputter(format);//格式化xml
String strFromXml = docWriter.outputString(doc);

System.out.println(strFromXml);
//在html中显示xml格式的字符串
response.setContentType("text/plain; charset=UTF-8");
response.setHeader("Content-Disposition", "inline");
response.getWriter().write(strFromXml);
return null;

 

 

生成的xml格式如下:

 

<?xml version="1.0" encoding="utf-8" ?>


- <datatable>

 

- <head>

 

<col type="String">文档名</col>


<col type="String">下载url</col>


<col type="String">上传时间</col>


<col type="String">文件后缀名</col>


<col type="String">下载次数</col>

</head>


- <body>

 

- <rows>

 

<cell>UltraReport入门手册.docx</cell>


<cell />


<cell>DetailInfo.action?fileid=40288e3532ed49bd0132ed52aa7d0002</cell>


<cell>docx</cell>


<cell>1</cell>

</rows>


- <rows>

 

<cell>生成图测试图的车市图是的的的生成图测试图的车市图是的的的.rar</cell>


<cell />


<cell>DetailInfo.action?fileid=40288e3532f5abda0132f61a18430005</cell>


<cell>rar</cell>


<cell>2</cell>

</rows>


- <rows>

 

<cell>生成图测试图的车市图是的的的.rar</cell>


<cell />


<cell>DetailInfo.action?fileid=4028828332ede6810132ee09e61d0005</cell>


<cell>rar</cell>


<cell>2</cell>

</rows>


- <rows>

 

<cell>生成图测试图的车市图是的的的生成图测试图的车市图是的的的.rar</cell>


<cell />


<cell>DetailInfo.action?fileid=4028828332ede6810132ee127b310009</cell>


<cell>rar</cell>


<cell>1</cell>

</rows>


- <rows>

 

<cell>index.html</cell>


<cell />


<cell>DetailInfo.action?fileid=40288e3532ecc9c20132eced56ef0016</cell>


<cell>html</cell>


<cell>1</cell>

</rows>


- <rows>

 

<cell>效果图.rar</cell>


<cell />


<cell>FileDetailInfo.action?fileid=4028828332ee8a8f0132ee913a6b0008</cell>


<cell>rar</cell>


<cell>1</cell>

</rows>

</body>

</datatable>

------分隔线----------------------------
标签(Tag):xml教程
------分隔线----------------------------
推荐内容
猜你感兴趣