HASH的解析

原始方法:
$GroupObject->GroupMemberAdd(
GID => 12,
UID => 6,
Permission => {
ro => 1,
move_into => 1,
create => 1,
owner => 1,
priority => 0,
rw => 0,
},
UserID => 123,
);
要解析的xml内容如下:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:env="http://schemas.xmlsoap.org/soap/envelop/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<Dispatch xmlns="/Core">
<Username xsi:type="xsd:string">user</Username>
<Password xsi:type="xsd:string">pass</Password>
<Object xsi:type="xsd:string">GroupObject</Object>
<Method xsi:type="xsd:string">GroupMemberAdd</Method>
<Param1_Name xsi:type="xsd:string">GID</Param1_Name>
<Param1_Value xsi:type="xsd:int">12</Param1_Value>
<Param2_Name xsi:type="xsd:string">UID</Param2_Name>
<Param2_Value xsi:type="xsd:int">11</Param2_Value>
<Param3_Name xsi:type="xsd:string">UserID</Param3_Name>
<Param3_Value xsi:type="xsd:int">11</Param3_Value>
[b][color=blue]<Param4_Name xsi:type="xsd:string">Permission</Param4_Name>
<Param4_Name>Permission<ro>1</ro>
<move_into>1</move_into>
<create>1</create><owner>1</owner>
<priority>1</priority>
<rw>1</rw>
</Param4_Name>[/color][/b]</Dispatch>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

java解析的内容如下:
public Map<String,String> userAddGroup(String gid,String uid,String userid,String i) throws Exception {
// Create the connection
SOAPConnection conn = OtrsRpcUtils.getSOAPConnection();
// Create message
SOAPMessage msg = OtrsRpcUtils.getSOAPMessage();
// Object for message parts
SOAPEnvelope env = OtrsRpcUtils.getSOAPEnvelope(msg);
//得到SOAPBodyElement
SOAPBodyElement dispatch = OtrsRpcUtils.getSOAPBodyElement(env);
OtrsRpcUtils.setPara(dispatch, "GroupObject","GroupMemberAdd");
// 传递变量
dispatch.addChildElement("Param1_Name").addTextNode("GID").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param1_Value").addTextNode(gid).setAttribute("xsi:type","xsd:int");
dispatch.addChildElement("Param2_Name").addTextNode("UID").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param2_Value").addTextNode(uid).setAttribute("xsi:type","xsd:int");
dispatch.addChildElement("Param3_Name").addTextNode("UserID").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Param3_Value").addTextNode(userid).setAttribute("xsi:type","xsd:int");
[color=blue][b] dispatch.addChildElement("Param4_Name").addTextNode("Permission").setAttribute("xsi:type","xsd:string");
SOAPElement e = dispatch.addChildElement("Param4_Name").addTextNode("Permission");
e.addChildElement("ro").addTextNode(i);
e.addChildElement("move_into").addTextNode(i);
e.addChildElement("create").addTextNode(i);
e.addChildElement("owner").addTextNode(i);
e.addChildElement("priority").addTextNode(i);
e.addChildElement("rw").addTextNode(i);[/b] [/color] URL url = OtrsRpcUtils.getUrl();
SOAPMessage resp = conn.call(msg, url);
msg.writeTo(System.out);
Document doc = OtrsRpcUtils.getDocument(resp);
RpcUtil.XMLtoStr(doc);
Map<String,String> m = new HashMap<String,String>();
return m;
}
public class RpcUtil {
// 输出返回数据的xml
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;
}
}
public class OtrsRpcUtils {
public static SOAPMessage getSOAPMessage()
{
MessageFactory mf = null;
SOAPMessage msg =null;
try {
mf = MessageFactory.newInstance();
msg = mf.createMessage();
} catch (SOAPException e) {
e.printStackTrace();
}
return msg;
}
/***
* 得到SOAPEnvelope
* @param msg SOAPMessage
* @return SOAPEnvelope
*/
public static SOAPEnvelope getSOAPEnvelope(SOAPMessage msg)
{
SOAPPart sp = msg.getSOAPPart();
SOAPEnvelope env = null;
try {
env = sp.getEnvelope();
} catch (SOAPException e) {
e.printStackTrace();
}
return env;
}
/****
* 得到 Document
* @param resp SOAPMessage
* @return Document
*/
public static Document getDocument(SOAPMessage resp)
{
Document doc = null;
try{
doc = resp.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
}
catch(Exception e)
{
e.printStackTrace();
}

return doc;
}
/***
* 得到SOAPBodyElement
* @param env SOAPEnvelope
* @return SOAPBodyElement
*/
public static SOAPBodyElement getSOAPBodyElement(SOAPEnvelope env)
{
SOAPBodyElement dispatch = null;
try {
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();
dispatch = body.addBodyElement(new QName("/Core","Dispatch"));
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
}
return dispatch;
}
/***
* 添加节点
* @param dispatch
* @param object
*/
public static void setPara(SOAPBodyElement dispatch,String object,String method)
{
try {
dispatch.addChildElement("Username").addTextNode("user").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Password").addTextNode("pass").setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Object").addTextNode(object).setAttribute("xsi:type","xsd:string");
dispatch.addChildElement("Method").addTextNode(method).setAttribute("xsi:type","xsd:string");
} catch (DOMException e) {
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值