Remove special character (horizontal tab) from xml file

Six Stars SJ
Six Stars

Remove special character (horizontal tab) from xml file

I have used following tJava replaceAll code to replace horizontal tab (\t) with an empty string. Here is my code:

if (input_row.filename!=null) {

context.file_name = input_row.filename.replaceAll("\\n","");

char asciiChar = (char)9;
String replaceString = ""+asciiChar;
context.file_name= input_row.filename.replaceAll(replaceString, "");
context.file_name = input_row.filename.replaceAll("\t","");
context.file_name = input_row.filename.replaceAll("\\t","");}
else
{context.file_name = input_row.filename;}

 

But this code didn't replace horizontal tabs in my files. I still think Talend can replace special characters in xml files. I have attached my job's screenshots1.PNG

Sixteen Stars

Re: Remove special character (horizontal tab) from xml file

Your code is not doing what you think. All you need is below (assuming you also want to remove new lines as well as tabs)....

 

if (input_row.filename!=null) {
     context.file_name = input_row.filename.replaceAll("\n","");
     context.file_name =context.file_name.replaceAll("\t","");
}else{
     context.file_name = input_row.filename;
}

The problem with your code was that you were overwriting your context.file_name with the result of attempting to remove different values. For example, if you have a variable "myString" which holds "abcdef" and you use the following code....

String myString = "abcdef";
context.file_name = myString.replaceAll("a","");
context.file_name = myString.replaceAll("b","");
context.file_name = myString.replaceAll("c","");
context.file_name = myString.replaceAll("d","");

....the result would actually be "abcef" because you are only removing the last value. To remove ALL of the values you would need to do this...

String myString = "abcdef";
context.file_name = myString.replaceAll("a","");
context.file_name = context.file_name.replaceAll("b",""); 
context.file_name = context.file_name.replaceAll("c","");
context.file_name = context.file_name.replaceAll("d","");

 

 

 

Six Stars SJ
Six Stars

Re: Remove special character (horizontal tab) from xml file

Hi, thanks for your post! but I am still getting horizontal tabs (\t) in my files:

 \t</Head> \t<Form> \t\t<

 

I am not sure if it's actually reading my files as it's not replacing this character.

 

Sony

Sixteen Stars

Re: Remove special character (horizontal tab) from xml file

I've just looked at your job and think you are probably right. What exactly do you have in your tFixedFlow component? I doubt it is the content of the file that the SCP component has downloaded. You need to read that into the job after downloading it.