NSXMLParser - modify the elements in iPhone
Asked Answered
C

1

1

I have an HTML string that I need to modify its elements.

I looked into NSXMLParser, but didn't see any method to modify the elements while reading them.

I don't like the solution of creating a NSMutableString and adding strings to it.

Is there a way to read HTML string and modify its element in an elegant way?

e.g.,

<div style="color:grey"></div>

will be

<div style="color:black"></div>

Unfortunately I saw that one cannot use NSXMLDocument in an iPhone app.

Courtly answered 29/12, 2012 at 19:13 Comment(0)
B
1

see the github project KissXML which is a 'clone of NSXML library and works the same. https://github.com/robbiehanson/KissXML


shameless self-advert: my fork of it works way better with html that's not real xml https://github.com/Daij-Djan/KissXML

Bronwen answered 29/12, 2012 at 22:20 Comment(8)
it is implemented ontop of libxml2 like NSXMLParser but it allocates a lot more objects and obviously the memory usage is MUCH higher because the whole tree is loaded... but for 'normal ebpages' thats not a problem on today's device :) and it IS convenient :DBronwen
If I would only parse the HTML and concat it to String would it consume less memory?Courtly
if you parse it with NSXMLParser and just concat to a NSMutableString buffer, it would use less memory but I would not go there right away because it will be much more work and might not be necessary. KissXML might be performant enough for the task. (likely IMO)Bronwen
Well - after spending a day or more with it - how do I parse the elements recursively? I may have more than one levels of div with font-size!Courtly
a SAX parser is by default not recursive... I used to built my own data model around it and change the delegate depending on where I am:Bronwen
e.g. on <bookstore> start, create a bookstore object and make it delegate, on <book> start make a book and name it delegate. ob book end go back to the bookstore as the delegate, on bookstore end go back to original delegateBronwen
Your fork doesn't work well with HTML. For instance, If the tags are not closed, like <img> it wouldn't parse. See my question here #14111654 and the comments about chatting with you about it. What do you suggest?Courtly
I said .. better :D of course there are no guarantees. make sure you pass options:NSXMLDocumentTidyHTMLBronwen

© 2022 - 2024 — McMap. All rights reserved.