简体   繁体   English

javascript getElementsByTagName排除某些标签

[英]javascript getElementsByTagName exclude certain tags

In my project I'm trying to parse rss & atom feeds. 在我的项目中,我试图解析rss和atom提要。 I target each feed's link with this: 我以此定位每个供稿的链接:

var feedLink = source.getElementsByTagName('link')[0];

Some feeds start directly with 一些提要直接以

<link>http://www.url.com</link>

but some start like this: 但有些开始是这样的:

<atom:link href="http://www.url.com/feed/" rel="self" type="application/rss+xml" />
<link>http://www.url.com</link>

and the selector I'm using targets also the link tags that starts with atom: Is there a way to avoid them ? 而我正在使用的选择器也以原子开头的链接标记为目标:有没有办法避免它们?

Thanks 谢谢

Try poping out the link from the end of NodeList , in this case, it will work for both cases. 尝试从NodeList的末尾弹出链接,在这种情况下,它对两种情况都适用。

var x = source.getElementsByTagName('link');
var feedLink = x[(x.length - 1)]

Isn't the best solution, but is the more fastly in this scenario. 这不是最好的解决方案,但是在这种情况下更快。

You can try using getElementsByTagNameNS instead, but i'm not sure how well it is supported across browsers. 您可以尝试使用getElementsByTagNameNS代替,但是我不确定跨浏览器支持的程度如何。 Works in latest Chrome. 在最新的Chrome中运行。

 var xmlString = [ '<?xml version="1.0" encoding="utf-8"?>', '<feed xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">', '<atom:link href="http://www.example.com/feed/" rel="self" type="application/rss+xml" />', '<link>http://www.example.com</link>', '</feed>' ].join('\\n'); var xml = (new DOMParser()).parseFromString(xmlString, "text/xml"); // Try regular search var found = xml.getElementsByTagName('link'); for (var i = 0; i < found.length; i++) { console.log('regular', i, found[i].nodeName); } // Try namespaced search var foundNS = xml.getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'link'); for (var i = 0; i < foundNS.length; i++) { console.log('namespaced', i, foundNS[i].nodeName); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM