ios - Trying to decode HTML characters in objective C is taking forever and 100% cpu usage -


i using piece of code decode few html special characters in xml data

    +(nsstring *)getnsstringformhtmlstring:(nsstring *)html {         if(system_version_greater_than_or_equal_to(@"7.0"))         {             nsmutableattributedstring* attrdisplaystring = [[nsmutableattributedstring alloc] initwithdata:[html datausingencoding:nsunicodestringencoding]options:@{nsdocumenttypedocumentattribute: nshtmltextdocumenttype} documentattributes:nil error:nil];             return [attrdisplaystring string];         }         return html;  } 

this code works in handy if using few number of times.

but,

my use case is, need parse junk xml has lot of encoded characters.

like this

  <filter name="added_time">        <displayname><![cdata[added&#x20;time]]></displayname>        <value display="dec - 2013"><![cdata[added_time&#x3a;dec&#x20;-&#x20;2013]]></value>        <value display="feb - 2014"><![cdata[added_time&#x3a;feb&#x20;-&#x20;2014]]></value>        <value display="mar - 2014"><![cdata[added_time&#x3a;mar&#x20;-&#x20;2014]]></value>        <value display="apr - 2014"><![cdata[added_time&#x3a;apr&#x20;-&#x20;2014]]></value>        <value display="sep - 2014"><![cdata[added_time&#x3a;sep&#x20;-&#x20;2014]]></value>        <value display="nov - 2014"><![cdata[added_time&#x3a;nov&#x20;-&#x20;2014]]></value>     </filter> 

since above piece of code called repeatedly decode every key , values, 100% cpu usage , parsing going on forever. (anyhow gets completed in 2 or 3 minutes of time)

see

see image

apparently,

getnsstringformhtmlstring: proving costly operation.

help me out!!

i need solution similar task doest consume of time.

best thing can come try out alternative library.

this answer has custom solution made of 2 file , written in c should quick. i'd try running through decode step , try getnsstringfromhtmlstring: call.

+(nsstring *)getnsstringformhtmlstring:(nsstring *)html {     if(system_version_greater_than_or_equal_to(@"7.0"))     {         nsmutabledata* data = [[html datausingencoding:nsutf8stringencoding] mutablecopy];         char* output = malloc(data.length);         char* bytes = data.mutablebytes;         decode_html_entities_utf8(bytes, bytes);         nsstring* converted = [nsstring stringwithutf8string:output];          nsmutableattributedstring* attrdisplaystring = [[nsmutableattributedstring alloc] initwithdata:[converted datausingencoding:nsunicodestringencoding]options:@{nsdocumenttypedocumentattribute: nshtmltextdocumenttype} documentattributes:nil error:nil];         return [attrdisplaystring string];     }     return html;  } 

no idea if quicker or not have copy when converting mutable nsdata array.

if find it's still slow, can try replace nsdata code directly converting data in html string using - (bool)getbytes:(void *)buffer maxlength:(nsuinteger)maxbuffercount usedlength:(nsuinteger *)usedbuffercount encoding:(nsstringencoding)encoding options:(nsstringencodingconversionoptions)options range:(nsrange)range remainingrange:(nsrangepointer)leftover;


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

qt - How to embed QML toolbar and menubar into QMainWindow -