Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 86
Registered: ‎05-13-2009
My Device: Not Specified
Accepted Solution

How do i this string operation sounds simple but causing lot of time to do :)

Hi All,

 

    I have a very big string i.e. the soap xml response send by server which i read after connecting to the server using http now the problem is i have to create a Document out of it and parse it which is straight forward but the response i get from server requires many string replacements like say 1) replacing "&lt;" with "<"  2) replacing "&gt;" with ">" etc before creating a document out of it now there are many such replacements to be done in the response send by server which is taking a lots of time. Around 5 minutes on emulator. So my question is how do i do it? Is there one native methods to replaceAll the occurences of a substring in a string or so that i can use or is there some other good way? Plz let me know thx.

 

Thanks,

Sagar

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: How do i this string operation sounds simple but causing lot of time to do :)

Maybe your replacement operations aren't written as efficiently as they can be? You could also try using a regex package.
Developer
Posts: 16,679
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How do i this string operation sounds simple but causing lot of time to do :)

how about using ksoap2 or a similar framework?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 86
Registered: ‎05-13-2009
My Device: Not Specified

Re: How do i this string operation sounds simple but causing lot of time to do :)

Hi,

 

can u plz elaborate on the package u r mentioning and exactly what and how should do it thx .

 

Also simon ksoap2 would be a big change i guess as the application already has most of its code and parsing done using the Document in rim

 

Thanks,

Sagar

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: How do i this string operation sounds simple but causing lot of time to do :)

[ Edited ]

"Around 5 minutes on emulator".  Wow.  That is a long time.  How long is this document?

 

Are you processing this yourself to remove all the escaped characters before supplying it to the XML parser?

 

Have you done any profiling to confirm exactly where all the time is going?

 

Edit: Fix typo.

Message Edited by peter_strange on 10-05-2009 02:44 PM
New Developer
Posts: 86
Registered: ‎05-13-2009
My Device: Not Specified

Re: How do i this string operation sounds simple but causing lot of time to do :)

Hi Peter,

 

   I Have a long ouputString send by server approximately 6500 lines and this code i used to replace the substrings before providing it xml parser

 

public static String replaceAll(String orignalStr, String oldStr, String newStr) {
       
        int index = -1;
        while ((index = orignalStr.indexOf(oldStr)) >= 0){
           
          String tempStr = orignalStr.substring(0, index);
          tempStr += newStr;
          tempStr += orignalStr.substring(index + oldStr.length());
          orignalStr = tempStr;           
        }
        return orignalStr;
    }

 

I replace strings like

 

1) "&lt;" with "<"

2) "&gt;" with ">"

 

Can u plz help.

 

Thanks,

Sagar

Developer
Posts: 16,679
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How do i this string operation sounds simple but causing lot of time to do :)

the code can be submitted here:
http://thedailywtf.com/

take a look at StringUtilities if you really want to go this way.
personally i think a server should send data the client can understand, doing the processing on the mobile side is not a good idea.
ksoap makes stuff like this a lot easier, there are quite some tutorials. i would spend at least some hours to evaluate possible ways instead of creating a string replace mess.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 86
Registered: ‎05-13-2009
My Device: Not Specified

Re: How do i this string operation sounds simple but causing lot of time to do :)

Hi Simon,

 

 Thanks for the reply can u plz tell me if i have to do a ksoap change what all i would require to do and any links or threads would be helpful.

 

Thanks,

Sagar

Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: How do i this string operation sounds simple but causing lot of time to do :)

We took over a project being done by someone else, and they were doing exactly what you are doing.  Here is their source, which I think is more efficient than yours  Give it a try if you want.

 

But be aware that I have never used or tested this code.

 

I'm guessing you are pulling this String from the centre of your SOAP response.  It has been XML encoded.  So you can do what we did in this case, and package the String as a data item in an XML element, and then use the XML parser to get the real data, which we then feed into a XML parser again to get the real data.  XML is already configured to process these characters so it was more efficient than the replace approach.  Actually to be honest, that wasn't the reason we swapped, doing it as part of the XML parser meant we were confident that it was being done properly.

 

 

public String replaceAll(String source, String pattern, String replace) { if ((source != null) && (pattern.length() > 0)) { final int len = pattern.length(); StringBuffer sb = new StringBuffer(); int found = -1; int start = 0; while ((found = source.indexOf(pattern, start)) != -1) { sb.append(source.substring(start, found)); sb.append(replace); start = found + len; } sb.append(source.substring(start)); return sb.toString(); } else return ""; }

 

Developer
Posts: 16,679
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How do i this string operation sounds simple but causing lot of time to do :)