Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CData Not parsing #8

Open
Prarik opened this issue Oct 10, 2013 · 2 comments
Open

CData Not parsing #8

Prarik opened this issue Oct 10, 2013 · 2 comments

Comments

@Prarik
Copy link

Prarik commented Oct 10, 2013

First of all thanks for creating this great parser that helped me a lot.

Now i know that CData means you have to ignore while parsing, but in case i need to parse it then how can i do it?

I found one method in NSXMLParserDelegate and i implemented it as below. The problem is how to place the string parsed exactly where it belongs.

-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
// Build the text value
[self.textInProgress appendString:someString];
NSLog(@"-------CData String %@",someString);
}

@amarcadet
Copy link
Owner

Hi, sorry for the delay. I assume that you have found a solution by now.

Would you mind to share it or open a PR with it?

I am trying to clean my issues and give that repository more attention than during the past.

@Prarik
Copy link
Author

Prarik commented Mar 24, 2014

Actually i have implemented the above method nothing new.

But sure i want to share following method which i used to clean-up the NSDictionary and generate the better output of the XML.

  1. First parse the XML using XMLReader.
  2. Than pass that dictionary into following method and compare before and after clean-up dictionary.
  3. I have changed NSString *const kXMLReaderTextNodeKey = @"temp"; in XMLReader.
#define kXMLDefaultKey @"text"
/**
 *  This method is used to clean up dictionary which we get after XML parsing
 *
 *  @param XMLDictionary Dictionary on which we want to perform cleanup
 *
 *  @return Cleaned dictionary
 */
+ (NSMutableDictionary *)cleanUpXMLDictionary:(NSMutableDictionary *)XMLDictionary
{
    for (NSString *key in [XMLDictionary allKeys]) {
        // get the current object for this key
        id object = [XMLDictionary objectForKey:key];

        if ([object isKindOfClass:[NSDictionary class]]) {
            if ([[object allKeys] count] == 1 &&
                [[[object allKeys] objectAtIndex:0] isEqualToString:kXMLDefaultKey] &&
                ![[object objectForKey:kXMLDefaultKey] isKindOfClass:[NSDictionary class]]) {
                // this means the object has the key "text" and has no node
                // or array (for multiple values) attached to it.
                [XMLDictionary setObject:[object objectForKey:kXMLDefaultKey] forKey:key];
            }
            else {
                // go deeper
                [self cleanUpXMLDictionary:object];
            }
        }
        else if ([object isKindOfClass:[NSArray class]]) {
            // this is an array of dictionaries, iterate
            for (id inArrayObject in (NSArray *)object) {
                if ([inArrayObject isKindOfClass:[NSDictionary class]]) {
                    // if this is a dictionary, go deeper
                    [self cleanUpXMLDictionary:inArrayObject];
                }
            }
        }
    }

    return XMLDictionary;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants