zoukankan      html  css  js  c++  java
  • dom4j创建xml报文并生成

    我使用的是dom4j创建xml报文

    1.创建root节点
             Document document = DocumentHelper.createDocument();  
             Element root = DocumentHelper.createElement("RBSPMessage");  
             document.setRootElement(root); 

    2.在root节点下添加节点
             Element Version = root.addElement("Version");
             Element SenderIDElement = root.addElement("SenderID");

    3.为节点添加值

             SenderIDElement.setText("C00-10002029");
    4.为节点添加属性值
             Element Security = root.addElement("Security");
             Element Signature = Security.addElement("Signature");
             Signature.addAttribute("Algorithm","des");
    5.生成xml报文
             OutputFormat format = OutputFormat.createCompactFormat();
              format.setEncoding("UTF-8");
              StringWriter writer = new StringWriter();
              XMLWriter output = new XMLWriter(writer, format);
              output.write(document);
              writer.close();
              output.close();
              return writer.toString();

    6.返回报文后,获取某某节点有几个节点。

              Document document = DocumentHelper.parseText(xml);
              Element rootElement = document.getRootElement();
              List list =rootElement.selectNodes("/RBSPMessage/Method/Items/Item/Value/Row");
              result = String.valueOf(list.size());

  • 相关阅读:
    Windows8 游戏开发教程开篇
    IPAD 游戏开发方案,windows开发
    谈一谈 Windows 8 的软件开发架构
    html5 大家一起来瞅吧瞅吧
    silverlight5中CLGF的推进
    html5 游戏界面问题
    挑战SVN,最纯洁的SVN客户端计划
    章鱼哥的暴走,HTML5也开始了
    MySQL的常用操作!
    两种屏蔽鼠标右键的方法
  • 原文地址:https://www.cnblogs.com/hmpcly/p/9674147.html
Copyright © 2011-2022 走看看