前面说了一些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>
如何用Google APIs和Google的应用系统进行集成(5)----如何把Google Tasks的JSON Schema转换成XML的Schema(XSD)?
时间: 2024-10-12 22:37:18