public class EntityTest { public class Eascovery { private void XmlCreater() // 这个方法用于创建 Xml 文档:EquipDict.xml { Map<String, Equip> equipMap = new HashMap<String, Equip>(); String hql = "from Zzqcdmzdb dmzdb order by dmzdb.zbdm"; List<Zzqcdmzdb> list = service.findByHql(hql, null); for (Zzqcdmzdb zdb : list) { String zbdm = zdb.getZbdm(); String zbmc = zdb.getZbmc(); String qcdm = zdb.getQcdm(); String qcmc = zdb.getQcmc(); if (!equipMap.containsKey(zbdm)) { Equip equip = new Equip(); equip.setZbdm(zbdm); equip.setZbmc(zbmc); Instrument ins = new Instrument(); ins.setQcdm(qcdm); ins.setQcmc(qcmc); List<Instrument> listIns = new ArrayList<Instrument>(); listIns.add(ins); equip.setInstrument(listIns); equipMap.put(zbdm, equip); } else // 这种情况下 key 已经存在,你只需要从已经存在的对象中取出 { Equip equip = equipMap.get(zbdm); //Map的 get(T key) 方法是返回以Key为健的 Value值 Instrument ins = new Instrument(); // 注意Key存在了,但是器材是不同的,所以要重新创建这个对象 Instrument ins.setQcdm(qcdm); ins.setQcmc(qcmc); List<Instrument> listIns = equip.getInstrument(); listIns.add(ins); equip.setInstrument(listIns); } } List<Equip> result = new ArrayList<Equip>(); result.addAll(equipMap.values()); String resultXml = x.toXML(result); // if (log.isDebugEnabled()) // log.debug("[周转器材本级入库/export()]导出XML:" + resultXml); ByteArrayInputStream bais = null; BufferedOutputStream bos = null; try { HttpServletResponse resp = getUTFResponse(); resp.setContentType("text/xml"); resp.setHeader("Content-disposition", "attachment; filename=" + new String("EquipList.xml")); bais = new ByteArrayInputStream(resultXml.getBytes("UTF-8")); bos = new BufferedOutputStream(resp.getOutputStream()); int bytesRead; byte[] buff = new byte[4096]; while (-1 != (bytesRead = bais.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (Exception e) { log.error("操作异常", e); ajaxJson("{success:false, message:'操作异常:" + ExceptionUtils.getRootCauseMessage(e) + "'}"); } finally { if (bais != null) try { bais.close(); } catch (Exception e) { } if (bos != null) try { bos.close(); } catch (Exception e) { } } } } } ====================================================================================================================================== public class Equip { private String zbmc; private String zbdm; private List<Instrument> instrument; public String getZbmc() { return zbmc; } public void setZbmc(String zbmc) { this.zbmc = zbmc; } public String getZbdm() { return zbdm; } public void setZbdm(String zbdm) { this.zbdm = zbdm; } public List<Instrument> getInstrument() { return instrument; } public void setInstrument(List<Instrument> instrument) { this.instrument = instrument; } } ================================================================================================================================== public class Instrument { private String qcmc; private String qcdm; public String getQcmc() { return qcmc; } public void setQcmc(String qcmc) { this.qcmc = qcmc; } public String getQcdm() { return qcdm; } public void setQcdm(String qcdm) { this.qcdm = qcdm; } }