• Post Reply Bookmark Topic Watch Topic
  • New Topic

replacing '&' with "&" and " ' " with "&apos"  RSS feed

 
Lijoy John
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having in my servlets. As a standalone program i am able to replacing '&' with "&" and " ' " with "'" but as i do the same thing in servlets, i am getting a different output .
Actually what the servelt does is it parses a blank XML document and then populates it the HTML form values and using the Transformer class converting the XML file into a String and storing in the database. What i have to check that if there is an occurence of "'" or "&" then that to be converted to "'" or "&". I am having a filter class which should replace any such charaters . and then return the String value back to servlets.
---------------------------------------------------------
In HTML (ie in an HTTPServlet) the character '&' will be changed to '&'
---------------------------------------------------------
The following is the piece of code in (servlet file)
--------------------------------------------------
String QReplace = "'"; String QSearch= "'";
domainname=req.getParameter("domainname");
RPFilter filter = new RPFilter(); // name of the class
firstNameT = filter.newReplace(firstName, QSearch, QReplace);
//here above i am passing a String variable got from the form i.e firstName.
CASE1
If i am giving "John&Doe" the result i am getting is "John&ampDoe"
i.e John&(semicolon)ampDoe.// I have typed in semicolon as it not been shown on the browser.
CASE2
If i am giving "John'Doe" the result i am getting is "John&aposDoe".

I am giving another String variable "lastName" as "John&Doe"
and not filtering it, still it automatically converts "&" to "&"
The following is the code in RPFilter
-------------------------------------
public static String newReplace(String sOriginal, String sSearch, String sReplace){
int iPos = sOriginal.indexOf(sSearch);
if(iPos==-1){
return sOriginal;
}else{
StringBuffer sb = new StringBuffer();
sb.append(sOriginal.substring(0,iPos));
sb.append(sReplace);
sb.append(sOriginal.substring(iPos+1));
return sb.toString();
//return modified string
}
}

Question?:Is there any way to avoid this conversion of & to & automatically and then adding 'apos' to it?
Kindly let me know if there is a better way out to check the occurences of these charaters and how to convert it so that it does not give and XML error as well?
Thanks,
Regards,
Lijoy
 
Tim Holloway
Bartender
Posts: 18715
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Visit http://www.mousetech.com/EJBWizard.html and download the most recent (or any recent) copy of the EJBWizard source.
In the file XMLWriter.java you'll find a static method that converts the "magic 5" characters of XML for a supplied string.
Feel free to use or abuse the code in any way you like - it's all open-source.
 
Lijoy John
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,
Thanks for responding to my query. I had downloaded EJBWizard source production version 2.2.1 but couldnt locate XMLWriter.java in any of the directory.
Regards,
Lijoy
Originally posted by Tim Holloway:
Visit http://www.mousetech.com/EJBWizard.html and download the most recent (or any recent) copy of the EJBWizard source.
In the file XMLWriter.java you'll find a static method that converts the "magic 5" characters of XML for a supplied string.
Feel free to use or abuse the code in any way you like - it's all open-source.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!