Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
883 views
in Technique[技术] by (71.8m points)

objective c - Parsing XML data to NSMutableArray iOS - iPhone

This is the XML data:

<Categorys> 
    <Category>  
        <categoryId>25</categoryId>  
            <categoryName>My Photos</categoryName>                                  
                <categoryDescription>Description</categoryDescription>      
            <categoryIconPath>7.jpg</categoryIconPath>      
            <userId>2</userId>  
            <categoryStatus>ON</categoryStatus>  
        <publicPrivate>Private</publicPrivate>
    </Category>
......more categories
<Categorys>

Here I want to get the <categoryName> values into my NSMutableArray categList.

The code I have used is:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
                                       qualifiedName:(NSString *)qualifiedName 
                                          attributes:(NSDictionary*)attributeDict {

    if ( [elementName isEqualToString:@"categList"]) {
        // addresses is an NSMutableArray instance variable
        if (!categList)
            categList = [[NSMutableArray alloc] init];
        return;
    }

    NSLog(@"StartedElement %@", elementName);

    element = [NSMutableString string];

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if(element == nil)

        element = [[NSMutableString alloc] init];

        [element appendString:string];

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
                                      namespaceURI:(NSString *)namespaceURI
                                     qualifiedName:(NSString *)qName {

    NSLog(@"Found an element named: %@ with a value of: %@", elementName, element);

    if ([elementName isEqualToString:@"categoryName"]) {

        NSLog(@"


found::::::::::::::: %@", element);

        category = element;

        NSLog(@"category:::: %@", category);

    }

    [categList addObject:element];
    NSLog(@"categList*************** %@", categList);
}

But I am not getting the category names in my NSMutableArray categList:

Whats wrong in my code? :(

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Hey You can use XMLReader but it will return NSDictionary.

The Following code demo for XMLReader to get NSDictionary.

import XMLReader in your file.

#import "XMLReader.h"

Then use following method to get NSDictionary. Where you have to pass your XML as NSString .

NSDictionary* xmlDict = [XMLReader dictionaryForXMLString:theXML error:&er];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...