2010-09-01 62 views
1

我正在实现一个当前处理JSON并需要处理XML的RESTful Web服务。我使用Jackson来自动将Java对象与JSON进行编组。对于XML,我需要定义一个Schema,主要是因为客户需要它。幸运的是,我可以将Schema交给JAXB,而不必编码自己的XML编码。开放式XML的模式

一个功能是客户端可以在实体上设置“自定义属性”。这些被存储但不被服务器解释;他们是为了客户端程序的方便。实施例在JSON:

{"id":"abcde", 
"customProperties":{ 
    "foo":"bar", "rating":5, 
    "ridiculousExample":{"food":["green eggs","ham"], 
      "innerObject":{"name":"Bill","age":47}} 
    } 
} 

据推测在XML这将如下所示:

<whatever> 
    <id>abcde</id> 
    <customProperties> 
    <customProperty> 
     <foo>bar</foo> 
    </customProperty> 
    <customProperty> 
     <rating>5</rating> 
    </customProperty> 
    <customProperty> 
     <ridiculousExample> 
     <food> 
      <foodItem>green eggs</foodItem> 
      <foodItem>ham</foodItem> 
     <food> 
     <innerObject> 
      <name>Bill</name> 
      <age>47</age> 
     </innerObject> 
     </ridiculousExample> 
    </customProperty> 
    </customProperties> 
</whatever> 

在内部(在Java中)JSON数组只是一个阵列和JSON对象是HashMap中。我们可以通过遵循规则从数组hashmaps的数组中生成XML,从而允许我们在XML和JSON之间转换这些自定义属性。

XML Schema中是否有任何方法将某些事情指定到某个特定点并说“从这里开始,只要它是有效的XML就是开放式的”?

回答

2

是,只要使用any

<xs:element name="customProperty"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:any namespace="##any"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element>