Line breaks from XML file not in NSManagedObject

57 views
Skip to first unread message

Thomas Görlich

unread,
Jan 7, 2013, 9:28:41 AM1/7/13
to res...@googlegroups.com
Hi,

i get a XML file from a server which looks like this:

<mgns1:message>
    <mgns1:subject>Message Subject</mgns1:subject>
    <mgns1:body>Message Body with a line break and special characters.&#xD;&#xA;&#xD;&#xA;&#x9;&#x9;!"'§$%&amp;/()=?&lt;&gt;;:,.-_+*#@€</mgns1:body>
</mgns1:message>
 

After restkit's xml parser created the corresponding NSManagedObject the string looks like this:

Message Body with a line break and special characters.!"'§$%&/()=?<>;:,.-_+*#@€

As you can see the line breaks from the xml file (&#xD;&#xA;) are not in the NSString of the NSManagedObject. Is it possible to have these line breaks in the NSString as well or do i have to create my own escape sequences for line breaks and parse them in the objectLoader:didLoadObjects: function myself?

Blake Watters

unread,
Jan 7, 2013, 8:29:50 PM1/7/13
to res...@googlegroups.com
Yick. No idea. The XML parsing code is not well tested/supported right now so I can't really advise.


--
 
 

Thomas Görlich

unread,
Jan 9, 2013, 10:17:11 AM1/9/13
to res...@googlegroups.com
Thank you very much for your reply Blake! I did not know the the XML parser is not that well supported. I looked into the code and i guess i fixed the problem. At least i did not run into any problems yet.

I changed two functions in the Vendor/XMLReader/XMLReader.m File:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    // Build the text value
//[textInProgress appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; // original code line
    [textInProgress appendString:string];
}

The Problem here has been, that the variable string does not contain the whole content of the xml tag at once and therefore might delete Characters it shouldn't (e.g. in the middle of the content).

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    // Update the parent dict with text info
    NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];
    
    // Pop the current dict
    [dictionaryStack removeLastObject];
    
    NSMutableString *newString = [[NSMutableString alloc] initWithString:[textInProgress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    [textInProgress release];
    textInProgress = newString;
    
    // Set the text property
    if ([textInProgress length] > 0)
    {
        if ([dictInProgress count] > 0)
        {
            [dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];
        }
        else
        {
            // Given that there will only ever be a single value in this dictionary, let's replace the dictionary with a simple string.
            NSMutableDictionary *parentDict = [dictionaryStack lastObject];
            id parentObject = [parentDict objectForKey:elementName];
            
            // Parent is an Array
            if ([parentObject isKindOfClass:[NSArray class]])
            {
                [parentObject removeLastObject];
                [parentObject addObject:textInProgress];
            }
            
            // Parent is a Dictionary
            else
            {
                [parentDict removeObjectForKey:elementName];
                [parentDict setObject:textInProgress forKey:elementName];
            }
        }
        
        // Reset the text
        [textInProgress release];
        textInProgress = [[NSMutableString alloc] init];
    }
}
Instead i delete all whitespace and newline characters at the time the parser finished parsing the element.

I don't know if this is a good solution or not. Any comments would be appreciated.
Reply all
Reply to author
Forward
0 new messages