简体   繁体   中英

Remove unwanted whitespaces between tags in XML (Javascript)

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM