ios中的xml解析器

时间:2013-03-24 11:36:21

标签: ios xml-parsing

有代码

`-(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是project.It不会创建新对象。并将其添加到数组中。它只是退出......

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

1 个答案:

答案 0 :(得分:0)

您可以使用此处提供的现成班级:http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

非常好,它会在传递你的xml数据后返回一个字典。