2

i am trying to convert a string to an xml document to read the verious nodes or to append a new node with jquery and there after convert it back to a string again. can anyone help me on the please. this is a snippet of my code

xmldoc.find('ROOT').append(
    '<USER><ENAME>'+ $(this).find('ENAME').text() +
     '</ENAME><OPERATOR>'+$(this).find('OPERATOR').text() +
     '</OPERATOR><PNR>'+$(this).find('PNR').text() +
     '<PNR></USER>'
);

if (window.ActiveXObject) {
    xmldocStr = xmldoc.xml;
}
// code for Mozilla, Firefox, Opera, etc.
else {
    xmldocStr = (new XMLSerializer()).serializeToString(xmldoc);
Anurag
  • 140,337
  • 36
  • 221
  • 257
fon
  • 255
  • 2
  • 7
  • 16

1 Answers1

1

Wrap it in a jQuery object.

var t = $('<foo><bar>something</bar></foo>');

//loop over 'bar' nodes
t.find('bar').each(function () {
    alert($(this).text());
});

And to convert it back to a string:

//then convert it back to a string
//for IE 
if (window.ActiveXObject) {
    var str = t.xml;
    alert(str);
 }
// code for Mozilla, Firefox, Opera, etc.
else {
   var str = (new XMLSerializer()).serializeToString(t);
   alert(str);
}
Aaron Butacov
  • 32,415
  • 8
  • 47
  • 61