JAva spring mvc jdom xml生成およびダウン

3373 ワード

function exceptCityList(){
	var cityIds = '';
	var rows = $('#cityDataGrid').datagrid('getSelections');
	
	for (var index = 0; index < rows.length; index++) {
		cityIds+=rows[index].id+",";
	}
	var link=getRootPath()+'/statis/supportcity/exceptCityXml/'+cityIds;  
    window.open(link);  
	return false;  
	 
}	

/**
* This
* @param response
* @param cityIds
* @throws FileNotFoundException
*/
@RequestMapping(value ="/exceptCityXml/{cityIds}")
public void exceptCityXml(HttpServletResponse response,@PathVariable("cityIds") String cityIds)throws FileNotFoundException {
List cityIDList = new ArrayList();
String[] ids = cityIds.split(",");
for (String string : ids) {
if (!string.isEmpty() && !"0".equals(string)) {
cityIDList.add(Integer.parseInt(string));
}
}
Element root = new Element("plist");
root.setAttribute("version","1.0");
Element array = new Element("array");
Document doc = new Document(root);
DocType docType = new DocType("plist");
docType.setPublicID("-//Apple//DTD PLIST 1.0//EN");
docType.setSystemID("http://www.apple.com/DTDs/PropertyList-1.0.dtd");
doc.setDocType(docType);
List> cityList = null;
try {
cityList = this.supportCityService.selectCityListByCityIdList(cityIDList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (cityList != null) {
for (Map city : cityList) {
Element element =new Element("dict");
element.addContent(new Element("key").setText("center"));
element.addContent(new Element("string").setText(city.get("cityLat") +","+ city.get("ctiyLng")));
element.addContent(new Element("key").setText("cityCode"));
element.addContent(new Element("string").setText(city.get("cityCode").toString()));
element.addContent(new Element("key").setText("name"));
element.addContent(new Element("string").setText(city.get("cityChName").toString()));
element.addContent(new Element("key").setText("enName"));
element.addContent(new Element("string").setText(city.get("cityName").toString()));
array.addContent(element);
}
root.addContent(array);
}
XMLOutputter xmlOut = new XMLOutputter();
Format f = Format.getPrettyFormat();
f.setEncoding("UTF-8");
xmlOut.setFormat(f);
String content = xmlOut.outputString(doc);
InputStream inStream = new ByteArrayInputStream(content.getBytes());
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename=citylist.plist");
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}