jQuery XML parsing and replace tags

jQuery XML parsing and replace tags

I needed to replace then node having field name with the xml node.

I tried with replaceWith which did not parse properly.

As suggested here I tried to create a workaround to replace xml tag


  1. $.fn.replaceWithXML = function replaceWithXML(ele) { var xml = this.html(); return $('<dummy />').append($(ele).append(xml)).html(); }      

It was not working with all the xml tags properly.

  1. xml = '<field><col>Col1</col></field>' xml = $.parseXML(xml);

Replacing the "field" with "column" yields unexpected output ''

Neither this works

  1. var xml_node = '<column></column>'; $(root).append(xml_node,{html: $(xml ).find('field').html()});


Nor this works

  1. $(root).append($(xml_node).append($(xml ).find('field').html()));

I created a jsfiddle  to show the output after parsing