How to save html while parsing xml using jQuery

Suppose I have the following XML structure:

<root>
 <item>
  <item1>some text is <b>here</b></item1>
  <item2>more text is here</item2>
 </item>
</root>

I can parse xml using something like:

$(responseXML).find('item').each(function(){
             var first_text = $(this).find('item1').text();
             var second_text = $(this).find('item2').text();
}

But html is not saved when using .text (). html () is not available for xml in jquery.

Any ideas on how to save internal html while parsing xml?

+3
source share
2 answers

insert sections that you do not want to interpret as xml in CDATA sections for example.

<root>
 <item>
  <item1><![CDATA[some text is <b>here</b>]]></item1>
  <item2><![CDATA[more text is here]]></item2>
 </item>
</root>

EDIT: Note that if using .text () does not work, try the following:

Quote from: http://dev.jquery.com/ticket/2425

. .text() , ( .text() .getCDATA():

jQuery.fn.getCDATA = function() {
if($.browser.msie)
    return this[0].childNodes[0].nodeValue;
    // Other browsers do this
return this[0].childNodes[1].nodeValue;
 };

, .

+4

Text() node. , :

var first_text = $(this).find('item1').html();
var second_text = $(this).find('item2').html();

HTML.

+3

Source: https://habr.com/ru/post/1709572/


All Articles