tag:blogger.com,1999:blog-7041745156199549192.post3509135245420641902..comments2024-02-18T21:35:27.335-08:00Comments on Filip's Technical Blog: Accessing CDATA section in XML DOM from JavascriptFilip Czajahttp://www.blogger.com/profile/12289949072596625867noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-7041745156199549192.post-74869452342615552372013-12-10T11:43:33.561-08:002013-12-10T11:43:33.561-08:00Here is my solution to this problem:
var getConte...Here is my solution to this problem:<br /><br />var getContent = function(p_xmlNode){<br /> if(p_xmlNode.childNodes.length == 0){<br /> console.log('getContent() | No text found in "'+getXmlString(p_xmlNode)+'"');<br /> return;<br /> }<br /> var txt;<br /> for(var i=0; i<p_xmlNode.childNodes.length; i++){<br /> txt = p_xmlNode.childNodes[i].nodeValue.replace(/^\s+|\s+$/g,'');<br /> //console.log('['+i+'] = Node Name = '+p_xmlNode.childNodes[i].nodeName+' : Node Type = '+p_xmlNode.childNodes[i].nodeType+' : Is Empty = '+(txt == '')+' : '+txt);<br /> if(txt != ''){<br /> return p_xmlNode.childNodes[i].nodeValue;<br /> }<br /> }<br /> };Anonymoushttps://www.blogger.com/profile/14639025369354726434noreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-83203857966711737982012-09-19T19:34:08.196-07:002012-09-19T19:34:08.196-07:00Works in chrome...
It's really help me...
Thx...Works in chrome...<br />It's really help me...<br /><br />Thx bro.. :DGilanghttps://www.blogger.com/profile/02707640037038317307noreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-68679461763538530602009-07-09T10:27:07.854-07:002009-07-09T10:27:07.854-07:00This code works alson on IE and FF, indpendent of ...This code works alson on IE and FF, indpendent of \n<br /><br />function getNodeCDATA(node)<br />{<br /> if (node.hasChildNodes())<br /> { crsXMLDOM.getText(node); //firefox & IE<br /> var node2=node.firstChild;<br /> while ((node2) && (node2.nodeType!=4))<br /> node2=node2.nextSibling;<br /> if ((node2) && (node2.nodeType==4))<br /> return node2; //encontrou CDATA<br /> };<br />return null;<br />}Rodolfohttps://www.blogger.com/profile/17255131513718976090noreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-40364599103621123142009-01-30T04:18:00.000-08:002009-01-30T04:18:00.000-08:00The solution you use isn't exactly a clean and pro...The solution you use isn't exactly a clean and proper one. What if for some reason the CData block is suddenly at index 2? The best way would be to take advantage of xpath. Here's how I go about it:<BR/><BR/>xmlDocument.selectSingleNode("//thexpath-to-your-node/text()").nodeValue<BR/><BR/>"selectSingleNode" is only available in IE, though using a cross browser XMLDOM implementation library like Sarissa would fix that, as well as "nodeValue".<BR/><BR/>or since you are using Sarissa now, a cross browser implementation of innerText is available as well. Giving you: "element.innerText"<BR/><BR/>Cheers !Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-52464773333983593082008-01-08T05:06:00.000-08:002008-01-08T05:06:00.000-08:00I've tested this script on FF & Opera. It wasn't f...I've tested this script on FF & Opera. It wasn't for commercial use so I skipped IE. Sorry for thatFilip Czajahttps://www.blogger.com/profile/12289949072596625867noreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-9084710601285001332007-12-23T19:32:00.000-08:002007-12-23T19:32:00.000-08:00textContext is exactly what I've been looking for....textContext is exactly what I've been looking for.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-53541128924853046902007-12-10T10:41:00.000-08:002007-12-10T10:41:00.000-08:00The reason you see the text showing up in the seco...The reason you see the text showing up in the second child node is because your XML file as written with a new line after the CDATA markup puts a newline "\n" element into the DOM tree in Firefox as the first child node.<BR/><BR/>Start instead with a single line :<BR/><BR/>[CDATA[Paragraph 1...<BR/><BR/>and your first method will work. <BR/><BR/>Or even better, keep your first text format and use "textContent":<BR/><BR/>xmlDocument.getElementsByTagName('content')[0].textContent;Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-78241156762227335942007-09-11T08:08:00.000-07:002007-09-11T08:08:00.000-07:00This was pretty helpful. It works the way you tho...This was pretty helpful. It works the way you thought in IE but in FF I have to use .childNodes[1]. Thanks for the post though, got me looking in the right direction.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-60657418270063261182007-08-19T14:38:00.000-07:002007-08-19T14:38:00.000-07:00on which browser you've tested the script? It work...on which browser you've tested the script? It works like a charm in Firefox, but i get some problems with IE...Fabrizio Piccahttps://www.blogger.com/profile/17379764982030713706noreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-77643197101150953032007-07-11T12:12:00.000-07:002007-07-11T12:12:00.000-07:00I wrote a function to handle this issue:function g...I wrote a function to handle this issue:<BR/><BR/>function getCDATA(element){<BR/><BR/> var ie = (typeof window.ActiveXObject != 'undefined');<BR/> var returnText;<BR/><BR/> if(ie){<BR/><BR/> if(element.hasChildNodes){<BR/> returnText = element.childNodes[0].nodeValue;<BR/> }<BR/> }<BR/> else{<BR/> <BR/> if(element.hasChildNodes){<BR/> returnText = element.childNodes[1].nodeValue;<BR/> }<BR/><BR/><BR/> }<BR/><BR/> return returnText;<BR/><BR/>}Raoul Dukehttps://www.blogger.com/profile/04941322080915195626noreply@blogger.comtag:blogger.com,1999:blog-7041745156199549192.post-80509010300340759632007-07-11T06:28:00.000-07:002007-07-11T06:28:00.000-07:00Sadly in it works different in IE and FFSadly in it works different in IE and FFKernelPanichttps://www.blogger.com/profile/17387139052460883630noreply@blogger.com