1

我正在获取一个 xml 文件并希望从中获取数据。xml 的来源并不重要,但我必须得到的某个字段是:

tracks = xmlDoc.getElementsByTagName("track");
variable = tracks.item(i).childNodes.item(4).childNodes.item(0).nodeValue;

现在这就像一个魅力,除非节点中没有价值。所以如果结构是这样的:

<xml>
 <one>
  <two>nodeValue</two>
 </one>
 <one>
  <two></two>
 </one>
</xml>

小部件将在第二个“one”节点上崩溃,因为“two”节点中没有值。控制台说:

TypeError: tracks.item(i).childNodes.item(4).childNodes.item(0) has no properties

关于如何让小部件只将空视为空字符串(null、空或“”)而不是崩溃的任何想法?我在猜测datagetValue()text或其他内容。

使用

var track= xmlDoc.getElementsByTagName('track')[0];
var info= track.getElementsByTagName('artist')[0];
var value= info.firstChild? info.firstChild.data : '';

不起作用并返回“TypeError:track has no properties”。那是从第二行调用艺术家的地方。

4

3 回答 3

1

在访问其数据之前测试“两个”节点是否有一个子节点。

childNodes.item(i)(或 JavaScript 简单形式 childNodes[i])通常应该避免,它有点脆弱,因为空白文本节点位于预期的确切位置。

我会做类似的事情:

var tracks= xmlDoc.getElementsByTagName('track')[0];
var track= tracks.getElementsByTagName('one')[0];
var info= track.getElementsByTagName('two')[0];
var value= info.firstChild? info.firstChild.data : '';

(如果你事先不知道'one'和'two'的标记名,你总是可以使用'getElementsByTagName('*')'来获取所有元素,只要你不需要支持IE5,其中这不起作用。)

An alternative to the last line is to use a method to read all the text inside the node, including any of its child nodes. This doesn't matter if the node only ever contains at most one Text node, but can be useful if the tree can get denormalised or contain EntityReferences or nested elements. Historically one had to write a recurse method to get this information, but these days most browsers support the DOM Level 3 textContent property and/or IE's innerText extension:

var value= info.textContent!==undefined? info.textContent : info.innerText;
于 2009-03-23T00:31:02.593 回答
0

without a dtd that allows a one element to contain an empty two element, you will have to parse and fiddle the text of your xml to get a document out of it.

Empty elements are like null values in databases- put in something, a "Nothing" or "0" value, a non breaking space, anything at all- or don't include the two element.

Maybe it could be an attribute of one, instead of an element in its own right. Attributes can have empty strings for values. Better than phantom elements .

于 2009-03-23T01:30:30.623 回答
0

Yahoo! Widgets does not implement all basic javascript functions needed to be able to use browser-code in a widget.

instead of using:

tracks = xmlDoc.getElementsByTagName("track");
variable = tracks.item(i).childNodes.item(4).childNodes.item(0).nodeValue;

to get values it's better to use Xpath with a direct conversion to string. When a string is empty in Yahoo! Widgets it doesn't give any faults, but returns the 'empty'. innerText and textContent (the basic javascript way in browsers, used alongside things like getElementsByTagName) are not fully (or not at all) implemented in the Yahoo! Widgets Engine and make it run slower and quite awfully react to xmlNodes and childNodes. an easy way however to traverse an xml Document structure is using 'evaluate' to get everything you need (including lists of nodes) from the xml.

After finding this out, my solution was to make everything a lot easier and less sensitive to faults and errors. Also I chose to put the objects in an array to make working with them easier.

            var entries = xmlDoc.evaluate("lfm/recenttracks/track");
            var length = entries.length;
            for(var i = 0; i &lt; length; i++) {
                var entry = entries.item(i);
                var obj = {
                    artist: entry.evaluate("string(artist)"),
                    name: entry.evaluate("string(name)"),
                    url: entry.evaluate("string(url)"),
                    image: entry.evaluate("string(image[@size='medium'])")
                    };
                posts[i] = obj;
            }
于 2009-03-27T15:30:41.400 回答