<Messages> <Organization>03310100</Organization> <ServiceType>test001</ServiceType> <Extension/> <StartTime>2008-11-10 06:00</StartTime> <Expiry>2015-11-10 23:00</Expiry> <AppId>sx2009bzxcdxt</AppId> <Pad1>10.192.113.11</Pad1> <Message> <Receiver>15601826467</Receiver> <Contents>testContents> </Message> </Messages>
创建如上面的xml文件,可以采用另一种方式:
/** * 创建XML报文 * * @param fileName * @param contents * @param phoneNum * @return */ public String getXML(String filename,String contents,String phoneNum,TUnit unit) { SAXReader saxReader = new SAXReader(); File file; XMLWriter writer = null; try { file = new File(filename); Document document = saxReader.read(file); Node node = document .selectSingleNode("//Messages/Message/Receiver"); if (phoneNum != null) { node.setText(phoneNum); } node=document.selectSingleNode("//Messages/Organization"); if(!StringUtil.isNull(unit.getSmsOrgCode())){ node.setText(unit.getSmsOrgCode()); } node=document.selectSingleNode("//Messages/Extension"); if(!StringUtil.isNull(unit.getSmsExt())){ node.setText(unit.getSmsExt()); } node = document.selectSingleNode("//Messages/Message/Contents"); node.setText(contents); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gbk"); StringWriter out = new StringWriter(); writer = new XMLWriter(out,format); writer.write(document); return out.toString(); } catch (Exception e) { log.error("创建XML文件失败",e); } finally { try { writer.close(); } catch (Exception e) { log.error("创建XML文件失败"); } } }原文链接:https://www.f2er.com/xml/300282.html