【问题标题】:XML attribute pasrsing problemXML属性解析问题
【发布时间】:2011-10-11 00:58:18
【问题描述】:

我必须解析一个 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 解析器时会检测到它但不返回它的值。 那我怎样才能得到它的价值呢?

【问题讨论】:

    标签: iphone objective-c attributes xml-parsing


    【解决方案1】:

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

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

    你只是指元素...

    【讨论】:

      【解决方案2】:

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

              - (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);
                  }
              }
      

      【讨论】:

        【解决方案3】:

        didStartElement: 中,您正在检查“运动”,但在 didEndElement: 中,您正在检查“运动”...

        【讨论】:

          猜你喜欢
          • 2010-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-27
          • 2010-11-02
          相关资源
          最近更新 更多