iphoneiosxml-parsingtbxml

get value of an element by parse xml in ios


I'm using TBXML to parse xml. But I have a problem. This is my xml file

<i va="00:13:025">*4</i>
<i va="00:18:915">*3</i>
<i va="00:19:995">*2</i>
<i va="00:21:075">*1</i>

I used the command

TBXMLElement *lyric = [TBXML childElementNamed:@"i" parentElement:param];

to get value "*4". How can I get value on va=""? Do you have any ideas or some examples to slove this? Thanks.


Solution

  • You can get that values by using NSXMLParser..

    NSXMLParser *xmlstr = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]] ;
    xmlstr.delegate = self;
    [xmlstr parse];
    

    And use this delegate method.

     - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
         NSLog(@"%@",[attributeDict description]);
    }
    

    This attributeDict will gives you the attribute in that started tag.