0

有代码

`-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"%@",elementName);
    if ([elementName isEqual:@"impproject"]) {
        impprojectdata=[[impProjectData  alloc]init];
        [impprojectdata setParentParserDelegate:self];
        [parser setDelegate:impprojectdata];
    }
}

`

然后它来到这里:

    ` -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
        {
        NSLog(@"did start parser:%@", elementName);
        if ([elementName isEqual:@"project"]){

            ProjectDataItem *project=[[ProjectDataItem alloc]init];
            [project setParentParserDelegate:self];
            [parser setDelegate:project];
            [items addObject:project];
           }   
        }`


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"found character:%@", string);
    [currentString appendString:string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"did end parser:%@", elementName);
     currentString=nil;
    if ([elementName isEqual:@"improject"]) {
        [parser setDelegate:parentParserDelegate];
        NSLog(@"items.count is %d",items.count);

    }
}

那么这里

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"startElement %@",elementName);
    if ([elementName isEqual:@"DWMC_CN"]) {
        currentString=[[NSMutableString alloc]init];
        self.name=currentString;
    }else if ([elementName isEqual:@"CZXMMC_CN"]){
        currentString=[[NSMutableString alloc]init];
        self.title=currentString;
    }else if ([elementName isEqual:@"CNNAME"]){
        currentString=[[NSMutableString alloc]init];
        self.name=currentString;
    }else if ([elementName isEqual:@"COMPANY"]){
        currentString=[[NSMutableString alloc]init];
        self.title=currentString;
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [currentString appendString:string];
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    currentString =nil;
    if ([elementName isEqual:@"project"]) {
        [parser setDelegate:parentParserDelegate];
    }
}

然后它返回第二部分代码;

所以有我的问题。它只是在数组'items'中添加一个对象;

不明白为什么……这里是github链接:github.com/TedWei/gaojiaohui

请帮我 。ios新人,可怜的家伙。

我不知道为什么 elementName 是项目。它不会创建新对象。并将其添加到数组中。它只是退出......

对不起我的英语。请帮助我。

4

1 回答 1

0

你可以在这里使用现成的类:http: //troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

它非常好,它会在解析您的 xml 数据后返回一个字典。

于 2013-03-24T12:06:08.987 回答