admin管理员组文章数量:1427361
For some reason DOMParser is adding some additional #text elements for each newline \n
for this url
.rss
...as well as many other RSS I've tried. I checked cnn/bbc feeds, they don't have newlines and dom parser handling them nicely. So I have to add the following before parsing it
var xmlText = htmlText.replace(/\n[ ]*/g, "");
var xmlDoc = parser.parseFromString(xmlText, "text/xml");
Server is returning text/xml.
var channel = xmlDoc.documentElement.childNodes[0];
this returning \n
without my code above and channel
with correction.
For some reason DOMParser is adding some additional #text elements for each newline \n
for this url
http://rt./Root.rss
...as well as many other RSS I've tried. I checked cnn/bbc feeds, they don't have newlines and dom parser handling them nicely. So I have to add the following before parsing it
var xmlText = htmlText.replace(/\n[ ]*/g, "");
var xmlDoc = parser.parseFromString(xmlText, "text/xml");
Server is returning text/xml.
var channel = xmlDoc.documentElement.childNodes[0];
this returning \n
without my code above and channel
with correction.
3 Answers
Reset to default 4Yes, that's what XML parsers are supposed to do by default. Get used to walking through child nodes checking to see whether they're elements (nodeType===1
) or text nodes (3
).
From Firefox 3.5 you get the Element Traversal API, giving you properties like firstElementChild
and nextElementSibling
. This makes walking over the DOM whilst ignoring whitespace easier. Alternatively you could use XPath (doc.evaluate
) to find the elements you want.
If you want to remove whitespace nodes for good, it's a much better idea to do it on the parsed DOM than by using a regex hack:
function removeWhitespace(node) {
for (var i= node.childNodes.length; i-->0;) {
var child= node.childNodes[i];
if (child.nodeType===3 && child.data.match(/^\s*$/))
node.removeChild(child);
if (child.nodeType===1)
removeWhitespace(child);
}
}
For some reason DOMParser is adding some additional #text elements for each newline \n for this url
that is standard behaviour. only IE ignores whithespace between Element Nodes. (XML Whitespace Handling, Whitespace @ MSDN, Whitespace @ MDC)
What is your question? Do you wish to not use the workaround? I think the workaround is necessary as the parser is working as expected.
本文标签: javascriptFirefox DOMParser problemStack Overflow
版权声明:本文标题:javascript - Firefox DOMParser problem - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745484396a2660316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论