How Do I Remove Unwanted Spaces From Exported XML Data Being Loaded Into Javascript
I've seen similar questions and answers to what i need, But my spaces are in a different place to the other questions i've seen. I am exporting data from the employee database into
Solution 1:
Why not just using replace
function ?
var str = "<your>xml </your>"
str = str.replace(/\s+/g, ' '); // Keep only one space character
str = str.replace(/>\s*/g, '>'); // Remove space after >
str = str.replace(/\s*</g, '<'); // Remove space before <
Here is jsFiddle.
Finally, what you want is trimming a string and replacing multiple spaces with only one space :
function trimMyNode(str){
str = str.replace(/\s+/g, ' '); // Keep only one space character
str = str.trim();
return str;
}
for (i = 0; i < 4; i++){
document.write("<table border='0'>");
document.write("<tr><td id=pic><img src=../images/employees/");
document.write(trimMyNode(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue));
document.write(".png width=100 height=100></td><td id=Yes>");
document.write(trimMyNode(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue));
document.write("</td></tr>");
document.write("</table>");
}
Here is another jsFiddle.
Solution 2:
If you are using jQuery to make the call, why not try something like this within your ajax call:
$(this).text($(this).text().replace(/\s+/g, ' ').trim());
The ajax call should look something like this:
$.ajax({
type: 'get',
url: 'data.xml',
dataType: 'xml',
success: function(response) {
$(response).find('employee').each(function() {
$(this).text($(this).text().replace(/\s+/g, ' ').trim());
}
// ... return final item
}
});
Post a Comment for "How Do I Remove Unwanted Spaces From Exported XML Data Being Loaded Into Javascript"