从XML格式拆分字符串

时间:2012-02-08 09:57:46

标签: iphone

我正在使用evernote api与evernote共享一张便条,它是成功的,它将笔记上传到evernote。但是当我从evernote下载笔记到我的应用程序时,它给了我xml formate.my代码中的注释发送便笺是。 在我的按钮单击 {

 EDAMNote * note = [[[EDAMNote alloc] init]autorelease];
note.title = @"Appname";
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];
ENML = [NSString stringWithFormat:@"%@%@", ENML, @"</en-note>"];
    NSLog(@"%@", ENML);

    // Adding the content & resources to the note
    [note setContent:ENML];
    }

它会将aString的值正确发送到evernote。 我的Downloding代码是

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Load the EDAMNote object that has guid we have stored in the object
    EDAMNote * note = [(Evernote *)[Evernote sharedInstance] getNote:guid];
// Changing the navigation title & the content block
    noteNavigation.topItem.title = [note title];
    //adding note to textview 
    noteContent.text = [note content];

    // 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];


}

它正确下载但是它显示了这样的音符

<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n<en-note> then our noteeeee in this areaaa>

但我希望textview.how中只有then our noteeeee in this areaaa来过滤这个吗?

3 个答案:

答案 0 :(得分:1)

一种方法是使用NSXMLParser。如果您唯一关心的是一行,那么编写解析器委托就很容易了。所有你需要的是:

  • 一个变量(使用NSMutableString)来保存文本,可能称为noteText

  • -parser:didStartElement:namespaceURI:qualifiedName:attributes:方法,以便在您获得noteText代码时清除<en-note>

  • -parser:foundCharacters:方法,可将找到的字符添加到noteText

  • -parser:didEndElement:namespaceURI:qualifiedName:方法在获得noteText代码时使用</en-note>执行某些操作

答案 1 :(得分:0)

您可以实现NSXMLParserDelegate并解析de xml。您可以找到许多样本,例如thisthis

答案 2 :(得分:0)