Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...