为什么Eclipse在我在String中插入XML Soap请求时发生错误?

问题描述:

我很新,在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

安德烈

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

在您的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

你需要逃避串内"这样\"

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"