本文介绍了XML DOM与Object中的Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近写了一些使用XML DOM对象并在Javascript中返回对象的代码。在与同事谈话后,我质疑这是否值得一试。



你们中有谁知道任何研究或文章讨论的利弊使用这两种方法?



示例
我的代码将使用一个这样的结构的XML DOM对象(代码因为NDA):

 < myObject id =123anotherAttr =hellocustomAttr =foo> 
< myChild name =child1foo =bar/>
< myChild name =child2foo =bar/>
< myChild name =child3foo =bar/>
< / myObject>

并返回:

  {
id:123,
anotherAttr:hello,
customAttr:foo,
children:[
{名称:child1,foo:bar},
{name:child2,foo:bar},
{name:child3,foo:bar}
]
}

三个主要原因我会这样做:


  1. 代码更易读。 (对象我可以使用点符号访问值)

  2. 我一直认为使用对象比使用DOM更快。

我的问题是:
你们中有谁知道任何研究或文章讨论使用这两种方法的利弊?我可以基于我的假设吗?

解决方案

这是我如何看待...



优点:



1)代码将为未来的程序员更加可读

  var name = xml.selectSingleNode(myObject / myChild)[4] .getAttribute(name); 
// vs
var name = myObject.children [4] .name

2)您获得智能感知(假设您正在使用支持智能感知的IDE的IDE)



3)您可以完全定义将会和将不可用的内容xml(它可能包含一堆垃圾,你不想永远保持)



4)如果你要修改值并进行操作,您将不得不承担一次通过xml的开销。



缺点



1)有是创建新对象的开销。取决于XML的大小,这可能是或可能不是很小,如果你要打击每个元素/属性,你解析出来。



2)奥卡姆的剃刀最简单的解决方案,只是使用您获得的格式的数据,而不是添加更多的逻辑来解析它)。



3)可维护性。这假设您控制返回的XML结构。如果你这样做,你必须保持XML的创建,并确保JSON被更新以匹配。 (如果写得很好,可以最小化)。



备注



如果我自己写一些东西,我会使用JSON,因为我不喜欢在javascript中处理XML,并将其解析为JSON,这样可以让我最大程度地减少我在代码中发生XML的位置。在任何一个方面都有一些好点,所以它真的归结于对你(或你的主管)必须做的表现与可读性的判断。


I recently wrote some code that took an XML DOM object and returned an object in Javascript. After talking with a co-worker I am questioning if this is a worth while approach.

Do any of you know of any research or articles that discuss the pros and cons of using either approach?

Example:My code would take a XML DOM object with a structure like this (code changed because of NDA):

<myObject id="123" anotherAttr="hello" customAttr="foo">
  <myChild name="child1" foo="bar"/>
  <myChild name="child2" foo="bar"/>
  <myChild name="child3" foo="bar"/>
</myObject>

and would return this:

{
  id: "123",
  anotherAttr: "hello",
  customAttr: "foo",
  children: [
    {name: "child1", foo: "bar"},
    {name: "child2", foo: "bar"},
    {name: "child3", foo: "bar"}
  ]
}

The three main reasons I would do this:

  1. The code is more readable. (with the object I can access the values with dot notation)
  2. I always thought that working with objects was faster than working with the DOM.

Again my question is:Do any of you know of any research or articles that discuss the pros and cons of using either approach? Am I way off base on my assumptions?

解决方案

Here is how I see it...

Pros:

1) The code will be more readable for future programmers

var name = xml.selectSingleNode("myObject/myChild")[4].getAttribute("name");
//vs
var name = myObject.children[4].name

2) You gain intellisense (assuming you are using an IDE that supports intellisense for javascript)

3) You are able to fully define what will and will not be available from the xml (it could contain a bunch of garbage you don't want to keep forever)

4) If you are going to have to modify the values and do manipulation, you will have to incur the overhead of going through the xml once already. Might as well only do it once.

Cons:

1) There is an overhead to creating a new object. This may or may not be small depending on the size of the XML and if you were going to hit every element/attribute you parse out anyway.

2) Occam's Razor (The simplest solution to just use the data in the format you get it, instead of adding more logic to parse it).

3) Maintainability. This assumes you control the XML structure returned. If you do, you'll have to maintain both the creation of the XML and make sure that the JSON is updated to match. (If it is well written, this could be minimized).

notes

If I were to write something on my own time, I'd use the JSON since I don't like to deal with XML in javascript and parsing it to JSON upfront would allow me to minimize where XML occurs in my code. There are some good points on either way, so it really comes down to a judgement call on performance vs readability you (or your supervisor) will have to make.

这篇关于XML DOM与Object中的Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 19:11
查看更多