【问题标题】:javascript xml nodevalue not workingjavascript xml节点值不起作用
【发布时间】:2012-04-15 01:34:41
【问题描述】:

我正在尝试从名为 episode 的父节点中提取名为 lastupdated 的元素: 数据 -> 剧集 -> 最后更新

如果我写了:

episodeList = xmlDoc.getElementsByTagName("Episode");
console.log(episodeList[1].getElementsByTagName("lastupdated")[0])

它返回一个长度为 1 的列表的零项,但带有最后更新的标签

如果我写了:

console.log(episodeList[1].getElementsByTagName("lastupdated")[0].nodeValue)

它返回空值! 这是为什么呢?

回应: textValue 返回 null

xml部分: TextContent 也返回 null。这是xml:

<Data>
<Series>
<id>75760</id>
<Actors>
|Josh Radnor|Cobie Smulders|Neil Patrick Harris|Jason Segel|Alyson Hannigan|Bob Saget|
</Actors>
<Airs_DayOfWeek>Monday</Airs_DayOfWeek>
<Airs_Time>8:00 PM</Airs_Time>
<ContentRating>TV-PG</ContentRating>
<FirstAired>2005-09-19</FirstAired>
<Genre>|Comedy|</Genre>
<IMDB_ID>tt0460649</IMDB_ID>
<Language>en</Language>
<Network>CBS</Network>
<NetworkID/>
<Overview>
How I Met Your Mother is a comedy about Ted and how he fell in love. It all started when Ted's best friend, Marshall, dropped the bombshell that he was going to propose to his longtime girlfriend, Lily, a kindergarten teacher. At that moment, Ted realized that he had better get a move on if he, too, hopes to find true love. Helping him in his quest is his friend Barney, a confirmed bachelor with endless, sometimes outrageous opinions, a penchant for suits and a foolproof way to meet women. When Ted meets Robin, he's sure it's love at first sight, but destiny has something else in store.
</Overview>
<Rating>9.2</Rating>
<RatingCount>381</RatingCount>
<Runtime>30</Runtime>
<SeriesID>33700</SeriesID>
<SeriesName>How I Met Your Mother</SeriesName>
<Status>Continuing</Status>
<added/>
<addedBy/>
<banner>graphical/75760-g25.jpg</banner>
<fanart>fanart/original/75760-45.jpg</fanart>
<lastupdated>1333146863</lastupdated>
<poster>posters/75760-12.jpg</poster>
<zap2it_id>EP00753796</zap2it_id>
</Series>
<Episode>
<id>1159571</id>
<Combined_episodenumber>1</Combined_episodenumber>
<Combined_season>0</Combined_season>
<DVD_chapter/>
<DVD_discid/>
<DVD_episodenumber/>
<DVD_season/>
<Director/>
<EpImgFlag>1</EpImgFlag>
<EpisodeName>Robin Sparkles Music Video - Let's Go to the Mall</EpisodeName>
<EpisodeNumber>1</EpisodeNumber>
<FirstAired>2006-11-20</FirstAired>
<GuestStars/>
<IMDB_ID/>
<Language>en</Language>
<Overview/>
<ProductionCode/>
<Rating>4.0</Rating>
<RatingCount>1</RatingCount>
<SeasonNumber>0</SeasonNumber>
<Writer/>
<absolute_number/>
<airsafter_season/>
<airsbefore_episode>10</airsbefore_episode>
<airsbefore_season>2</airsbefore_season>
<filename>episodes/75760/1159571.jpg</filename>
<lastupdated>1275339740</lastupdated>
<seasonid>23219</seasonid>
<seriesid>75760</seriesid>
</Episode>

【问题讨论】:

    标签: javascript xml dom


    【解决方案1】:

    您需要使用nodeValue 而不是nodevalue。 Javascript 区分大小写,DOM Node 没有 nodevalue 属性。

    更新:如果你想在 xml 节点中使用文本值,你还需要使用 textContent。 有关更多信息,请查看 textContentnodeValue 参考资料。

    【讨论】:

    • @MarcoMartins 你能提供你正在处理的那部分XML吗?
    • @MarcoMartins 也许您需要使用textContent 而不是nodeValue
    • 刚刚用 xml 更新了第一篇文章,textContent 没有帮助
    • @MarcoMartins 这个xmlDoc.getElementsByTagName("Episode")[0].getElementsByTagName("lastupdated")[0].textContent 为我工作。
    • 试试这样,看看它是否适合你: var episodeList = new Array(); episodeList = xmlDoc.getElementsByTagName("Episode"); var episodeUpdatedTime = new Array(); for(var i = 0; i
    猜你喜欢
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2018-08-13
    • 2014-02-02
    • 1970-01-01
    相关资源
    最近更新 更多