Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unwanted whitespaces between tags in XML (Javascript)

Tags:

javascript

xml

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.

like image 917
StackAddict Avatar asked Feb 11 '14 10:02

StackAddict


People also ask

Can there be spaces in XML tags?

XML Naming Rules Element names cannot start with the letters xml (or XML, or Xml, etc) Element names can contain letters, digits, hyphens, underscores, and periods. Element names cannot contain spaces.

How do you whitespace in XML?

Put &#160; in string. xml file to indicate a single space in an android project.


1 Answers

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);
like image 58
Piotr Stapp Avatar answered Oct 06 '22 10:10

Piotr Stapp