2011-09-09 43 views
4

我有許多請求類型 - 這實際上是枚舉。如何在xml中執行枚舉

但在我的代碼,這些請求類型是一個枚舉:

enum RequestType { 
    RequestRegister, 
    RequestUnregister, 
    etc 
}; 

我在WSDL文件當前的嘗試是下面。但它使用一個字符串類型。在我的服務器中,我需要從xml中提取枚舉/ int。對字符串進行查找看起來像是糟糕的設計。

那麼如何形成我的wsdl文件,以便請求類型是枚舉?

<?xml version="1.0" encoding="UTF-8"?> 
<definitions name="CubaCTI" 
targetNamespace="http://www.iteloffice.com/wsdl/cubacti.wsdl" 
xmlns="http://schemas.xmlsoap.org/wsdl/" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:tns="http://www.iteloffice.com/wsdl/cubacti.wsdl" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

<message name="MonitorStartRequest"> 
<part name="user_name" type="xsd:string" /> 
<part name="password" type="xsd:string" /> 
<part name="dn" type="xsd:string"/> 
</message> 
<message name="MonitorStartResponse"> 
<part name="errorcode" type="xsd:short"/> 
<part name="errormessage" type="xsd:string"/> 
</message> 

<message name="MonitorStopRequest"> 
<part name="user_name" type="xsd:string" /> 
<part name="password" type="xsd:string" /> 
<part name="dn" type="xsd:string"/> 
</message> 
<message name="MonitorStopResponse"> 
<part name="errorcode" type="xsd:short"/> 
<part name="errormessage" type="xsd:string"/> 
</message> 

<message name="MakeCallRequest"> 
<part name="user_name" type="xsd:string" /> 
<part name="password" type="xsd:string" /> 
<part name="dn" type="xsd:string"/> 
<part name="destination" type="xsd:string"/> 
<part name="userdata" type="xsd:string"/> 
</message> 
<message name="MakeCallResponse"> 
<part name="errorcode" type="xsd:short"/> 
<part name="errormessage" type="xsd:string"/> 
</message> 

<message name="ClearConnectionRequest"> 
<part name="user_name" type="xsd:string" /> 
<part name="password" type="xsd:string" /> 
<part name="dn" type="xsd:string"/> 
<part name="destinationconnectionid" type="xsd:string"/> 
</message> 
<message name="ClearConnectionResponse"> 
<part name="errorcode" type="xsd:short"/> 
<part name="errormessage" type="xsd:string"/> 
</message> 

<portType name="CubaCTIRequests"> 
    <operation name="MonitorStart"> 
    <input message="tns:MonitorStartRequest"/> 
    <output message="tns:MonitorStartResponse"/> 
    </operation> 
    <operation name="MonitorStop"> 
    <input message="tns:MonitorStopRequest"/> 
    <output message="tns:MonitorStopResponse"/> 
    </operation> 
    <operation name="MakeCall"> 
    <input message="tns:MakeCallRequest"/> 
    <output message="tns:MakeCallResponse"/> 
    </operation> 
    <operation name="ClearConnection"> 
    <input message="tns:ClearConnectionRequest"/> 
    <output message="tns:ClearConnectionResponse"/> 
    </operation> 

</portType> 

<binding type="tns:CubaCTIRequests" name="cubactibinding"> 
<soap:binding style="document" 
     transport="http://schemas.xmlsoap.org/soap/http" /> 

<operation name="MonitorStart"> 
    <soap:operation soapAction="MonitorStart"/> 
    <input> 
     <soap:body use="literal"/> 
    </input> 
    <output> 
     <soap:body use="literal"/> 
    </output> 
</operation> 

<operation name="MonitorStop"> 
    <soap:operation soapAction="MonitorStop"/> 
    <input> 
     <soap:body 
      encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
      namespace="http://www.iteloffice.com/cubctirequests" 
      use="encoded"/> 
    </input> 
    <output> 
     <soap:body 
      encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
      namespace="http://www.iteloffice.com/cubctirequests" 
      use="literal"/> 
    </output> 
</operation> 

<operation name="MakeCall"> 
    <soap:operation soapAction="MakeCall"/> 
    <input> 
     <soap:body use="literal"/> 
    </input> 
    <output> 
     <soap:body use="literal"/> 
    </output> 
</operation> 

<operation name="ClearConnection"> 
    <soap:operation soapAction="ClearConnection"/> 
    <input> 
     <soap:body use="literal"/> 
    </input> 
    <output> 
     <soap:body use="literal"/> 
    </output> 
</operation> 


</binding> 


<service name="CubaCTI_Service"> 
    <documentation>WSDL File for Cuba CTI services</documentation> 
    <port binding="tns:cubactibinding" name="CubaCTIRequestsBinding"> 
    <soap:address 
     location="http://angusnotebook:8080"/> 
    </port> 
</service> 
</definitions> 

附加說明。

我'被迫'使用XML,因爲客戶端只能發送XML消息(無法控制這一點)。但我彌補了客戶端使用的xml。我控制/寫的服務器是用C++編寫的,我使用libxml來提取xml文件的'部分'。理想情況下,該項目將是一個int或枚舉。因爲我想要例如這樣做:

//從xml中提取項目 - 變爲枚舉或int RequestType rqtype = getRequestType();

switch(rqtype) { 
case RequestMakeCall: 
    //do whatever 

在上述情況下的RequestType是一個枚舉。提取字符串值並且然後必須查找相關的枚舉值是無效的。

我看到的枚舉的所有例子似乎都使用了字符串,這看起來很奇怪。

+0

我想代表字符串作爲枚舉是更自然。它使得XML和解析代碼更具可讀性。任何性能差異可能都無關緊要 - 庫和解析代碼已經必須與字符串一起工作,所以更多一點不會傷害。如果讀取值的速度對您很重要,請勿使用XML。 – svick

回答

3

您可以創建自己的類型:

<definitions targetNamespace="http://www.iteloffice.com/wsdl/cubacti.wsdl" 
      xmlns:tns="Mediaresearch.SimNet.Communication" 
      xmlns:s="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://schemas.xmlsoap.org/wsdl/"> 
    <types> 
    <s:schema elementFormDefault="qualified" 
       targetNamespace="http://www.iteloffice.com/wsdl/cubacti.wsdl"> 
     <s:simpleType name="OperatingSystemVersion"> 
     <s:restriction base="s:string"> 
      <s:enumeration value="None" /> 
      <s:enumeration value="WinXp" /> 
      <s:enumeration value="WinVista" /> 
      <s:enumeration value="Win7" /> 
     </s:restriction> 
     </s:simpleType> 
    </s:schema> 
    </types> 
    <message name="OperatingSystemVersion"> 
    <part name="user_name" type="tns:OperatingSystemVersion" /> 
    </message> 
</definitions> 
+0

可能我有點困惑與限制鹼是字符串。限制基地=「字符串」只是意味着枚舉的名稱是一個字符串?所有的名字都是字符串?在我的接收器代碼中,我使用libxml。我擔心的是libxml如何提取OperatingSystemVersion?我是否需要查看libxml如何處理枚舉值? –

+0

''s:resctrictions>'說的是這樣的:類型是一個字符串,但是有那些額外的限制。是的,很多人說這個名字是一個字符串。我不知道你需要在libxml中做什麼,我從來沒有使用它。 – svick

+0

按類型而不是元素屬性指定部件元素時要小心。這留下了未指定父節點的名稱空間(與正確限定的節點的子元素相反),從而將其名稱空間定義打開以解釋,這可能會導致互操作性問題。 – Eduardo