2013-11-21 35 views
0

我很新,在SOAP web服務在Java中,我有以下問題。爲什麼Eclipse在我在String中插入XML Soap請求時發生錯誤?

我有創建SOAP信封REQUEST的方法,這一個:

String soapXml = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"><soapenv:Header/><soapenv:Body><tem:getConfigSettings><tem:login>name.surname</tem:login><tem:password>myPassword</tem:password><tem:ipAddress>192.120.30.40</tem:ipAddress><tem:clientVersion>1</tem:clientVersion><tem:lastUpdateTime>1</tem:lastUpdateTime></tem:getConfigSettings></soapenv:Body></soapenv:Envelope>"; 

正如你可以看到我把SOAP請求信封soapXml字符串內。

的問題是,當我把這個XML這個String對象的Eclipse標記內這條線是不正確的說,我下面的錯誤:

Multiple markers at this line 
    - Syntax error, insert ";" to complete Statement 
    - Line breakpoint:WebServiceHelper [line: 124] - authentication(String, String, String, 
    String) 

它是關於如何我已經插入內部的XML代碼中的錯誤串?或者是什麼?我能做些什麼來解決?

TNX

安德烈

回答

1

您要插入的字符串包含",終止初始字符串。

在您的SOAP字符串中每隔"作爲\"轉義。

WRONG: 
String soapXml = "<soapenv:Envelope xmlns:soapenv="http://sc ... to be continued  

CORRECT: 
String soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://sc ... to be continued 
+0

所以每次「字之前,我必須添加\轉義字符?轉義字符到底是什麼意思?它是對Java說的,下一個「字符不是字符串的結尾,但它是一個」INSIDE字符串? – AndreaNobili

+1

正確。T他轉義字符告訴java編譯器,下一個字符是一個特殊字符('\ t'是TAB,'\ n'是換行,'\ r'是回車,'\ uXXXX'是一個Unicode字符代碼XXXX)或將被視爲字符串的一部分,就像''',它通常會終止字符串。要在一個字符串中添加一個'\',你也需要轉義它。因爲你需要告訴java編譯器,你不需要'\'的特殊含義,你只需要這個字符。所以,字符串中的'\'在代碼中真的是'\\'。 – thst

0

你需要逃避串內"這樣\"

String soapXml = "<soapenv:Envelope xmlns:"+ 
"soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\""+ 
"xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body>"+ 
"<tem:getConfigSettings><tem:login>name.surname</tem:login>"+ 
"<tem:password>myPassword</tem:password><tem:ipAddress>192.120.30.40"+ 
"</tem:ipAddress><tem:clientVersion>1</tem:clientVersion>"+ 
"<tem:lastUpdateTime>1</tem:lastUpdateTime></tem:getConfigSettings>"+ 
"</soapenv:Body></soapenv:Envelope>"; 

更合適的例子是

String s = "hello"; 
String s2 = "\"hello\""; 
System.out.println(s);  => hello 
System.out.println(s2);  => "hello" 
相關問題