2011-02-25 9 views
11

में पेस्की नई लाइनें और व्हाइटस्पेस मैं ब्लॉगर (http://troybrant.net/blog/) द्वारा लिखी गई कक्षा का उपयोग कर रहा हूं जो एक एक्सएमएल स्ट्रिंग लेता है और एनएस डिक्शनरी को थूकता है।एक्सएमएल रीडर क्लास

यह सुंदर है ... पूरी तरह से काम करता है, सिवाय इसके कि मैं कई तत्व मानों की शुरुआत में न्यूलाइन और सफेद स्थान की अजीब विन्यास के साथ समाप्त होता हूं।

मैं यह समझने में सक्षम नहीं हूं कि क्यों। मैं यहां कक्षा पोस्ट कर रहा हूं इसलिए मुझे आंखों की दूसरी जोड़ी मिल सकती है ... देखें कि क्या कोई भी उस विधि में संदेह करता है जिस पर वह उपयोग कर रहा है।

// 
// XMLReader.m 
// 

#import "XMLReader.h" 

NSString *const kXMLReaderTextNodeKey = @"text"; 

@interface XMLReader (Internal) 

- (id)initWithError:(NSError **)error; 
- (NSDictionary *)objectWithData:(NSData *)data; 

@end 


@implementation XMLReader 

#pragma mark - 
#pragma mark Public methods 

+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error 
{ 
    XMLReader *reader = [[XMLReader alloc] initWithError:error]; 
    NSDictionary *rootDictionary = [reader objectWithData:data]; 
    [reader release]; 
    return rootDictionary; 
} 

+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error 
{ 
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; 
    return [XMLReader dictionaryForXMLData:data error:error]; 
} 

#pragma mark - 
#pragma mark Parsing 

- (id)initWithError:(NSError **)error 
{ 
    if ((self = [super init])) 
    { 
     errorPointer = error; 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    [dictionaryStack release]; 
    [textInProgress release]; 
    [super dealloc]; 
} 

- (NSDictionary *)objectWithData:(NSData *)data 
{ 
    // Clear out any old data 
    [dictionaryStack release]; 
    [textInProgress release]; 

    dictionaryStack = [[NSMutableArray alloc] init]; 
    textInProgress = [[NSMutableString alloc] init]; 

    // Initialize the stack with a fresh dictionary 
    [dictionaryStack addObject:[NSMutableDictionary dictionary]]; 

    // Parse the XML 
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; 
    parser.delegate = self; 
    BOOL success = [parser parse]; 

    // Return the stack's root dictionary on success 
    if (success) 
    { 
     NSDictionary *resultDict = [dictionaryStack objectAtIndex:0]; 
     return resultDict; 
    } 

    return nil; 
} 

#pragma mark - 
#pragma mark NSXMLParserDelegate methods 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    // Get the dictionary for the current level in the stack 
    NSMutableDictionary *parentDict = [dictionaryStack lastObject]; 

    // Create the child dictionary for the new element, and initilaize it with the attributes 
    NSMutableDictionary *childDict = [NSMutableDictionary dictionary]; 
    [childDict addEntriesFromDictionary:attributeDict]; 

    // If there's already an item for this key, it means we need to create an array 
    id existingValue = [parentDict objectForKey:elementName]; 
    if (existingValue) 
    { 
     NSMutableArray *array = nil; 
     if ([existingValue isKindOfClass:[NSMutableArray class]]) 
     { 
      // The array exists, so use it 
      array = (NSMutableArray *) existingValue; 
     } 
     else 
     { 
      // Create an array if it doesn't exist 
      array = [NSMutableArray array]; 
      [array addObject:existingValue]; 

      // Replace the child dictionary with an array of children dictionaries 
      [parentDict setObject:array forKey:elementName]; 
     } 

     // Add the new child dictionary to the array 
     [array addObject:childDict]; 
    } 
    else 
    { 
     // No existing value, so update the dictionary 
     [parentDict setObject:childDict forKey:elementName]; 
    } 

    // Update the stack 
    [dictionaryStack addObject:childDict]; 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    // Update the parent dict with text info 
    NSMutableDictionary *dictInProgress = [dictionaryStack lastObject]; 

    // Set the text property 
    if ([textInProgress length] > 0) 
    { 
     [dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey]; 

     // Reset the text 
     [textInProgress release]; 
     textInProgress = [[NSMutableString alloc] init]; 
    } 

    // Pop the current dict 
    [dictionaryStack removeLastObject]; 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    // Build the text value 
    [textInProgress appendString:string]; 
} 

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 
{ 
    // Set the error pointer to the parser's error object 
    *errorPointer = parseError; 
} 

@end 

इस एक कष्टप्रद सवाल है, तो मैं माफी माँगता हूँ (और मुझे नीचे द्वेष के कारण वोट नहीं करते कृपया)। मैं बस यह पता लगाने की कोशिश कर रहा हूं कि यहां क्या हो रहा है और इसे खोजने के लिए कोडिंग कौशल की कमी है। (मैं करने के लिए नया हूँ Obj सी)

क्लिफ

समाधान:

बदलें इस लाइन:

[dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey]; 

इस लाइन के लिए:

[dictInProgress setObject:[textInProgress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:kXMLReaderTextNodeKey]; 
+0

आप एक जीवन बचतकर्ता हैं –

उत्तर

10

आप उस वर्ग से प्राप्त मूल्यों को ट्रिम कर सकता है:

NSString *trimmedValue = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

जो अग्रणी और पिछली सफेद जगह को हटा देगा।

+0

मैं ऐसा कुछ करने की कोशिश कर रहा हूं, लेकिन निराशाजनक हिस्सा यह है कि मेरे पास इस तरह का मूल्य होगा: "\ n \ n \ n बहुत बढ़िया मूल्य"। इसलिए जब तक मैं मूल्य हिट नहीं करता तब तक मुझे लूप करना पड़ता है। बहुत कष्टप्रद। क्या वह वर्णमाला इस परिदृश्य को उचित रूप से संभाल लेगा? (यदि हां, तो यह बहुत अच्छा है ... मैं newLineCharacterSet का उपयोग कर रहा हूं .. किसी ने भी देखा है कि – clifgriffin

+0

मुझे लगता है कि इस चरित्र सेट को शुरुआत में नई लाइनों और रिक्त स्थान के किसी भी संयोजन से निपटना चाहिए। इसे आज़माएं :) –

+0

आप बहुत ही अच्छे हैं। यह चमत्कार काम किया। धन्यवाद। – clifgriffin

संबंधित मुद्दे