在XML中存储基元

时间:2018-05-04 22:47:09

标签: json node.js xml primitive-types

我没有使用过多年的XML,大多只是使用JSON来处理序列化数据。 我正在创建一个本地XML到JSON转换器,以便与Node.js一起使用,这不是那么容易,但应该是可行的。

存储基元类型的标准是什么,我能想到的是:

<root>
  <a type="string">foobar</a>
  <b type="boolean">true</b>
  <c type="number">69</c>
</root>

有没有比这更好的方法?

1 个答案:

答案 0 :(得分:1)

虽然XML Schema通常用于在模式中声明文档的内容和词法类型,但它还将xsi:type属性设置为表达动态类型元素或属性的惯用语,以及字符串,数字,日期等的XML Schema数据类型

在您的示例中使用XML Schema数据类型和xsi:type如下所示:

<root
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <a xsi:type="xsd:string">foobar</a>
  <b xsi:type="xsd:boolean">true</b>
  <c xsi:type="xsd:unsignedInt">69</c>
</root>