Wednesday, May 15, 2013

Method to get rid off invalid XML characters:-



public String stripNonValidXMLCharacters(String in) {
        StringBuffer out = new StringBuffer(); // Used to hold the output.
        char current; // Used to reference the current character.

        if (in == null || ("".equals(in))) return ""; // vacancy test.
        for (int i = 0; i < in.length(); i++) {
            current = in.charAt(i); 
            if ((current == 0x9) ||
                (current == 0xA) ||
                (current == 0xD) ||
                ((current >= 0x20) && (current <= 0xD7FF)) ||
                ((current >= 0xE000) && (current <= 0xFFFD)) ||
                ((current >= 0x10000) && (current <= 0x10FFFF)))
                out.append(current);
        }
        return out.toString();
    }  

Thursday, April 18, 2013

I imported data from an external source into MySQL and found few strange characters.

How do we find out the records which have characters outside of the charset (ASCII in my case)?


Solution:-

MySQL provides a nice character set management which can be useful for such scenarios.

SELECT anything

FROM table_name
WHERE column_to_check <> CONVERT(column_to_check USING charset)

CONVERT(column_to_check USING charset) would convert the nonconvertible character into their respective replacements. Hence, the converted and unconverted text would be unequal.


For example,

SELECT eName
FROM employee
WHERE eName <> CONVERT(eName USING ASCII)

Wednesday, April 17, 2013

When you build an application using maven, the dependency automatically gets downloaded. However in Eclipse, they are not displayed under referenced libraries section. How do we resolve the issue?

There are two ways of doing it: 
  1. use plugin like m2eclipse 
  2. use mvn eclipse:eclipse - run the command and it shall create a .project and a .classpath file. Problem solved.
I usually prefer the second method.