DOMParser.parseFromString(text,“text / html”)只解释第一个〜21,500字节。这是一个错误? [英] DOMParser.parseFromString(text,"text/html") only interprets the first ~21,500 Bytes. Is this a bug?

查看:2061
本文介绍了DOMParser.parseFromString(text,“text / html”)只解释第一个〜21,500字节。这是一个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面的代码只返回199个带有aXML.getElementsByTagName(node)的节点, ,而在解析的文本中有300个,这是不正确的XML形式。

  var atext =''; 
for(var i = 0; i <300; i ++){
atext + ='< node id ='+ i +'lat =42.5168939lon =1.553855version = 2changeset =21730124/>'+\\\
\r;
}
parser = new DOMParser();
aXML = parser.parseFromString(atext,text / html);
console.log(nodes:+ aXML.getElementsByTagName(node)。length
+\\\
\r atext.length:+ atext.length);
console.log(aXML.getElementsByTagName('node'));

控制台显示:

 节点:199 

atext.length:25390
HTMLCollection [< node#0>,< node#1>,< node#2> <节点#3>,<节点#4>,<节点#5>,<节点#6>,<节点#7>,<节点#8> ,189 weitere ...]

这可能是一个错误吗?



Web控制台没有显示parseFromString的任何错误。
$ b $(最近我得到了其他错误的日志,我不能把任何与打开的标签的关系。

pre $ 一个承诺链未能处理拒绝
日期:星期六2015年3月14日22时01分10秒GMT + 0100
完整消息:null

是相关的?)

解决方案

问题是,您试图将XML解析为HTML。这两者在有效的语法方面完全不同。而不是使用:

  aXML = parser.parseFromString(atext,text / html); 

您需要改用:

  aXML = parser.parseFromString(atext,text / xml); 

另外,确保XML是有效的,否则不会被解析。在你的例子中,它不是,但我认为这只是一个测试用例。

I have Win 7, 64 Bit, Firefox 32.0.1, Noscript running.

The following code only returns 199 nodes with aXML.getElementsByTagName("node"), whereas there are 300 in the parsed text, which is not well formed xml.

var atext = '';
for (var i=0;i<300;i++) {
    atext += '  <node id="'+i+'" lat="42.5168939" lon="1.553855" version="2" changeset="21730124"/>'+"\n\r";
}
parser = new DOMParser();
aXML= parser.parseFromString(atext , "text/html");
console.log(" nodes: " + aXML.getElementsByTagName("node").length
        +"\n\r atext.length:" + atext.length);
console.log(aXML.getElementsByTagName('node'));

The console shows:

" nodes: 199

 atext.length:25390"
HTMLCollection [ <node#0>, <node#1>, <node#2>, <node#3>, <node#4>, <node#5>, <node#6>, <node#7>, <node#8>, <node#9>, 189 weitere… ]

Could this be a bug?

The web-console doesn't show any error from parseFromString.

(I get a log of other errors lately, that I can't put in any relation to the open tabs.

A promise chain failed to handle a rejection.
Date: Sat Mar 14 2015 22:01:10 GMT+0100
Full Message: null

Could that be related?)

解决方案

The problem is that you are trying to parse XML as HTML. The two are quite different in terms of valid syntax. Instead of using:

aXML= parser.parseFromString(atext , "text/html");

You need to instead use:

aXML= parser.parseFromString(atext , "text/xml");

Also, make sure the XML is valid or it will not parse. In your example, it is not, but I assume that is just a test case.

这篇关于DOMParser.parseFromString(text,“text / html”)只解释第一个〜21,500字节。这是一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆