XML属性解析问题

发布于 2024-11-25 12:13:58 字数 2368 浏览 1 评论 0原文

我必须解析 xml,该 xml 的链接是

http://qasimshah.sitesled.com/schedule。 xml

可能要解析的是:

-(IBAction)autoLoginBtn{

    NSString *url=[NSString stringWithFormat: @"http://qasimshah.sitesled.com/schedule.xml"];

    [self parseXMLFileAtURL:url];

}
- (void)parseXMLFileAtURL:(NSString *)URL
{   
    xmlDataArray1 = [[NSMutableArray alloc] init];

    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    NSXMLParser *rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];

}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
    NSLog(@"Error on XML Parse: %@", [parseError localizedDescription] );
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
    //NSLog(@"found this element: %@", elementName);
    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"Sports"]) {
        // clear out our story item caches...
        item = [[NSMutableDictionary alloc] init];
        currentTitle = [[NSMutableString alloc] init];
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
    if ([elementName isEqualToString:@"Sport"]) {

        [item setObject:currentTitle forKey:@"id"];

        [xmlDataArray1 addObject:[item copy]];
        NSLog(@"id: %@", currentTitle);
    }

}

但是当我尝试获取“id”或 name 解析器检测到它但不返回它的值。 那么我怎样才能得到它的值呢?

i have to parse an xml and the link of that xml is

http://qasimshah.sitesled.com/schedule.xml

and may to parse is:

-(IBAction)autoLoginBtn{

    NSString *url=[NSString stringWithFormat: @"http://qasimshah.sitesled.com/schedule.xml"];

    [self parseXMLFileAtURL:url];

}
- (void)parseXMLFileAtURL:(NSString *)URL
{   
    xmlDataArray1 = [[NSMutableArray alloc] init];

    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    NSXMLParser *rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];

}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
    NSLog(@"Error on XML Parse: %@", [parseError localizedDescription] );
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
    //NSLog(@"found this element: %@", elementName);
    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"Sports"]) {
        // clear out our story item caches...
        item = [[NSMutableDictionary alloc] init];
        currentTitle = [[NSMutableString alloc] init];
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
    if ([elementName isEqualToString:@"Sport"]) {

        [item setObject:currentTitle forKey:@"id"];

        [xmlDataArray1 addObject:[item copy]];
        NSLog(@"id: %@", currentTitle);
    }

}

but when i try to get 'id' or name parser detect it but do not return me its value.
so how can i get its value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泼猴你往哪里跑 2024-12-02 12:13:58

您应该在“attributeDict”中获取属性值,

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

您仅引用元素...

You should get the attribute value in "attributeDict"

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

you are only referring element ...

倾`听者〃 2024-12-02 12:13:58

试试这个。你可以像这样得到所有的值。

        - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
          namespaceURI:(NSString *)namespaceURI 
         qualifiedName:(NSString *)qName 
            attributes:(NSDictionary *)attributeDict
        {   
            if([elementName isEqualToString:@"Sport"])
            {
                NSString *s = [NSString stringWithFormat:@"%@",elementName];
                NSLog(@"%@",s);

                NSString *s1 =[NSString stringWithFormat:@"%@",[attributeDict valueForKey:@"id"]];
                NSLog(@"%@",s1);

                NSString *s2 =[NSString stringWithFormat:@"%@",[attributeDict valueForKey:@"name"]];
                NSLog(@"%@",s2);

                NSString *s3 =[NSString stringWithFormat:@"%@",[attributeDict valueForKey:@"abbr"]];
                NSLog(@"%@",s3);
            }
        }

Try this. You can get all the values like this.

        - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
          namespaceURI:(NSString *)namespaceURI 
         qualifiedName:(NSString *)qName 
            attributes:(NSDictionary *)attributeDict
        {   
            if([elementName isEqualToString:@"Sport"])
            {
                NSString *s = [NSString stringWithFormat:@"%@",elementName];
                NSLog(@"%@",s);

                NSString *s1 =[NSString stringWithFormat:@"%@",[attributeDict valueForKey:@"id"]];
                NSLog(@"%@",s1);

                NSString *s2 =[NSString stringWithFormat:@"%@",[attributeDict valueForKey:@"name"]];
                NSLog(@"%@",s2);

                NSString *s3 =[NSString stringWithFormat:@"%@",[attributeDict valueForKey:@"abbr"]];
                NSLog(@"%@",s3);
            }
        }
秋风の叶未落 2024-12-02 12:13:58

didStartElement: 中,您正在检查“Sports”,但在 didEndElement: 中,您正在检查“Sport”...

In didStartElement: you're checking for "Sports" but in didEndElement: you're checking for "Sport"...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文