0

I am trying to accomplish a quite simple task here: modified a XML string using jQuery and get the modified string back.

I think that before, I could use the .html() function to do this, but I think they got rid of the functionnality with parse XML document.

So, here is the code:

var tempXML = "<node1><node2>Content #1</node2></node1>";
var parsedXML = $.parseXML(tempXML);
$(parsedXML).find('node2').text('XXXXXX');
alert($(parsedXML).find('node2').text());  // That works, it will display XXXXXX

Now, as I would suspect, the tempXML var is not modified.

So, how do I get the XML code that should read : "XXXXXX"?

Calling $(parsedXML).text(); only return XXXXXX and .html() is not defined for the object...

1 Answer 1

1

That is a trick, but here it is:

var tempXML = "<node1><node2>Content #1</node2></node1>";
var parsedXML = $.parseXML(tempXML);
$(parsedXML).find('node2').text('XXXXXX');

alert($("<div>").append($(parsedXML).find('node2')).html());​

EDIT: Or, here is, I think, more reliable solution:

function XMLToString(oXML) {   
    if (window.ActiveXObject) {     
        return oXML.xml;   
    } else {     
        return (new XMLSerializer()).serializeToString(oXML);   
    } 
}
var tempXML = "<node1><node2>Content #1</node2></node1>";
var parsedXML = $.parseXML(tempXML);
$(parsedXML).find('node2').text('XXXXXX');
alert(XMLToString($(parsedXML).find('node2')[0]));

jsFiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Yes. But the only thing needed here is to get a string which contains updated XML. Browser will not even display XML tags, but why it should change them?
@RobW Found some better solution. This one must be more reliable than a trick with <div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.