- I need to replace a XML tag in string. The problem is that some of responses don't give me XML tag
<?xml version="1.0" encoding="UTF-8"?>
in the beginning and some of them do. - There are responses, who have uppercase and lowercase letters. So if I match with String.replace() it ignores it.
- I need to add XLS tag to that response. (currently working)
Response 1. Without XML tag in the beginning
<records>
<message>Something was found</message>
</records>
Response 2. With XML tag (from another service)
<?xml version="1.0" encoding="UTF-8"?>
<records>
<message>Nothing found</message>
</records>
Response 3. With XML tag (lowercase utf-8)
<?xml version="1.0" encoding="utf-8"?>
<records>
<message>Nothing found</message>
</records>
When I add XLS tags, the final product needs to look like this:
<?xml-stylesheet type="text/xsl" href="C:\Template.xsl"?>
<?xml version="1.0" encoding="UTF-8"?>
<records>
<message>Nothing found</message>
</records>
At the moment I was using String.replace(), and matched
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);
- the problem with 'replacable' is that some of responses give me uppercase and some lowercase letters. Using
.toUppercase() || .toLowerCase()
is not an option, because it affects all xml response.
Is there a way, that I can create following expression?
If it starts with OR contains "<?xml"
tag in the beginning, then only add XLS before it and if !contains, then add XLS schema + XML tag?
SOLVED! comment here