有xml
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Dispatch xmlns="/Core">
<c-gensym4 xsi:type="xsd:string">user</c-gensym4>
<c-gensym6 xsi:type="xsd:string">pass</c-gensym6>
<c-gensym8 xsi:type="xsd:string">TicketObject</c-gensym8>
<c-gensym10 xsi:type="xsd:string">TicketSearch</c-gensym10>
<c-gensym12 xsi:type="xsd:string">Result</c-gensym12>
<c-gensym14 xsi:type="xsd:string">ARRAY</c-gensym14>
<c-gensym16 xsi:type="xsd:string">StateType</c-gensym16>
<soapenc:Array soapenc:arrayType="xsd:string[1]" xsi:type="soapenc:Array">
<item xsi:type="xsd:string">new</item>
</soapenc:Array> <c-gensym19 xsi:type="xsd:string">UserID</c-gensym19>
<c-gensym21 xsi:type="xsd:int">5</c-gensym21>
<c-gensym23 xsi:type="xsd:string">Permission</c-gensym23>
<c-gensym25 xsi:type="xsd:string">rw</c-gensym25>
</Dispatch>
</soap:Body>
</soap:Envelope>[size=18px现在想按照xml给定的参数通过java向webservice提交申请并返回数据;
因为没遇到过Array类型。处理有些困难,望大虾帮帮忙,帮看看怎么处理?[/size]
java调用的代码如下:
package com.cops.otrs.rpc;
import java.io.StringWriter;
import java.net.URL;import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;/**
* 对象:TicketObject
*方法:TicketSearch
*
*参数:
*TPermission => 'rw',
*UserID => $userId,
*StateType => ['new',]
*Result => 'ARRAY',
*
*返回值:返回ticket id 列表。
*
*/
@SuppressWarnings("restriction")
public class NewTicket_id {
private String ticketid = "";
public String getTicketId(String userid) throws Exception {
// Create the connection
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();
// Create message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
// Object for message parts
SOAPPart sp = msg.getSOAPPart();
SOAPEnvelope env = sp.getEnvelope();
env.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
env.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
env.addNamespaceDeclaration("enc","http://schemas.xmlsoap.org/soap/encoding/");
env.addNamespaceDeclaration("env","http://schemas.xmlsoap.org/soap/envelop/");
env.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
SOAPBody body = env.getBody();
SOAPBodyElement dispatch = body.addBodyElement(new QName("/Core","Dispatch"));
dispatch.addChildElement("Username").addTextNode("user").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Password").addTextNode("pass").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Object").addTextNode("TicketObject").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Method").addTextNode("TicketSearch").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param1_Name").addTextNode("Result").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param1_Value").addTextNode("ARRAY").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement("Param2_Name").addChildElement("item","enc", "http://schemas.xmlsoap.org/soap/envelop/").addTextNode("StateType").setAttribute("env:arrayType","xsd:array");
//// dispatch.addChildElement("Param2_Value").addChildElement("item").addTextNode("new").setAttribute("env:arrayType","enc:string[1]");
// dispatch.addChildElement("Param2_Value").addChildElement("item","enc", "http://schemas.xmlsoap.org/soap/envelop/").addTextNode("new").setAttribute("env:arrayType","xsd:array");
//// dispatch.addChildElement("Param2_Value").addChildElement("item").addTextNode("['open']").setAttribute("env:arrayType","xsd:string[2]");;
Name name = env.createName("Array", "soapenc","http://schemas.xmlsoap.org/soap/encoding/");
dispatch.addChildElement(name).addChildElement("Param2_Name").addTextNode("StateType").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement(name).addChildElement("Param2_Value").addChildElement("item").addTextNode("new").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement(name).addChildElement("Param2_Value").addChildElement("item").addTextNode("open").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement("Param2_Name").addChildElement("itemType").addTextNode("StateType").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement("Param2_Value").addChildElement("item").addTextNode("new").setAttribute("xsi:type","xsd:string"); dispatch.addChildElement("Param2_Name").addChildElement("itemType").addTextNode("StateType").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param2_Value").addChildElement("item").addTextNode("new").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement("Param2_Value").addChildElement("itemType").addTextNode("open").setAttribute("xsi:type","xsd:string[]");;
dispatch.addChildElement("Param3_Name").addTextNode("UserID").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param3_Value").addTextNode(userid).setAttribute("xsi:type","xsd:int");
dispatch.addChildElement("Param4_Name").addTextNode("Permission").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param4_Value").addTextNode("rw").setAttribute("xsi:type","xsd:string");
URL url = new URL("http://otrs.china-ops.com/otrs/rpc.pl");
SOAPMessage resp = conn.call(msg, url);
Document doc = resp.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
XMLtoStr(doc);
Element el = doc.getDocumentElement();
NodeList nl = el.getChildNodes();
for (int i = 0; i < (nl.getLength() / 2); i++) {
// System.out.println(nl.item(i * 2).getTextContent().trim() + " : " + nl.item(i * 2 + 1).getTextContent().trim());
ticketid += nl.item(i*2).getTextContent().trim()+",";
ticketid += nl.item(i*2+1).getTextContent().trim()+",";
}
return ticketid;
}
public static void main(String[] args) {
NewTicket_id app = new NewTicket_id();
try {
System.out.println(app.getTicketId("7"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String XMLtoStr(Document document) {
String result = null;
if (document != null) {
StringWriter strWtr = new StringWriter();
StreamResult strResult = new StreamResult(strWtr);
TransformerFactory tfac = TransformerFactory.newInstance();
try {
Transformer t = tfac.newTransformer();
t.setOutputProperty(OutputKeys.ENCODING, "gb2312");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
t.transform(new DOMSource(document.getDocumentElement()),
strResult);
} catch (Exception e) {
System.err.println("XML.toString(Document): " + e);
}
result = strResult.getWriter().toString();
// System.out.println(result);
} return result;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Dispatch xmlns="/Core">
<c-gensym4 xsi:type="xsd:string">user</c-gensym4>
<c-gensym6 xsi:type="xsd:string">pass</c-gensym6>
<c-gensym8 xsi:type="xsd:string">TicketObject</c-gensym8>
<c-gensym10 xsi:type="xsd:string">TicketSearch</c-gensym10>
<c-gensym12 xsi:type="xsd:string">Result</c-gensym12>
<c-gensym14 xsi:type="xsd:string">ARRAY</c-gensym14>
<c-gensym16 xsi:type="xsd:string">StateType</c-gensym16>
<soapenc:Array soapenc:arrayType="xsd:string[1]" xsi:type="soapenc:Array">
<item xsi:type="xsd:string">new</item>
</soapenc:Array> <c-gensym19 xsi:type="xsd:string">UserID</c-gensym19>
<c-gensym21 xsi:type="xsd:int">5</c-gensym21>
<c-gensym23 xsi:type="xsd:string">Permission</c-gensym23>
<c-gensym25 xsi:type="xsd:string">rw</c-gensym25>
</Dispatch>
</soap:Body>
</soap:Envelope>[size=18px现在想按照xml给定的参数通过java向webservice提交申请并返回数据;
因为没遇到过Array类型。处理有些困难,望大虾帮帮忙,帮看看怎么处理?[/size]
java调用的代码如下:
package com.cops.otrs.rpc;
import java.io.StringWriter;
import java.net.URL;import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;/**
* 对象:TicketObject
*方法:TicketSearch
*
*参数:
*TPermission => 'rw',
*UserID => $userId,
*StateType => ['new',]
*Result => 'ARRAY',
*
*返回值:返回ticket id 列表。
*
*/
@SuppressWarnings("restriction")
public class NewTicket_id {
private String ticketid = "";
public String getTicketId(String userid) throws Exception {
// Create the connection
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();
// Create message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
// Object for message parts
SOAPPart sp = msg.getSOAPPart();
SOAPEnvelope env = sp.getEnvelope();
env.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
env.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
env.addNamespaceDeclaration("enc","http://schemas.xmlsoap.org/soap/encoding/");
env.addNamespaceDeclaration("env","http://schemas.xmlsoap.org/soap/envelop/");
env.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
SOAPBody body = env.getBody();
SOAPBodyElement dispatch = body.addBodyElement(new QName("/Core","Dispatch"));
dispatch.addChildElement("Username").addTextNode("user").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Password").addTextNode("pass").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Object").addTextNode("TicketObject").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Method").addTextNode("TicketSearch").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param1_Name").addTextNode("Result").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param1_Value").addTextNode("ARRAY").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement("Param2_Name").addChildElement("item","enc", "http://schemas.xmlsoap.org/soap/envelop/").addTextNode("StateType").setAttribute("env:arrayType","xsd:array");
//// dispatch.addChildElement("Param2_Value").addChildElement("item").addTextNode("new").setAttribute("env:arrayType","enc:string[1]");
// dispatch.addChildElement("Param2_Value").addChildElement("item","enc", "http://schemas.xmlsoap.org/soap/envelop/").addTextNode("new").setAttribute("env:arrayType","xsd:array");
//// dispatch.addChildElement("Param2_Value").addChildElement("item").addTextNode("['open']").setAttribute("env:arrayType","xsd:string[2]");;
Name name = env.createName("Array", "soapenc","http://schemas.xmlsoap.org/soap/encoding/");
dispatch.addChildElement(name).addChildElement("Param2_Name").addTextNode("StateType").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement(name).addChildElement("Param2_Value").addChildElement("item").addTextNode("new").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement(name).addChildElement("Param2_Value").addChildElement("item").addTextNode("open").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement("Param2_Name").addChildElement("itemType").addTextNode("StateType").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement("Param2_Value").addChildElement("item").addTextNode("new").setAttribute("xsi:type","xsd:string"); dispatch.addChildElement("Param2_Name").addChildElement("itemType").addTextNode("StateType").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param2_Value").addChildElement("item").addTextNode("new").setAttribute("xsi:type","xsd:string");
// dispatch.addChildElement("Param2_Value").addChildElement("itemType").addTextNode("open").setAttribute("xsi:type","xsd:string[]");;
dispatch.addChildElement("Param3_Name").addTextNode("UserID").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param3_Value").addTextNode(userid).setAttribute("xsi:type","xsd:int");
dispatch.addChildElement("Param4_Name").addTextNode("Permission").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param4_Value").addTextNode("rw").setAttribute("xsi:type","xsd:string");
URL url = new URL("http://otrs.china-ops.com/otrs/rpc.pl");
SOAPMessage resp = conn.call(msg, url);
Document doc = resp.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
XMLtoStr(doc);
Element el = doc.getDocumentElement();
NodeList nl = el.getChildNodes();
for (int i = 0; i < (nl.getLength() / 2); i++) {
// System.out.println(nl.item(i * 2).getTextContent().trim() + " : " + nl.item(i * 2 + 1).getTextContent().trim());
ticketid += nl.item(i*2).getTextContent().trim()+",";
ticketid += nl.item(i*2+1).getTextContent().trim()+",";
}
return ticketid;
}
public static void main(String[] args) {
NewTicket_id app = new NewTicket_id();
try {
System.out.println(app.getTicketId("7"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String XMLtoStr(Document document) {
String result = null;
if (document != null) {
StringWriter strWtr = new StringWriter();
StreamResult strResult = new StreamResult(strWtr);
TransformerFactory tfac = TransformerFactory.newInstance();
try {
Transformer t = tfac.newTransformer();
t.setOutputProperty(OutputKeys.ENCODING, "gb2312");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
t.transform(new DOMSource(document.getDocumentElement()),
strResult);
} catch (Exception e) {
System.err.println("XML.toString(Document): " + e);
}
result = strResult.getWriter().toString();
// System.out.println(result);
} return result;
}
}
解决方案 »
- GoogleGson转换复杂java对象,请求帮忙!急急急!!!!!!!!!!!
- 请教几个简单的正则表达式!!!!!!!!
- jsp动态包含一个子页面时为什么会找不到子页面中js的form表单名称呢?
- 推荐书
- jboss部署多个工程 报内存溢出
- 通过时间查询 高手请来!!!
- tomcat 如何调用应用服务器端的 EJB 组件方法?
- 求高人来指点一下?我实在没搞明白
- [转载&改编]一步一步开发Spring Framework MVC应用程序-1(Intellij IDEA版)
- 怎么在Struts的一个jsp处理两个actionForm?
- 为什么一次请求会进两次过滤器啊
- Excel导入数据库的问题 ?
上面代码返回是全部数据,array那个参数并没有传递进入。array中有参数['new','open'...],通过java调用perl完成的webservice项目,并得到相应的返回数据。如果哪个大侠明白,请多指点!
StateType => $['new','open'] dispatch.addChildElement("Param2_Name").addTextNode("StateType").setAttribute("xsi:type","xsd:string");
SOAPElement e = dispatch.addChildElement("Array", "enc");
e.setAttribute("enc:arrayType", "xsd:string[2]");
e.setAttribute("xsi:type", "enc:Array");
e.addChildElement("item").addTextNode("new").setAttribute("xsi:type", "xsd:string");
e.addChildElement("item").addTextNode("open").setAttribute("xsi:type", "xsd:string"); 后有又有问题,但已解决,分享下:
原程序调用方法:
Attachment => [
{
Content => $Content,
ContentType => $ContentType,
Filename => 'lala.txt',
},
{
Content => $Content,
ContentType => $ContentType,
Filename => 'lala1.txt',
},
],
解决方法:
Filename,ContentType,Content分别为传入的上传附件的参数;Content为附件转换成base64Binary种字符串;
当对象为多个时需要调整xsd:anyType[n]的数量;
dispatch.addChildElement("Param19_Name").addTextNode("Attachment").setAttribute("xsi:type","xsd:string");
SOAPElement e = dispatch.addChildElement("Array", "enc");
e.setAttribute("enc:arrayType", "xsd:anyType[1]");
e.setAttribute("xsi:type", "xsd:Array");
SOAPElement obj = e.addChildElement("item");
e.addChildElement(obj);
obj.addChildElement("Filename").addTextNode(Filename).setAttribute("xsi:type","xsd:string");
obj.addChildElement("ContentType").addTextNode(ContentType).setAttribute("xsi:type","xsd:string");
obj.addChildElement("Content").addTextNode(Content).setAttribute("xsi:type","xsd:base64Binary");
e.addChildElement(obj);
文件读取可试用
http://tomhat.javaeye.com/admin/blogs/844707