加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice 返回自定义xml

发布时间:2020-12-17 01:06:31 所属栏目:安全 来源:网络整理
导读:webservice返回自定义XML: ?xml version="1.0" encoding="utf-8" ?? returnInfo sysInfo ? SucessFlag0000/SucessFlag ? Error获取数据成功/Error ? /sysInfo result ? month201105/month ? account666666/account ? userName张三/userName ? addressxx市xx

webservice返回自定义XML:
<?xml version="1.0" encoding="utf-8" ?>?
<returnInfo>
<sysInfo>
? <SucessFlag>0000</SucessFlag>
? <Error>获取数据成功</Error>
? </sysInfo>
<result>
? <month>201105</month>
? <account>666666</account>
? <userName>张三</userName>
? <address>xx市xx区xx街XX号</address>
? <recordTime>5</redcordTime>
? <recorder>10001</record>
? <payment>123:45</payent>
? <lastDate>20110530</lastdate>
? <remark>操过最后期限需要额外缴纳滞纳金</remark>
<dataList>
<item>
? <lastCount>1234</lastCount>
? <thisCount>1244</thisCount>
? <consumption>10</consumption>
? <unitPrice>0.56</unitPrice>
? <amount>5.60</amount>
</item>
… …
</dataList>
</result>
</returnInfo>

?

?import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class XmlBean { /** * @param args */ public static Document GetDocument() { Document document = DocumentHelper.createDocument(); Element root = document.addElement("root"); Element author1 = root.addElement("Lynch"); author1.addAttribute("Age","25"); author1.addAttribute("Country","China"); author1.addText("I am great!"); Element author11 = author1.addElement("ssss"); author11.addAttribute("Age","25"); author11.addAttribute("Country","中国"); author11.addText("I am great!"); Element author2 = root.addElement("Legend"); author2.addAttribute("Age","25"); author2.addAttribute("Country","China"); author2.addText("I am great!too!"); return document; } public static String GetXMLString() {//将XML文件构造成String形式返回 StringWriter sw = new StringWriter(); XMLWriter writer = null; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GBK"); try { writer = new XMLWriter(format); writer.setWriter(sw); writer.write(GetDocument()); writer.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println(sw.toString()); return sw.toString(); } public static void CreateXMLFile() {//在服务器端创建XML文件 try { // Document document = // DocumentHelper.parseText(GetXMLString());//将字符串转化为Document对象 Document document = GetDocument();// 自行构造Document对象 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileWriter( "../swt//demo3.xml"),format);// 格式化输出 // XMLWriter writer = new XMLWriter(new // FileWriter("../json/src/demo3.xml")); writer.write(document); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub //CreateXMLFile(); GetXMLString(); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读