为什么节点顺序在 XML 中很重要? [英] Why is node order important in XML?

查看:52
本文介绍了为什么节点顺序在 XML 中很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在处理一个 API,它要求 XML 文档的节点按特定顺序排列.我想知道为什么他们觉得有必要强制执行这一点,而我完全没有理由认为事情会变成这样.

I've been dealing with an API recently that requires nodes of the XML document to be in a particular order. I was wondering why they feel the need to enforce this when I can find absolutely no reason why things should be like this.

例如,这是正确的(xml大大简化)

For intance, this would be correct (xml greatly simplified)

<transaction>
    <address>1 main st</address>
    <amount>100</amount>
    <orderId>1234</orderId>
</transaction>

但这会返回错误

<transaction>
    <address>1 main st</address>
    <orderId>1234</orderId>
    <amount>100</amount>
</transaction>

起初我以为他们可以以列表/数组的形式存储东西,并且索引总是指向同一个节点.我理解为什么按相同顺序发送具有相同名称的兄弟节点很重要,正如 在这个问题中所解释的那样.但是,可以省略一些节点:

At first I thought it would be so that they can store things in a list/array form and have the indices always refer to the same node. I understand why sending sibling nodes that have the same name in the same order is important as explained in this question. However, some nodes can be left out:

<transaction>
    <amount>100</amount>
    <orderId>1234</orderId>
</transaction>

因此,在第三个示例中,amount 和 orderId 现在位于 [0] 和 [1],而不是第一个(正确)示例中的 [1] 和 [2].

So in the third example, amount and orderId would now be at [0] and [1] instead of at [1] and [2] in the first (correct) example.

另一种想法是,他们将 XML 作为字符串处理,并要求他们始终知道哪些节点紧随其后,但同样,由于允许省略节点,因此该理论没有意义.

Another thought would be that they process the XML as a string and require that they always know what nodes come after each other, but again since omitting nodes is allowed, that theory doesn't make sense.

谁能向我解释为什么我给节点的顺序很重要?还是我只是在处理一个陈旧且脾气暴躁的 API?

Can anyone explain to me why the order in which I give nodes matters? Or am I just dealing with an API that is old and grumpy?

推荐答案

节点顺序在 XML 中显然很重要,如下所示:

Node order obviously matters in XML like this:

<p>
   <span>This wouldn't make much sense</span>
   <span>if the order of these nodes were reversed.</span>
</p>

它在 XML 中不太明显,就像您提供的那样,它似乎是某种序列化格式.但是,如果属性设置顺序不正确,属性设置器有副作用的对象可能会失败.

It's less obvious in XML like what you provided, which appears to be some kind of serialization format. But objects whose property setters have side effects can fail if the properties aren't set in the right order.

想象一个具有私有 Person 字段的类,该字段公开 PersonIDName 属性.PersonID setter 创建 Person 的私有实例,Name setter 在私有 Name 属性上设置 Name代码>人员字段.在这种情况下,在设置 PersonID 之前设置 Name 失败,因为 Person 尚不存在.

Imagine a class with a private Person field that exposes PersonID and Name properties. The PersonID setter creates the private instance of Person, and the Name setter sets the Name property on the private Person field. In this case, setting Name before you set PersonID fails, because Person doesn't exist yet.

在这种情况下,实现要求 PersonID 出现在 XML 中的 Name 之前的架构可以防止此错误发生,代价是迫使其他开发人员这样做看似荒谬的事情.

This is a case where implementing a schema that requires PersonID to appear before Name in the XML keeps this error from happening, at the cost of forcing other developers to do things that are apparently nonsensical.

在这种情况下,显而易见的事情是找到编写该类的开发人员并击败他.这几乎是不可能的,尽管思考它曾经存在的世界很有趣.

Tthe obvious thing to do in situations like this is to find the developer who wrote that class and beat him. This is rarely possible, though it's interesting to contemplate a world in which it were.

这篇关于为什么节点顺序在 XML 中很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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