java list 生成 soap

来源:互联网 发布:淘宝手机模拟器安卓版 编辑:程序博客网 时间:2024/05/17 22:11
  1. package com.seahigh.acs.soap;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.xml.namespace.QName;  
  7. import javax.xml.parsers.DocumentBuilderFactory;  
  8. import javax.xml.soap.MessageFactory;  
  9. import javax.xml.soap.SOAPBody;  
  10. import javax.xml.soap.SOAPElement;  
  11. import javax.xml.soap.SOAPEnvelope;  
  12. import javax.xml.soap.SOAPException;  
  13. import javax.xml.soap.SOAPFactory;  
  14. import javax.xml.soap.SOAPHeader;  
  15. import javax.xml.soap.SOAPMessage;  
  16. import javax.xml.soap.SOAPPart;  
  17. import javax.xml.transform.OutputKeys;  
  18. import javax.xml.transform.Source;  
  19. import javax.xml.transform.Transformer;  
  20. import javax.xml.transform.TransformerFactory;  
  21. import javax.xml.transform.dom.DOMSource;  
  22. import javax.xml.transform.sax.SAXSource;  
  23. import javax.xml.transform.stream.StreamResult;  
  24.   
  25. import org.w3c.dom.Document;  
  26. import org.w3c.dom.Node;  
  27. import org.xml.sax.InputSource;  
  28. /** 
  29.  *  
  30.  * @author 汪心利 
  31.  * Date 2010-3-30下午02:51:30 
  32.  * (c)CopyRight seahigh 2010 
  33.  */  
  34. public class SoapUtil {  
  35.     public SOAPPart initSoapPart() throws SOAPException {  
  36.   
  37.         SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();  
  38.   
  39.         SOAPPart soapPart = soapMessage.getSOAPPart();  
  40.   
  41.         SOAPEnvelope soapEnvelope = soapPart.getEnvelope();  
  42.         SOAPHeader soapHeader = soapEnvelope.getHeader();  
  43.         SOAPElement cwmp = soapEnvelope.addNamespaceDeclaration("cwmp",  
  44.                 "urn:dslforum-org:cwmp-1-0");  
  45.         SOAPElement xsi = soapEnvelope.addNamespaceDeclaration("xsi",  
  46.                 "http://www.w3.org/2001/XMLSchema-instance");  
  47.         SOAPElement xsd = soapEnvelope.addNamespaceDeclaration("xsd",  
  48.                 "http://www.w3.org/2001/XMLSchema");  
  49.   
  50.         SOAPElement enc = soapEnvelope.addNamespaceDeclaration("SOAP-ENC",  
  51.                 "http://schemas.xmlsoap.org/soap/encoding/");  
  52.   
  53.         SOAPElement id = soapHeader.addChildElement("ID""cwmp");  
  54.         id.setTextContent("1");  
  55.         return soapPart;  
  56.     }  
  57.   
  58.     public void soap2String(Source source) throws Exception {  
  59.         if (source != null) {  
  60.             Node root = null;  
  61.             if (source instanceof DOMSource) {  
  62.                 root = ((DOMSource) source).getNode();  
  63.             } else if (source instanceof SAXSource) {  
  64.                 InputSource inSource = ((SAXSource) source).getInputSource();  
  65.                 DocumentBuilderFactory dbf = DocumentBuilderFactory  
  66.                         .newInstance();  
  67.                 dbf.setNamespaceAware(true);  
  68.                 Document doc = dbf.newDocumentBuilder().parse(inSource);  
  69.                 root = (Node) doc.getDocumentElement();  
  70.             }  
  71.             Transformer transformer = TransformerFactory.newInstance()  
  72.                     .newTransformer();  
  73.             transformer.setOutputProperty(OutputKeys.INDENT, "yes");  
  74.             transformer.transform(new DOMSource(root), new StreamResult(  
  75.                     System.out));  
  76.         }  
  77.     }  
  78.   
  79.     public Source informResponse(SOAPPart part) throws Exception {  
  80.         SOAPEnvelope soapEnvelope = part.getEnvelope();  
  81.         SOAPBody soapBody = soapEnvelope.getBody();  
  82.         SOAPElement informRes = soapBody.addChildElement("InformResponse",  
  83.                 "cwmp");  
  84.         SOAPElement max = SOAPFactory.newInstance().createElement(  
  85.                 "MaxEnvelopes""""");  
  86.         max.setTextContent("1");  
  87.         informRes.addChildElement(max);  
  88.         return part.getContent();  
  89.     }  
  90.   
  91.     public Source getParameterValues(SOAPPart part, List list) throws Exception {  
  92.         SOAPEnvelope soapEnvelope = part.getEnvelope();  
  93.         SOAPBody soapBody = soapEnvelope.getBody();  
  94.         SOAPElement informRes = soapBody.addChildElement("GetParameterValues",  
  95.                 "cwmp");  
  96.         SOAPFactory soapFactory = SOAPFactory.newInstance();  
  97.         SOAPElement names = soapFactory.createElement("ParameterNames""""");  
  98.         names.addAttribute(new QName("SOAP-ENC:arrayType"), "xsd:string["  
  99.                 + list.size() + "]");  
  100.         SOAPElement nameElement = null;  
  101.         for (int i = 0; i < list.size(); i++) {  
  102.             nameElement = soapFactory.createElement("string""""");  
  103.             nameElement.setTextContent((String) list.get(i));  
  104.             names.addChildElement(nameElement);  
  105.         }  
  106.         informRes.addChildElement(names);  
  107.         return part.getContent();  
  108.     }  
  109.   
  110.     public static void main(String[] args) throws Exception {  
  111.         SoapUtil util = new SoapUtil();  
  112.         SOAPPart part = util.initSoapPart();  
  113.         Source inform = util.getParameterValues(part, util.getTestnames());  
  114.         util.soap2String(inform);  
  115.     }  
  116.   
  117.     public List<String> getTestnames() {  
  118.         List<String> list = new ArrayList<String>();  
  119.         list.add("InternetGatewayDevice.DeviceInfo.X_CT-COM_CPU");  
  120.         list.add("InternetGatewayDevice.DeviceInfo.X_CT-COM_WorkTime");  
  121.         list.add("InternetGatewayDevice.DeviceInfo.SoftwareVersion");  
  122.         list.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID");  
  123.         list  
  124.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_ReceiveNoise");  
  125.         list  
  126.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalBytesReceived");  
  127.         list  
  128.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalBytesSent");  
  129.         list  
  130.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalBytesReceived");  
  131.         list  
  132.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalBytesSent");  
  133.         list  
  134.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalPacketsReceived");  
  135.         list  
  136.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalPacketsSent");  
  137.         list  
  138.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalPacketsReceived");  
  139.         list  
  140.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalPacketsSent");  
  141.         list  
  142.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.ResponsePass");  
  143.         list  
  144.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.AskPass");  
  145.         list  
  146.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.SuccessPass");  
  147.         list.add("InternetGatewayDevice.DeviceInfo.X_CT-COM_Temp");  
  148.         list  
  149.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.LAN-PacketsError");  
  150.         list  
  151.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.LAN-TotalBytesReceived");  
  152.         list  
  153.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.LAN-TotalBytesSent");  
  154.         list  
  155.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.WAN-TotalBytesReceived");  
  156.         list  
  157.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.WAN-PacketsError");  
  158.         list  
  159.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.WAN-TotalBytesSent");  
  160.         list  
  161.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AssociatedDevice.1.X_CT-COM_ReceiveRate");  
  162.         list  
  163.                 .add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AssociatedDevice.1.X_CT-COM_SendRate");  
  164.         list.add("InternetGatewayDevice.DeviceInfo.SerialNumber");  
  165.         list.add("InternetGatewayDevice.DeviceInfo.ManufacturerOUI");  
  166.         return list;  
  167.     }  

原创粉丝点击