删除XML(Javascript)中标记之间不需要的空格



我收到一个格式如下的XML。

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

但这行不通,因为我的系统不接受标签之间存在的空格。我需要如下内容

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

请帮助我解决Javascript问题。多谢

PS :如果之前有人问过这个问题,非常抱歉。我搜索了很多,但没有得到信息。

以下代码应完成工作 (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);

最新更新