开放式XML的模式

时间:2010-09-01 16:59:41

标签: xml xsd

我正在实现一个当前处理JSON并需要处理XML的RESTful Web服务。我使用Jackson自动将Java对象与JSON进行编组。对于XML,我需要定义一个Schema,主要是因为客户需要它。幸运的是,我可以将Schema交给JAXB而不必自己编写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。我们可以通过遵循规则从数组的哈希映射数组生成XML,允许我们在XML和JSON之间转换这些自定义属性。

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

1 个答案:

答案 0 :(得分:2)

是的,只需使用any

<xs:element name="customProperty">
    <xs:complexType>
        <xs:sequence>
            <xs:any namespace="##any"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
相关问题