开发者

XML DOM vs Object in Javascript

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 l开发者_StackOverflow社区ike 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.


In my opinion using the JavaScript version is much better because the Xml version is not standard HTML. Although browsers are forgiving and will probably allow you to have an "xml data island" (MSXML3) I'd stick with JavaScript object.

Moreover, even though it is a "DOM" element, it is not quite parsed yet... you still have to use a DOM parser to detect and parse the XML before it is actually usable as per Mozila XML Data Island doc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜