1
  1. 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.
  2. There are responses, who have uppercase and lowercase letters. So if I match with String.replace() it ignores it.
  3. 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

Community
  • 1
  • 1
Kefirchiks
  • 280
  • 4
  • 13
  • 1
    What's the context here? Usually any messing around with the string representation of an XML document is a really bad idea... just parse the XML in an XML API and use the result. – Jon Skeet Sep 08 '15 at 08:22
  • Take a look here http://stackoverflow.com/a/5055036/529256 Demonstrates case insensative find/replace. – Craig Sep 08 '15 at 08:27
  • @Kefirchiks You should put that as an answer to your question, its illegible in the comments area. Also, I'm glad I could help, please upvote my response. Thanks. – Craig Sep 08 '15 at 09:01

2 Answers2

1

SOLVED! Did this /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;
Kefirchiks
  • 280
  • 4
  • 13
0

you can change integrationResponse to uppercase like this :

result = integrationResponse.toUppercase().replace(replacable.toUppercase(),finalTags);
result_ = result.toLowerCase() ;
Maroo
  • 21
  • 2