Why is node order important in XML?

后端 未结 5 624
庸人自扰
庸人自扰 2021-01-05 09:48

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 ca

5条回答
  •  暖寄归人
    2021-01-05 10:02

    Node order obviously matters in XML like this:

    This wouldn't make much sense if the order of these nodes were reversed.

    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.

    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.

    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.

提交回复
热议问题