我无法解析包含XML数据中的换行符和引号的大量文本。我正在通过AppDelegate运行解析器,以便在应用程序打开时预加载。我的问题是在解析器的didEndElement部分中正确地引入了文本,但是当我的视图从AppDelegate调用来自NSMutableArray的文本时,\ n和其他html标记出现时它们不应该出现。屏幕截图显示在此问题的底部。
AppDelegate.h相关代码:
@interface AppDelegate : UIResponder <UIApplicationDelegate, NSXMLParserDelegate>
{
NSXMLParser *exhibitParser;
NSMutableArray *exhibitInfo;
}
@property (nonatomic, retain) NSXMLParser *exhibitParser;
@property (nonatomic, retain) NSMutableArray *exhibitInfo;
- (void) parseAllData;
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "ExhibitParser.h"
@implementation AppDelegate
@synthesize exhibitParser, exhibitInfo;
@synthesize window = _window;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//delay to allow for splash screen
[self performSelector:@selector(parseAllData) withObject:nil afterDelay:2.0f];
return YES;
}
- (void)parseAllData
{
exhibitParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.website-Goes-Here.com/variable=%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"gallery_preference"]]]];
ExhibitParser *exParser = [[ExhibitParser alloc] initExhibitParser];
[exhibitParser setDelegate:exParser];
//[parser parse];
BOOL exsuccess = [exhibitParser parse];
if(exsuccess)
NSLog(@"No Exhibit Errors");
else
NSLog(@"Exhibit ERROR!!");
}
ExhibitParser.h:
#import <Foundation/Foundation.h>
@class AppDelegate, ExhibitInfo;
@interface ExhibitParser : NSObject
{
NSString *currentExhibit;
NSMutableString *descAttribute;
NSMutableDictionary *dict;
AppDelegate *exAppDelegate;
ExhibitInfo *aExItems;
}
@property (nonatomic, retain) NSString *currentExhibit;
@property (nonatomic, retain) NSMutableString *descAttribute;
@property (nonatomic, retain) NSMutableDictionary *dict;
@property (nonatomic, retain) ExhibitInfo *aExItems;
- (ExhibitParser *) initExhibitParser;
@end
ExhibitParser.m:
(注意:didEndElement中的一系列格式化取得了XML文本,并使我们在iPad上看起来更好。在这里写的\ n在下面的NSLog中显示为正确的换行符,但显示为字面上“\ n”一旦显示在ExhibitView中。)
#import "ExhibitParser.h"
#import "AppDelegate.h"
#import "ExhibitInfo.h"
@implementation ExhibitParser
@synthesize currentExhibit, descAttribute, dict, aExItems;
- (ExhibitParser *) initExhibitParser
{
self = [super init];
exAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentExhibit = [elementName copy];
if ([elementName isEqualToString:@"ExhibitData"])
{
exAppDelegate.exhibitInfo = [[NSMutableArray alloc] init];
}
if ([elementName isEqualToString:@"exhibit"])
{
aExItems = [[ExhibitInfo alloc] init];
dict = [[NSMutableDictionary alloc] init];
descAttribute = [[NSMutableString alloc] init];
}
if ([attributeDict objectForKey:@"exhibitfact"])
{
descAttribute = [attributeDict objectForKey:@"exhibitfact"];
[dict setObject:descAttribute forKey:@"exhibitfact"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"exhibit"])
{
[exAppDelegate.exhibitInfo addObject:aExItems];
[dict setObject:descAttribute forKey:@"exhibitfact"];
//Formatting Series to make it pretty
NSString *descriptionRaw = [NSString stringWithFormat:@"%@", descAttribute];
NSString *descriptionFinal = [descriptionRaw stringByReplacingOccurrencesOfString:@" " withString:@"\n"];
NSString *descriptionEdited = [descriptionFinal stringByReplacingOccurrencesOfString:@" " withString:@" "];
//This Log displays the text perfectly
NSLog (@"%@", descriptionEdited);
[aExItems setDescription:descriptionEdited];
}
}
//I have read that foundCharacters can cause issues with hidden characters such as \n, so I've tried leaving this in and commenting it out. It seems to have no effect either way.
//- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
//{
//
// if([self.currentExhibit isEqualToString:@"description"])
// {
// [self.descAttribute appendString:string];
// }
//}
@end
ExhibitInfo.h:
#import <Foundation/Foundation.h>
@interface ExhibitInfo : NSObject
{
NSString *description;
}
@property (nonatomic, retain) NSString *description;
@end
ExhibitInfo.m:
#import "ExhibitInfo.h"
@implementation ExhibitInfo
@synthesize description;
@end
ExhibitView.m(其中的文本框将填充解析的数据):
- (void)viewDidLoad
{
[super viewDidLoad];
[self setTitle:@"Exhibit Information"];
exAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
exhibitText.text = [NSString stringWithFormat:@"%@", [exAppDelegate.exhibitInfo description]];
}
因此,我完全被大脑砸碎的地方是,ExhibitParser.m中的NSLog正确显示了整个文本,但是在ExhibitView.m中,当exhibitText.text填充了我认为应该与之相同的内容时NSLog,它不是。它显示了整个身体周围的\ n和开启和关闭()。这是因为它在在ExhibitView中调用之前被写入NSMutableArray吗?为什么NSLog和视图显示有什么区别?
NSLog输出的屏幕截图 - 正确显示:
应用程序运行时在视图中显示的方式的屏幕截图(注意开头和结尾的括号,\ n来自我的格式和换行符,开头和结尾的引号,以及\旁边的引号在文本正文中间):
答案 0 :(得分:1)
当您找到元素时,尝试以这种方式删除换行符:
if ([attributeDict objectForKey:@"exhibitfact"])
{
descAttribute = [[attributeDict objectForKey:@"exhibitfact"] stringByReplacingOccurrencesOfString:@"/n" withString:@""];
[dict setObject:descAttribute forKey:@"exhibitfact"];
}