2014-02-11 146 views
1

我收到XML格式如下。删除XML(Javascript)中标记之间的不需要的空格

<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:非常抱歉,如果这个问题以前曾被问过。我搜索了很多,但没有得到信息。

+0

你如何处理这个XML在JavaScript? –

+0

我收到一个具有XML的DOM对象。我将它序列化为String格式,将其转换为JSON并进行处理。 – StackAddict

+1

检查此问题http://stackoverflow.com/questions/20346243/how-do-i-remove-unwanted-spaces-from-exported-xml-data-being-loaded-into-javascr?rq=1 – Naren

回答

7

下面的代码应该做的工作(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); 
+0

好而简单.. 谢谢... – StackAddict