<?xml version="1.0" encoding="UTF-8"?>
,有些回复
做。响应1.开头没有XML标记
<records>
<message>Something was found</message>
</records>
响应2.使用XML标记(来自其他服务)
<?xml version="1.0" encoding="UTF-8"?>
<records>
<message>Nothing found</message>
</records>
响应3.使用XML标记(小写utf-8)
<?xml version="1.0" encoding="utf-8"?>
<records>
<message>Nothing found</message>
</records>
当我添加XLS标签时,最终产品需要如下所示:
<?xml-stylesheet type="text/xsl" href="C:\Template.xsl"?>
<?xml version="1.0" encoding="UTF-8"?>
<records>
<message>Nothing found</message>
</records>
目前我正在使用 String.replace(),并匹配
String xlsSchema = "<?xml-stylesheet type=\"text/xsl\" href=\"" + template.getAbsolutePath() + "\"?>";
String replacable = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
String finalTags = replacable + xlsSchema;
result = integrationResponse.replace(replacable, finalTags);
.toUppercase() || .toLowerCase()
不是一个选项,因为它会影响所有xml响应。有没有办法,我可以创建以下表达式?
如果以OR开头包含 "<?xml"
标记,那么只在它之前添加XLS,如果!包含,那么添加XLS架构+ XML标记?< / p>
已解决! comment here
答案 0 :(得分:1)
<强>解决!强> 这是/ w regex。
String xlsSchema = "<?xml-stylesheet type=\"text/xsl\" href=\"" + template.getAbsolutePath() + "\"?>";
String newTag = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
String tag = "<?xml";
String strippedXML = "";
// Remove all XML tags from beginning
if (integrationResponse.toLowerCase().contains(tag)) {
strippedXML = integrationResponse.replaceAll("<\\?[^>]*\\?>", "");
result = newTag + xlsSchema + strippedXML;
}
else
result = newTag + xlsSchema + integrationResponse;
答案 1 :(得分:0)
您可以将integrationResponse更改为大写,如下所示:
result = integrationResponse.toUppercase().replace(replacable.toUppercase(),finalTags);
result_ = result.toLowerCase() ;