如何在ios中将字符串与xml分开

时间:2012-02-08 10:54:31

标签: iphone objective-c cocoa

我有一个可以分享到evernote的注释,我已成功完成。但是有一个名为import note的选项。当我们点击导入按钮时,evernote中的注释必须加载到importpage textview中。但是我的问题是是来自xml formate中的evernote appers的注释,其中note.my代码用于向evernote服务器上传注释

-(IBAction)sendNoteEvernote:(id)sender{
EDAMNote * note = [[[EDAMNote alloc] init]autorelease];

    // Setting initial values sent by the user
    note.title = @"mybibleapp";

 NSMutableString *str = [[NSMutableString alloc] initWithString:@"NOTES:"]; 

    for (int i = 0; i<[appDelegate.notesArray count]; i++) { 
        NSString * aString = [[NSString alloc] initWithString:[appDelegate.notesArray objectAtIndex:i]] ;
        NSString * ENML= [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n<en-note>%@",aString];
//aString contains the note value

ENML = [NSString stringWithFormat:@"%@%@", ENML, @"</en-note>"];
    NSLog(@"%@", ENML);

    // Adding the content & resources to the note
    [note setContent:ENML];
 @try {
        [[Evernote sharedInstance] createNote:note];
        _acteverbackup.hidden = YES;
        _actimageeverbackup.hidden =YES;
    }
    @catch (EDAMUserException * e) {
        _acteverbackup.hidden = YES;
        _actimageeverbackup.hidden =YES;
        NSString * errorMessage = [NSString stringWithFormat:@"Error saving note: error code %i", [e errorCode]];

        proAlertView *alert = [[proAlertView alloc]initWithTitle:@"Evernote" message:errorMessage delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [alert setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0] withStrokeColor:[UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:1.0]];
        [alert show];
        [alert release];      
  return;
    }

我正确地得到了永远的笔记 这是我的下载或导入代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Load the EDAMNote object that has guid we have stored in the object
    EDAMNote * note = [(Evernote *)[Evernote sharedInstance] getNote:guid];
noteNavigation.topItem.title = [note title];

    noteContent.text = [note content];
    //noteContent is the textview which display the note
    // Adding a back button to close the windows
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(goBack:)];

    UINavigationItem *item = [[[UINavigationItem alloc] init] autorelease];
    item.leftBarButtonItem = doneButton;
    item.hidesBackButton = YES;
    [noteNavigation pushNavigationItem:item animated:NO];
    noteNavigation.topItem.title = [note title];


}

noteContent是显示注释的textview,这里是获取此值

<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n<en-note>%@",mynote is here</en-note>

在此textview中我只需要mynote is here。如何将其与注释分开?

1 个答案:

答案 0 :(得分:0)

你可以使用一个非常好的NSString类别,它是项目MWFeedParser的一部分。更具体地说,您查找文件NSString + HTML。

NSString + HTML类别现在支持XML解码,您可以阅读它here

…NSString+XMLEntities category depreciated and replaced by NSString+HTML with improved HTML character entity encoding/decoding via Google Toolbox for Mac.

此类别将以下方法添加到NSString类

- (NSString *)stringByStrippingTags;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;

您应该能够使用它以 smart 的方式将XML转换为NSString。

NSString *summary = [[[item.summary stringByStrippingTags] stringByRemovingNewLinesAndWhitespace] stringByDecodingHTMLEntities];

希望它有所帮助!