开发者

convert a string to xml doc with jquery or the other way round

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 co开发者_如何学JAVAde

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);


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜