You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Bertrand Goupil <bg...@clever-age.com> on 2002/09/05 23:14:06 UTC

Re[2]: Character translations

Hello,
Maybe you can get your data with <![CDATA[<you'&data>]]>

You've got nothing to change!

cheers
DJ> Mark -

DJ> I wrote my own function to do that... perhaps there is a better way that someone can suggest, but this is what worked for me:

DJ> I wrote a class called StringHelper that does the conversion as follows:

DJ> ----------------------------------------------------
DJ> package abcdef.eai.utilities;
DJ> /**
DJ>  * This utility class includes some (hopefully) useful String manipulation methods
DJ>  * Creation date: (6/3/2002 1:03:36 PM)
DJ>  * @author: James DePaul
DJ>  */
DJ> public class StringHelper {
DJ> /**
DJ>  * This is a static method.  You can use it to search/replace values in an Input String, * i.e. String resultString = StringHandler.replaceString(inputString, "&", "&amp;");  If the inputString
DJ>  * contained a value "AT&T", the resultString would contain "AT&amp;T"
DJ>  * 
DJ>  * Creation date: (6/3/2002 1:06:13 PM)
DJ>  * @return java.lang.StringBuffer - after all occurences of searchString have been replaced with instance(s) of replaceString
DJ>  * @param inputString java.lang.String - the String to search
DJ>  * @param searchString java.lang.String - the pattern to find in the inputString
DJ>  * @param replaceString java.lang.String - the pattern to replace in the resulting return String
DJ>  */
DJ> public static StringBuffer replaceString(String inputString, String searchString, String replaceString) {

DJ>         int counter=0;
DJ>         StringBuffer sb = new StringBuffer();

DJ>         // Spin through the whole inputString string replacing...
DJ>         while (counter<inputString.length()) {
DJ>                 int x = inputString.indexOf(searchString,counter);
DJ>                 if (x > -1) {
DJ>                         sb.append(inputString.substring(counter,x));
DJ>                         sb.append(replaceString);
DJ>                         counter = x + searchString.length();
DJ>                 } else {
DJ>                         sb.append(inputString.substring(counter,inputString.length()));
DJ>                         counter = inputString.length() + 1;
DJ>                 }
DJ>         }

DJ>         return sb;

DJ> }
DJ> }

DJ> ----------------------------------------------------------------

DJ> You can then invoke this function in your code as follows:

DJ>         StringBuffer workStringB;

DJ>         // First properly encode XML Entity 
DJ>         workStringB = StringHelper.replaceString(inputXMLstring, "&", "&amp;");

DJ>         // Return 'clean' XML
DJ>         String cleanXML = workStringB.toString();

DJ> ----------------------------------------------------------------

DJ> Good luck - 
DJ> James




DJ> -----Original Message-----
DJ> From: mark.russel@pncbank.com [mailto:mark.russel@pncbank.com]
DJ> Sent: Wednesday, September 04, 2002 12:03 PM
DJ> To: xerces-j-user@xml.apache.org
DJ> Subject: Character translations


DJ> I have a servlet that takes input from users.  part of the input can be <>
DJ> &"'.  Is there a function I can run these input streams through to convert
DJ> them. (e.g.  "corner of fifth & second" should look like this after run
DJ> through the translator function "corner of fifth &amp; second")

DJ> thanks in advance.

DJ> Mark Russell
DJ> PNC
DJ> 412-768-9603


DJ> ---------------------------------------------------------------------
DJ> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
DJ> For additional commands, e-mail: xerces-j-user-help@xml.apache.org




DJ> ---------------------------------------------------------------------
DJ> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
DJ> For additional commands, e-mail: xerces-j-user-help@xml.apache.org



-- 
Bertrand GOUPIL    http://www.clever-age.com
Clever Age - conseil en architecture technique
Tél: +33 1 49 01 28 63


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org