Getting CDATA from XML using SMXMLDocument in iOS - ios

I'm coding an iOS app and I need to parse some values from an XML file.
I'm parsing the XML using SMXMLDocument and everything works smooth but some tags are returning null value. I noted that those tags are CDATA and after some research I found some methods to workaround this thing. The problem is all those methods refer to NSXMLParser and I can't understand how to do this using SMXMLDocument.
The code in my app is very similar (you could say identical) to the code at this page.
Can you please help me?
Thank you

Apparently SMXMLDocument does not conform to all of the NSXMLParser delegate functions when it parses your document. You are going to have to manually modify SMXMLDocument.m and add the function:
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
}
Inside that function should look very similar to the foundcharacters function, you just need to take the NSData that you get and convert it into a string. Beware of encoding though, CData data is not always UTF8.

Related

Trying to understand NSLog for printing to the console in Xcode

I'm trying to understand NSLog and how to print to the console in Xcode. I understand that NSLog uses what are called "tokens" to setup the type of variable being referenced to print (I think that's right?). What I need to know is the difference in which tokens to use and what they mean?
For example, after declaring an NSArray like below, I'd want to print the drink names to the console. I could do that like:
NSArray *drinks = #[#"juice", #"water", #"coffee"];
for (NSString *drinkName in drinks) {
NSLog(#"%#", drinkName);
}
So...am I using the #"%#" token because it's an NSString?
I would use #"%i" for an integer, and a #"%f" for a float? What about doubles? If anyone could shed some easy-to-understand beginner's knowledge on NSLog, that would be great! =)
As I understand it, NSLog isn't an Objective C function but a C function built into the foundation of Cocoa. Therefore it conforms to basic C functions with variadic arguments. You can use %# for all objects including NSString. This will in turn call the objects description method and print the appropriate string. Most objects have a rather useful representation already there (e.g. NSArray objects return the descriptions of all their contents)

Converting HTML to NSAttributedString

I am working on an ePub reader and it is going pretty well so far. But I need some performance running. Currently the used API to convert HTML strings to NSAttributedString objects is Apple's initialization method
- (instancetype)initWithData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error NS_AVAILABLE_IOS(7_0);
The only issue with this method is that, it can't be done in the background. Only on the UI/Main Thread and it takes so much running memory.
Is there some other solution that can tweak my app up a little to enhance the performance and memory utilization?
What you current code does, if by far the most efficient way.
Load html ONCE and parse it to NSAttributedString's once, save them and render inside a custom view.

How do I use dictionary literal syntax without subclassing from NSDictionary?

I don't want to subclass from NSDictionary but I want to get the literal syntax. It means that I am about to save and retrieve the key by using the "[]". I get this question from Parse iOS SDK, the PFObject is not inherited from the NSMutableDictionary. And they are able to use the dictionary literal syntax.
Here is the link from Parse iOS SDK: https://parse.com/docs/ios_guide#objects/iOS.
See for example the good Object Subscripting article at NSHipster. You just need to implement two methods in your class:
- (id)objectForKeyedSubscript:(id <NSCopying>)key;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;

Instance method xyz not found (return type defaults to id)?

I am facing issue for a method, which I have using for other NSString and working fine.
See this image.
Here html is NSString, I have used it in other project, like a clone of this, its working fine there, even I wrote like this, working fine in that project,
html = [html stringByConvertingHTMLToPlainText];
but here in this method , both the ways giving this warning.
What that mean and how can I solve it
?
If htmlis typed as an id, but you know that it's in fact an NSString, try casting it to NSStringand call your selector from there (id type to NSString).
That said, the reported error often occurs when your method is not declared in your header file (https://stackoverflow.com/a/13154244/865688). Did you forget to #import?
"stringByConvertingHTMLToPlainText" is not a standard method defined in the NSString class. It is s commonly used category defined in the MWFeedParser project.
Make sure you have imported the category using the right syntax. Refer: https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html Search for text "import the category header file" read around that area if not the whole chapter.

Ipad XML parsing doubts

my requirement is parse an xml file and display it in ipad.But i was totally confused how to parse this xml file,i am novice in xml parsing. kindly help me how to create array for it and how to display in ipad.Its a magazine news application ,any source code or sample applicatiom may help me.
There is a built in XML parsing class in iOS called NSXMLParser. As an init parameter it takes an NSData that is usually the data you get back from a web service. Assign yourself as its delegate and call [parser parse] and it will call your delegate when it encounters an element, when it finishes an element, etc. See documentation for the parser and its delegate protocol.
If you have control over the structure of your XML, I've found it easier when it's structured like this:
<Element attr1='value1' attr2='value2' />
instead of:
<Element>
<attr1>value1</attr1>
<attr2>value2</attr2>
</Element>
The reason is that the callback for when it finds an element is:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
where attributes is a dictionary of the values, so in the above example, [attributes objectForKey:#"attr1"] would give you "value1" if it was structured in the first way. If it's the second way, you end up doing lots of state management that's a bit of a pain, especially if you've got elements within elements, etc.
In addition to NSXMLParser thare are open-source parsers that either do everything themselves or wrap the built-in parser. I've written my own wrapper for it before. See here for a post on the subject (thanks to this question for the link).

Resources