Как сделать enum в xml - PullRequest
       14

Как сделать enum в xml

4 голосов
/ 09 сентября 2011

У меня есть несколько типов запросов - которые действительно являются перечислениями.

Но в моем коде эти типы запросов являются перечислением:

enum RequestType {
  RequestRegister,
  RequestUnregister,
  etc
};

Моя текущая попытка wsdl-файла приведена ниже. Но он использует строковый тип. На моем сервере мне нужно извлечь enum / int из XML. Поиск строк - плохой дизайн.

Так как мне сформировать мой wsdl-файл, чтобы типы запросов были enums?

<?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 или enum. Потому что я хочу, например, сделать это:

// извлечение элемента из xml - в enum или int RequestType rqtype = getRequestType ();

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

В приведенном выше случае RequestType является перечислением. Неэффективно извлечь строковое значение, а затем выполнить поиск соответствующего значения перечисления.

Кажется, что во всех примерах перечислений используются строки, что кажется странным.

1 Ответ

4 голосов
/ 09 сентября 2011

Вы можете создавать свои собственные типы:

<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>
...