9

I receive an XML formatted as follows.

<rec>
      <id> servicedescription </id>
      <name> Description Value </name>
      <type> textbox </type>
</rec>

But this won't work since my system doesn't accept the spaces present between the tags. I require something as follows

<rec>
      <id>servicedescription</id>
      <name>Description Value</name>
      <type>textbox</type>
</rec>

Please help me out in Javascript. Thanks a lot

PS : Extremely sorry if this question has been asked before. I searched quite a lot but didn't get the info.

3
  • how you process this XML in javascript? Commented Feb 11, 2014 at 10:25
  • I receive a DOM object which has the XML. I serialize it into String format, convert it into JSON and process it. Commented Feb 11, 2014 at 10:38
  • 1
    check this stackoverflow.com/questions/20346243/… Commented Feb 11, 2014 at 10:38

1 Answer 1

19

The following code should do the work (http://jsfiddle.net/b8FBn/):

var str = "<rec> <id> servicedescription </id> <name> Description Value </name> <type> textbox </type></rec>";
str = str.replace(/>\s*/g, '>');  // Replace "> " with ">"
str = str.replace(/\s*</g, '<');  // Replace "< " with "<"

alert(str);
Sign up to request clarification or add additional context in comments.

1 Comment

This won't remove the redundant white space inside xml tags (between the attributes), but it mostly does the job, especially in case you don't have much of them. +1 from me

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.