JSON到XML的转换 [英] JSON to XML conversion

查看:161
本文介绍了JSON到XML的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将JSON转换为XML并转换回XML的最佳方法是什么.例如,下面的JSON

What is the best way to convert JSON to XML and back. For example, the below JSON

{
    "user": "gerry",
    "likes": [1, 2, 4],
    "followers": [
        {
            "name": "megan"
        },
        {
            "name": "pupkin"
        }
    ]
}

可以像这样(#1)转换成XML:

could be converted into XML like this (#1):

<?xml version="1.0" encoding="UTF-8" ?>
<user>gerry</user>
<likes>1</likes>
<likes>2</likes>
<likes>4</likes>
<followers>
    <name>megan</name>
</followers>
<followers>
    <name>pupkin</name>
</followers>

或这样(#2):

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <likes>
      <element>1</element>
      <element>2</element>
      <element>4</element>
   </likes>
   <followers>
      <element>
         <name>megan</name>
      </element>
      <element>
         <name>pupkin</name>
      </element>
   </followers>
   <user>gerry</user>
</root>

尤其是,区别在于转换数组.对象属性转换非常简单.我也确信还有其他方法可以将JSON转换为XML.

In particular, the difference arises converting arrays. Object property conversion is quite trivial. I am also sure that there are other ways to convert JSON to XML.

所以问题是:最好的方法是什么?有什么标准吗?

So the question is: What is the best way? Are there any standards?

另一个问题:是否有一种方法可以以某种数学形式来表示转换映射本身.例如,可以描述一个映射,这样当给定JSON对象和映射对象时,转换函数将确切知道要生成哪个XML.并反转它.

Another question: is there a way to express the conversion mapping itself in some mathematical form. Eg, is it possible to describe a mapping such that a conversion function when given the JSON object and the mapping object would know exactly which XML to produce. And reverse it, too.

XML_1 = convert(JSON, mapping_1)
XML_2 = convert(JSON, mapping_2)
JSON  = convert(XML_1, mapping_1)
JSON  = convert(XML_2, mapping_2)
JSON  = convert(XML_1, mapping_2) # Error!

推荐答案

您显然对数据序列化背后的理论感兴趣.我将尝试使用以下标题进行解释.

You're obviously interested in the theory behind data serialization. I'll try to explain using the following headings.

  • XML作为数据序列化格式的问题
  • 为什么喜欢其他格式
  • 这真的与信息和关系有关

我要介绍的是语义网的介绍及其格式各种不同格式的数据.

What I'm leading to is an introduction to the Semantic web and how it formats data in various different formats.

您已经发现了几种以XML构造数据的方法.这是因为XML从一开始就是文档标记. XML没有内置的方式来描述简单的数据结构,例如列表或哈希.

As you've discovered there a several ways to structure data in XML. This is because XML started life as a documentation markup. XML has no built in way to describe simple data structures like lists or hashes.

这是一个简单的例子:

<data>
  <user name="gerry"/>
</data>

可以将其反序列化为简单的哈希值:

This can be deserialized as a simple hash:

data.user.name = "gerry"

或不太明显地显示为哈希列表:

or less obviously as a list of hashes:

data.user[0].name = "gerry"

事实是另一个XML文档,可能指定了多个用户标签:

Fact is a different XML document could be specifying multiple user tags:

<data>
  <user name="gerry"/>
  <user name="tom"/>
</data>

抢救的XML模式

此问题的解决方案是设计一个单独的架构规范,该规范描述文档的格式:

XML schema to the rescue

The solution to this problem was to design a separate schema specification that describes how the document is formatted:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="data">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="user" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="name" use="optional"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

person标签被描述为一系列元素...因此,这使XML解析器可以将这些信息存储在列表构造中.

The person tag is described as being a sequence of elements... So this enables XML parsers to store this information in a list construct.

这是许多处理XML数据的Web服务框架所采用的方法.在WSDL/XML模式中描述了消息格式,并且自动生成了处理消息的编程代码.

This is the approach taken by many web service frameworks which process XML data. The message format is described in the WSDL/XML schema and the programming code that processes the message is generated automatically.

JSON YAML 是专门用于序列化数据的. 他们不需要架构文档即可明确解析数据.

Formats like JSON and YAML are specifically designed to serialize data. They don't require schema documents in order to parse data unambiguously.

但是...即使如此....JSON和YAML不能解决所有问题.乍一看,数据更加明显,但没有描述数据结构的标准....

but... Even so.... JSON and YAML don't solve all problems. While the data is more obvious at first glance there are no standards for describing data structures....

我之前曾批评过XML模式,但是这些模式对于确定一段数据是否在程序上可用(有效)非常有用.即使这样,XML Schema也不能告诉我一个数据与另一个数据之间的关系.

Earlier I vilified XML schemas, but these can be really useful to determining whether a piece of data is programmatically usable (valid) or not. Even so an XML Schema does not tell me the relationship between one piece of data and another.

语义网运动是一种尝试创建自我描述和协作式互联网的尝试.问题是(IMHO)相关标准很复杂,难以理解和应用.开始的地方是RDF:

The Semantic web movement is an attempt to create a self describing and collaborative internet. Problem is (IMHO) the associated standards are complex and difficult to understand and apply. The place to start is RDF:

它被设计为一种通用的信息交换格式,并且以与数据实际序列化方式无关的方式巧妙地工作.

It's designed as a generic information interchange format and cleverly works in manner that is independent of how data is actually serialized.

您的简单示例,表示为RDF XML:

Your simple example and expressed as RDF XML:

<?xml version="1.0"?>
<rdf:RDF xmlns:user="http://myspotontheweb.com/user/1.0/" xmlns:ex="http://myspotontheweb.com/example/user/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <rdf:Description rdf:about="http://myspotontheweb.com/example/user/1">
        <user:name>gerry</user:name>
        <user:likes>1</user:likes>
        <user:likes>2</user:likes>
        <user:likes>4</user:likes>
    </rdf:Description>
    <rdf:Description rdf:about="http://myspotontheweb.com/example/user/2">
        <user:name>tom</user:name>
        <user:likes>2</user:likes>
        <user:likes>4</user:likes>
        <user:likes>6</user:likes>
        <user:follows rdf:resource="http://myspotontheweb.com/example/user/1" />
    </rdf:Description>
    <rdf:Description rdf:about="http://myspotontheweb.com/example/user/3">
        <user:name>felix</user:name>
        <user:likes>3</user:likes>
        <user:likes>5</user:likes>
        <user:follows rdf:resource="http://myspotontheweb.com/example/user/1" />
    </rdf:Description>
</rdf:RDF>

每个数据项都有一个唯一的标识符和一组自定义的属性:

Each item of data has a unique identifier and a custom set of attributes:

  • 名称
  • 喜欢
  • 跟随:用于将一个RDF实体链接到另一个.

XML只是表达RDF的一种方式,我更喜欢更紧凑的 N3 RDF格式 :

XML is just one way to express RDF, I prefer the more compact N3 RDF format:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix user: <http://myspotontheweb.com/user/1.0/> .
@prefix ex: <http://myspotontheweb.com/example/user/> .

ex:1 user:name "gerry" .
ex:1 user:likes "1" .
ex:1 user:likes "2" .
ex:1 user:likes "4" .

ex:2 user:name "tom" .
ex:2 user:likes "2" .
ex:2 user:likes "4" .
ex:2 user:likes "6" .
ex:2 user:follows ex:1 .

ex:3 user:name "felix" .
ex:3 user:likes "3" .
ex:3 user:likes "5" .
ex:3 user:follows ex:1 .

再次注意顶部的自定义前缀声明和每条数据(在RDF术语中为"tuple")表示的清晰声明.我认为这表明它是关于信息而不是数据格式的!

Again note the custom prefix declaration at the top and the clear statement of what each piece of data ("tuple" in RDF parlance) represents. I think this demonstrates it's about information not data format!

为了完整起见,以 JSON-LD 格式显示的RDF信息:

And for completeness the RDF information presented in JSON-LD format:

{
  "@graph": [
    {
      "@id": "http://myspotontheweb.com/example/user/3",
      "http://myspotontheweb.com/user/1.0/follows": {
        "@id": "http://myspotontheweb.com/example/user/1"
      },
      "http://myspotontheweb.com/user/1.0/likes": [
        "3",
        "5"
      ],
      "http://myspotontheweb.com/user/1.0/name": "felix"
    },
    {
      "@id": "http://myspotontheweb.com/example/user/2",
      "http://myspotontheweb.com/user/1.0/follows": {
        "@id": "http://myspotontheweb.com/example/user/1"
      },
      "http://myspotontheweb.com/user/1.0/likes": [
        "2",
        "6",
        "4"
      ],
      "http://myspotontheweb.com/user/1.0/name": "tom"
    },
    {
      "@id": "http://myspotontheweb.com/example/user/1",
      "http://myspotontheweb.com/user/1.0/likes": [
        "2",
        "4",
        "1"
      ],
      "http://myspotontheweb.com/user/1.0/name": "gerry"
    }
  ]
}

注意:

  • 有多种方法可以将RDF表示为JSON.请参见 JSON + RDF

一旦信息以RDF表示,它与其他数据实体的关系就可以直观地绘制成图形:

Once the information is expressed as RDF its relationships to other data entities can be graphed visually:

语义网走得更远,它仅以RDF开始.有类似XML模式的标准,用于发布花絮之间的良好理解的关系.使用这些可以开始以非常有趣的方式处理RDF数据.

The Semantic web goes a lot further, it only starts with RDF. There are XML schema-like standards for publishing well understood relationships between tuplies. Using these one can start to manipulate RDF data in very interesting ways.

我并不声称自己是数据处理方面的专家.我要承认的是,一些非常聪明的人一直在研究这个问题已有一段时间了.这些概念很难学习,但是对于更好地理解信息论来说是值得的.

I don't claim to be an expert in data processing. What I do acknowledge is that some very clever people have been looking at this problem for some time. The concepts are tough to learn, but worthwhile in order to better understand information theory.

这篇关于JSON到XML的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆