0

I need help in finding and replacing a value in xml node using JQuery, please suggest, below is my scenario,

var metaData ='<Control Type="Table" ID="4900a2a9-47d7-4d3b-9a35-fdd32b185730"></Control>
        <Control Type="TextBox"  ID="7a499af6-16c1-4fe8-9ea0-fe02e7eef886"></Control>';

in the above xml structure I want to search for ID="4900a2a9-47d7-4d3b-9a35-fdd32b185730" and replace it with ID="91e3cbe6-8168-40be-bf26-ccdd6acb1e17" in JQuery. Please suggest.

Below is what I tried,

var oldID = "4900a2a9-47d7-4d3b-9a35-fdd32b185730";
var newID = "91e3cbe6-8168-40be-bf26-ccdd6acb1e17";
metaData.replace(oldID, newID);

the above code was not successful.

2 Answers 2

1

Since you have just string in xml variable, use JS .replace()

xml.replace('4900a2a9-47d7-4d3b-9a35-fdd32b185730', '91e3cbe6-8168-40be-bf26-ccdd6acb1e17');

var xml = '<Control Type="Table" ID="4900a2a9-47d7-4d3b-9a35-fdd32b185730"></Control><Control Type="TextBox"  ID="7a499af6-16c1-4fe8-9ea0-fe02e7eef886"></Control>';

var result = xml.replace('4900a2a9-47d7-4d3b-9a35-fdd32b185730', '91e3cbe6-8168-40be-bf26-ccdd6acb1e17');
$("#from").text(xml);
$("#result").text(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<b>Before:</b><br/>
<div id='from'></div><br/>
<b>After:</b><br/>
<div id='result'></div>

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

4 Comments

@Abhinay Well, it's working fine for me. Check updated answer
let me try again with the approach you suggested.
@Abhinay don't forget that .replace does not modify origin array
thank you so much, I was expecting the origin array will be modified, after your comment I tried now it is working below is the updated code for replace, metaData = metaData.replace(oldID, newID);
0

I wouldn't do a string replace.

var xml = '<Control Type="Table" ID="4900a2a9-47d7-4d3b-9a35-fdd32b185730"></Control><Control Type="TextBox"  ID="7a499af6-16c1-4fe8-9ea0-fe02e7eef886"></Control>';

var doc = $.parseXML('<root>' + xml + '</root>');
var $root = $(doc);
$root.find('[ID="4900a2a9-47d7-4d3b-9a35-fdd32b185730"]').attr('ID', 'x');
var newxml = xmlToString(doc);
$('#result').text(newxml.substring(6, newxml.length - 7))


//borrowed from http://stackoverflow.com/questions/6507293/convert-xml-to-string-with-jquery
function xmlToString(xmlData) {

    var xmlString;
    //IE
    if (window.ActiveXObject) {
        xmlString = xmlData.xml;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        xmlString = (new XMLSerializer()).serializeToString(xmlData);
    }
    return xmlString;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>

Comments

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.