soap类型的xml报⽂如何使⽤Java⽣成?
1、什么是soap?
英⽂全称:Simple Object Access Protocol,简单对象访问协议是交换数据的⼀种协议规范,是⼀种轻量的、简单的、基于(下的⼀个⼦集)的协议,它被设计成在WEB上交换结构化的和固化的信息。
2、SOAP格式:
1 2 3 4 5 6 7 8<SOAP-ENV:Envelope  各种属性>
<!--百度百科⽰例-->
 <SOAP:HEADER>
 </SOAP:HEADER>
 <SOAP:Body>
 </SOAP:Body>
</SOAP-ENV:Envelope>
主要在web服务中运⽤。
3、语法规则
构建模块
⼀条 SOAP 消息就是⼀个普通的 XML ⽂档,包含下列元素:
必需的 Envelope 元素,可把此 XML ⽂档标识为⼀条 SOAP 消息可选的 Header 元素,包含头部信息
必需的 Body 元素,包含所有的调⽤和响应信息
可选的 Fault 元素,提供有关在处理此消息所发⽣错误的信息
这⾥是⼀些重要的语法规则:
SOAP 消息必须⽤ XML 来编码
SOAP 消息必须使⽤ SOAP Envelope 命名空间
SOAP 消息必须使⽤ SOAP Encoding 命名空间
SOAP 消息不能包含 DTD 引⽤
SOAP 消息不能包含 XML 处理指令
消息基本结构
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17<?xml
 version="1.0"?>
<soap:Envelope
 xmlns:soap=""
 soap:encodingStyle=""> <soap:Header>
<!--百度百科⽰例-->
</soap:Header>
<soap:Body>
<!--百度百科⽰例-->
<soap:Fault>
<!--百度百科⽰例-->
</soap:Fault>
</soap:Body>
</soap:Envelope>
4、案例分享
代码
soap;
import java.util.ArrayList;
import java.util.List;
import Document;
l.namespace.QName;
l.parsers.DocumentBuilderFactory;
l.soap.MessageFactory;
l.soap.SOAPBody;
l.soap.SOAPElement;
l.soap.SOAPEnvelope;
l.soap.SOAPException;
l.soap.SOAPFactory;
l.soap.SOAPHeader;
l.soap.SOAPMessage;
l.soap.SOAPPart;
l.transform.OutputKeys;
l.transform.Source;
l.transform.Transformer;
l.transform.TransformerFactory;
l.transform.dom.DOMSource;
l.transform.sax.SAXSource;
l.transform.stream.StreamResult;
import org.w3c.dom.Node;
l.sax.InputSource;
public class SOAPUtil {
public static void main(String[] args) throws Exception {
//创建本类的对象
SOAPUtil util = new SOAPUtil();
//调⽤本类的⽅法
SOAPPart part = util.initsoappart();
//获取返回的soap报⽂
Source inform = parametervalues(part, TestNames());
//输出这些soap报⽂到控制台,如果是我,我只需要封装就⾏了
util.soap2string(inform);
}
/
*
* 总结:
* 1、既然有set元素,那么就可以进⾏get元素中的内容
*/
/**
* 封装命名空间、请求头
* @return
* @throws SOAPException
*/
public SOAPPart initsoappart() throws SOAPException {
//创建SoapMessage
SOAPMessage soapmessage = wInstance().createMessage();
//创建SoapPart
SOAPPart soappart = SOAPPart();
//创建SoapEnvelope
SOAPEnvelope soapenvelope = Envelope();
//创建Header
SOAPHeader soapheader = Header();
//创建命名空间声明
//实际的报⽂输出:SOAP-ENV、cwmp、soap-enc、xsd、xsi
//维度没有第⼀个?说明第⼀个是默认的
SOAPElement cwmp = soapenvelope.addNamespaceDeclaration("cwmp",
"urn:dslforum-org:cwmp-1-0");
SOAPElement xsi = soapenvelope.addNamespaceDeclaration("xsi",
"/2001/xmlschema-instance");
SOAPElement xsd = soapenvelope.addNamespaceDeclaration("xsd",
"/2001/xmlschema");
SOAPElement enc = soapenvelope.addNamespaceDeclaration("soap-enc",
"/soap/encoding/");
//向soap头中添加数据
SOAPElement id = soapheader.addChildElement("id", "cwmp");//也就是说在header中新增⼦标签
//向标签中添加数据
id.setTextContent("1");
/
/返回SOAPPart对象
return soappart;
}
/**
* 封装请求体内容
* @param part
* @param list
* @return
* @throws Exception
*/
public Source getparametervalues(SOAPPart part, @SuppressWarnings("rawtypes") List list) throws Exception { //再次通过soappart对象创建envelope对象
SOAPEnvelope soapenvelope = Envelope();
//通过envelope对象获取body对象
SOAPBody soapbody = Body();
//通过body对象创建⼦节点  <cwmp:getparametervalues>
SOAPElement informres = soapbody.addChildElement("getparametervalues",
"cwmp");
@SuppressWarnings("static-access")
//实例化SOAP⼯⼚
SOAPFactory soapfactory = wInstance();
//通过soap⼯⼚创建标签  <parameternames soap-enc:arraytype="xsd:string[27]">
SOAPElement names = ateElement("parameternames", "", "");
/
/并且为这个标签新增属性 name 以及 valuejava xml是什么
names.addAttribute(new QName("soap-enc:arraytype"), "xsd:string["
+ list.size() + "]");
//⽅法传⼊的参数list,来⾃于另外⼀个⽅法,其实就是⼀个保存有value的集合,也就是⼦标签中需要存⼊的数据        //
SOAPElement nameelement = null;
for (int i = 0; i < list.size(); i++) {
//使⽤soap⼯⼚创建标签
nameelement = ateElement("string", "", "");
//将集合中的内容保存到创建的string标签中
nameelement.setTextContent((String) (i));
//再把存有⼦标签的数据存到外层标签中
names.addChildElement(nameelement);
}
//在把这个多重⼦标签,存⼊到外⾯的标签中
informres.addChildElement(names);
//最后返回这个最外层的part对象,其中就包含了header和body
Content();
}
/**
* 封装请求体中的数据
* @return
*/
public List<String> getTestNames() {
//创建⼀个List集合,然后调⽤add⽅法,存⼊数据
List<String> list = new ArrayList<String>();
list.add("internetgatewaydevice.deviceinfo.x_ct-com_cpu");
list.add("internetgatewaydevice.deviceinfo.x_ct-com_worktime");
list.add("internetgatewaydevice.deviceinfo.softwareversion");
list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.ssid");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_receivenoise");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytesreceived");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytessent");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytesreceived");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytessent");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketsreceived");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketssent");
list
.
add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketsreceived");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketssent");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_sponsepass");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.askpass");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.successpass");
list.add("internetgatewaydevice.deviceinfo.x_ct-com_temp");
list
.
add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-packetserror");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytesreceived");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytessent");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytesreceived");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-packetserror");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytessent");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_receiverate");
list
.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_sendrate");
list.add("internetgatewaydevice.deviceinfo.serialnumber");
list.add("internetgatewaydevice.deviceinfo.manufactureroui");
return list;
}
/**
* 将soap报⽂转换成string,在控制台输出
* @param source
* @throws Exception
*/
public void soap2string(Source source) throws Exception {
//此处的source就是封装的soap请求报⽂
if (source != null) {
//定义⼀个w3c包中的node对象
Node root = null;
//如果请求报⽂属于DOM类型
if (source instanceof DOMSource) {
//就获取node
root = ((DOMSource) source).getNode();
//如果请求报⽂是sax类型
} else if (source instanceof SAXSource) {
//最终还是获取其中的元素
InputSource insource = ((SAXSource) source).getInputSource();
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
dbf.setNamespaceAware(true);
Document doc = (Document) wDocumentBuilder().parse(insource);
root = (Node) DefaultRootElement();
}
/
/创建transfermerfactory⽰例,创建transformer对象
Transformer transformer = wInstance().newTransformer();
//设置属性为yes
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//调⽤⽅法,将node类型的请求报⽂在控制台进⾏输出
}
}
/**
* 封装响应体内容
* @param part
* @return
* @throws Exception
*/
public Source informresponse(SOAPPart part) throws Exception {
SOAPEnvelope soapenvelope = Envelope();
SOAPBody soapbody = Body();
SOAPElement informres = soapbody.addChildElement("informresponse",
"cwmp");
SOAPElement max = wInstance().createElement(
"maxenvelopes", "", "");
max.setTextContent("1");
informres.addChildElement(max);
Content();
}
}
执⾏结果
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="/soap/envelope/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:soap-enc="/soap/encoding/" xmlns:xsd="/2001/xmlschema <SOAP-ENV:Header>
<cwmp:id>1</cwmp:id>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<cwmp:getparametervalues>
<parameternames soap-enc:arraytype="xsd:string[27]">
<string>internetgatewaydevice.deviceinfo.x_ct-com_cpu</string>
<string>internetgatewaydevice.deviceinfo.x_ct-com_worktime</string>
<string>internetgatewaydevice.deviceinfo.softwareversion</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.ssid</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_receivenoise</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytesreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytessent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytesreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytessent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketsreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketssent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketsreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketssent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_sponsepass</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.askpass</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.successpass</string>
<string>internetgatewaydevice.deviceinfo.x_ct-com_temp</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-packetserror</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytesreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytessent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytesreceived</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-packetserror</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytessent</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_receiverate</string>
<string>internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_sendrate</string>
<string>internetgatewaydevice.deviceinfo.serialnumber</string>
<string>internetgatewaydevice.deviceinfo.manufactureroui</string>
</parameternames>
</cwmp:getparametervalues>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。