2014-02-11 122 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