[英]How to ignore double quote in StringBuffer
I am using StringBuffer to dynamically append the string, using \\ for ignoring single double (") but the proper result is not coming. Please help我正在使用 StringBuffer 动态附加字符串,使用 \\ 忽略单双 (") 但没有得到正确的结果。请帮忙
Now现在
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<tv time=\"10\" name=\"Main Gate\" id=\"9410\" device=\"smartTV\">
<playlist name=\"Welcome\" id=\"56\">
</playlist>
</tv>"
Expected预期的
"<?xml version="1.0" encoding="UTF-8"?>
<tv time="10" name="Main Gate" id="9410" device="smartTV">
<playlist name="Welcome" id="56">
</playlist>
</tv>"
Below in java code下面在java代码中
StringBuffer stringBuffer = new StringBuffer("<?xml version=\"1.0\"
encoding=\"UTF-8\"?>");
stringBuffer.append("<tv time=\"10\" device-name=\""+
device.getName() +"\" "
+ "device-id=\""+device.getDeviceHardId()+"\"
device=\""+device.getDeviceType()+"\">");
Playlist playlist = device.getPlaylist();
if(playlist != null){
stringBuffer.append("<playlist name=\""+playlist.getName()+"\"
id=\""+playlist.getId() +"\">");
stringBuffer.append("</playlist>");
}
stringBuffer.append("</tv>");
您的代码按预期工作,转义字符串是在 xml 中获取实际引号的方法,唯一的问题是您使用什么编译器来解析字符串,Java 按预期采用转义字符,但如果您使用的是其他类型的编辑器它可以以不同的方式处理它
produce type should be @Produces({ MediaType.APPLICATION_XML })生产类型应该是@Produces({ MediaType.APPLICATION_XML })
convert StringBuffer into String...将 StringBuffer 转换为 String...
Now working fine for me.现在对我来说工作正常。
@GET
@Path("xml")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_XML })
public String GetXMl(){
StringBuffer sb = new StringBuffer("<tv name=\"sony\"></tv>");
String xml =sb.toString();
return xml;
}
This is not a good practice.这不是一个好的做法。 Instead of escaping the double quote everywhere why not use
replace()
method provided by String Object.为什么不使用 String Object 提供的
replace()
方法,而不是到处转义双引号。 Replace double quote with a unique identifier like '"'.用像“"”这样的唯一标识符替换双引号。 At the end replace '"' with a double quote.
最后用双引号替换“"”。 Below snippet will demonstrate
下面的片段将演示
StringBuffer str = new StringBuffer();
str.append("<?xml version="1.0" encoding="UTF-8"?><tv time="10" name=""+ device.getName() + "" id="" + device.getDeviceHardId() + "" device=""+ device.getDeviceType() + "">");
Playlist playlist2 = device.getPlaylist();
if (playlist2 != null) {
str.append("<playlist name="" + playlist2.getName() + "" id="" + playlist2.getId()+""></playlist></tv>");
}
System.out.println(str.toString().replace(""", "\"")); //Will print the desired XML
Still, if you need your implementation, below code should work.不过,如果你需要你的实现,下面的代码应该可以工作。
StringBuffer stringBuffer = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
stringBuffer.append("<tv time=\"10\" name=\"" + device.getName() + "\" " + "id=\"" + device.getDeviceHardId() + "\" device=\"" + device.getDeviceType() + "\">");
Playlist playlist1 = device.getPlaylist();
if (playlist1 != null) {
stringBuffer.append("<playlist name=\"" + playlist1.getName() + "\" id=\"" + playlist1.getId() + "\">");
stringBuffer.append("</playlist>");
}
stringBuffer.append("</tv>");
Edit : It's not a solution, it's a workaround.编辑:这不是解决方案,而是一种解决方法。 &qout is an example.
&qout 就是一个例子。 Can be any unique stringset
可以是任何唯一的字符串集
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.