2014-06-05 187 views
0

我的parseFromString返回'undefined',既沒有'< parsererror ...'也沒有分析xml!parseFromString返回'undefined'

我找不到原因。你能幫忙嗎?

var parser = new DOMParser(), // https://developer.mozilla.org/en-US/docs/Web/API/DOMParser 
    _sourceDOC = null;  
alert('_sourceXML: "' + _sourceXML.innerHTML + '"'); // returns string with xml source :-) 
_sourceDOC = parser.parseFromString(_sourceXML.innerHTML,"text/xml"); 
alert('parseFromString: ' + _sourceDOC.xml); // returns 'undefined', neither '<parsererror…' nor parsed xml! 

第一個提醒:

PS:在另一方面,這種按預期工作。但我使用Firefox 17並且不想使用ActiveX。

var xmlDocIE = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0"); 
xmlDocIE.async = false; 
xmlDocIE.loadXML(_sourceXML.innerHTML); 

PPS:解決方法:

var parser = new DOMParser(), 
    _sourceDOC = null, 
    serializer = new XMLSerializer(), 
_sourceDOC = parser.parseFromString(_sourceXML.innerHTML,"text/xml"); 
str = serializer.serializeToString (_sourceDOC); 
alert('parseFromString: ' + str); // Result as expecded! :-) 

因此,我在劇本替換了錯誤的代碼(resultDoc.xml):

_destination.innerHTML = resultDoc.xml; 

通過正確的代碼,它工作正常:

_destination.replaceChild(resultDoc, _destination.childNodes[0]); 

謝謝,user1279647!

回答