如何在iOS 7中解析此xml?

时间:2013-11-26 12:23:52

标签: ios ios7 nsxmlparser

我想解析这个XML:

<?xml version="1.0" encoding="UTF-8"?>
<eventdata>
    <rev></rev>
        <event>
            <id>  </id>
            <name></name>
            <thumb> </thumb>
            <eimage></eimage>
            <artists></artists>
            <date></date>
            <time> </time>
            <cost> </cost>
            <discount> </discount>
            <gmap> </gmap>
            <web>  </web>
            <desc> </desc>
            <file>
                <src> </src>
                <size> </size>
            </file>
            <vtype></vtype>
            <address></address>
            <area></area>
            <city></city>
            <pcode></pcode>
            <lmark></lmark>
            <likes></likes>
            <hl></hl>
            <pref></pref>
            <img>  <src> </src>
                <size> </size></img>
            <vid>  <src> </src>
                <size> </size></vid>

        </event>
        <event>
            <id>  </id>
            <name></name>
            <thumb> </thumb>
            <eimage></eimage>
            <artists></artists>
            <date></date>
            <time> </time>
            <cost> </cost>
            <discount> </discount>
            <gmap> </gmap>
            <web>  </web>
            <desc> </desc>
            <file>
                <src> </src>
                <size> </size>
            </file>
            <vtype></vtype>
            <address></address>
            <area></area>
            <city></city>
            <pcode></pcode>
            <lmark></lmark>
            <likes></likes>
            <hl></hl>
            <pref></pref>
            <img>  <src> </src>
                <size> </size></img>
            <vid>  <src> </src>
                <size> </size></vid>

        </event>
        </eventdata>

我试过这段代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    _currentParsedCharacterData = [[NSMutableString alloc] init];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"NewYearPartyData" ofType:@"xml"];
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];

    // parsing...
    BOOL success = [parser parse];


    // test the result
    if (success) {
        //NSLog(@"No errors - user count : %i", [parser [users count]]);
        NSLog(@"Success");
        // get array of users here
        //  NSMutableArray *users = [parser users];
    } else {
        NSLog(@"Error parsing document!");
    }
 }

#pragma mark - NSXMLParser delegate methods

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqualToString:@"event"]) {
        NSLog(@"user element found – create a new instance of User class...");
        //user = [[User alloc] init];
        //We do not have any attributes in the user elements, but if
        // you do, you can extract them here:
        // user.att = [[attributeDict objectForKey:@"<att name>"] ...];
        NSLog(@"Event Name = %@", [attributeDict objectForKey:@"name"]);
        NSLog(@"attributeDict = %@", attributeDict);
    }
    if ([elementName isEqualToString:@"name"]) {
        NSLog(@"attributeDict = %@", attributeDict);
        NSLog(@"Event Name = %@", [attributeDict objectForKey:@"name"]);
        NSLog(@"elementName = %@", elementName);
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"didEndElement");
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    NSLog(@"value of element %@", string); 
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
}

非常擅长NSXMLParser的人 - 我请他们向我解释这个流程。

我的基本问题是“如何访问事件数据,即id,name,thumb等?”

1 个答案:

答案 0 :(得分:3)

NSXMLParsing是解析数据的最简单方法。您可以使用委托方法

轻松访问事件数据,即id,name,thumb等
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

}

您可以创建名为Event的NSOBject类。 这是我的代码

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([elementname isEqualToString:@"id"])
        {
            event.id = currentNodeContentChapters;
        }
        if ([elementname isEqualToString:@"name"])
        {

            event.name = currentNodeContentChapters;
        }
        if ([elementname isEqualToString:@"thumb"])
        {
            event.thumb = currentNodeContentChapters;
        }
        if ([elementname isEqualToString:@"eiimage"])
        {
            event.eiimageUrl = currentNodeContentChapters;
        }
       -------------------
       --------------- 
}

希望它对你有用。