前面说了一些Google API的介绍,可是在实际的开发其中,我们可能须要把Google RESTful API返回的JSON数据转换成XML数据输入到第三方系统,这在企业应用集成里面很的常见。
那么里面就有一个问题,怎样确保转换后的XML数据格式是规范的。这就须要XML Schema(XML)来进行校验。如今关键是,我们仅仅知道Google API的JSON的schema,可是Google RESTful并没有提供返回数据的XML的schema。那么XML的Schema将会是什么样子的呢?让我以Google
Tasks API为样例。
从以下的URL我们能够看到Google Tasks RESTFul的API JSON Schema的信息:https://www.googleapis.com/discovery/v1/apis/tasks/v1/rest
那么。怎样把Google Tasks的JSON Schema转换成XML的XSD Schema?XML的Schema将会是什么样子的呢?请參考以下转换实现。
<?xml version='1.0' encoding='UTF-8'?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Task">
<xs:sequence>
<xs:element name="completed" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="deleted" form="unqualified" type="xs:boolean" minOccurs="0"/>
<xs:element name="due" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="etag" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="hidden" form="unqualified" type="xs:boolean" minOccurs="0"/>
<xs:element name="id" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="kind" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="links" form="unqualified" type="Tasklinks" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="notes" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="parent" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="position" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="selfLink" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="status" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="title" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="updated" form="unqualified" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TaskList">
<xs:sequence>
<xs:element name="etag" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="id" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="kind" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="selfLink" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="title" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="updated" form="unqualified" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TaskLists">
<xs:sequence>
<xs:element name="etag" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="items" form="unqualified" type="TaskList" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="kind" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="nextPageToken" form="unqualified" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Taskitems">
<xs:sequence>
<xs:element name="description" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="link" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="type" form="unqualified" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Tasklinks">
<xs:sequence>
<xs:element name="items" form="unqualified" type="Taskitems" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Tasks">
<xs:sequence>
<xs:element name="etag" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="items" form="unqualified" type="Task" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="kind" form="unqualified" type="xs:string" minOccurs="0"/>
<xs:element name="nextPageToken" form="unqualified" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Task" type="Task"/>
<xs:element name="TaskList" type="TaskList"/>
<xs:element name="TaskLists" type="TaskLists"/>
<xs:element name="Tasks" type="Tasks"/>
</xs:schema>