<div>
<p>
<a href="#" class="transcriptLink" onclick="seekVideo(0); return false;">A few years ago,</a>
<a href="#" class="transcriptLink" onclick="seekVideo(2000); return false;">I felt like I was stuck in a rut,</a>
<a href="#" class="transcriptLink" onclick="seekVideo(5000); return false;">so I decided to follow in the footsteps</a>
<a href="#" class="transcriptLink" onclick="seekVideo(7000); return false;">of the great American philosopher, Morgan Spurlock,</a>
<a href="#" class="transcriptLink" onclick="seekVideo(10000); return false;">and try something new for 30 days.</a>
</p>
</div>
这是网络代码的一部分。
我想知道如何获取标签中的文字。如:“几年前,”
我可以在"<a> text </a>",
但我不知道如何在"<a href="#" class="transcriptLink" onclick="seekVideo(0); return false;">A few years ago,</a> "
<a href="#" class="transcriptLink" onclick="seekVideo(0); return false;">
<a href="#" class="transcriptLink" onclick="seekVideo(2000); return false;">
....................
只有onclick="seekVideo(....);
/div/p/a[substring-before(substring-after(@onclick, '('), ')') = '0']/text()
可以获得"A few years ago";
但现在我想得到seekVideo(number)
中的数字????
/div/p/a[number(substring-before(substring-after(@onclick, '('), ')'))]
不对!
例如:<a href="#" class="transcriptLink" onclick="seekVideo(2000); return false;">
示例项目或代码更好,谢谢
答案 0 :(得分:0)
如果它是一个有效的XML(从您向我们展示的示例中它是有效的XML),您可以使用NSXMLParser进行解析。
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(!currentString){
currentString = [[NSMutableString alloc] init];
}
[currentString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"a"]){
NSLog(@"Text betweeen \"a\" tag %@",currentString);
}
[currentString release],currentString =nil;
}
要从seekVideo(0)获得0,请使用
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"a"]){
NSString* onClickAttribString = [attributeDict objectForKey:@"onclick"];
NSLog(@"OnClick Attrib: %@",onClickAttribString);
// seekVideo(0); return false;
NSArray* arrayList = [onClickAttribString componentsSeparatedByString:@" "]; // gives array of string seperated by space
NSLog(@"seekvideo %@",[arrayList objectAtIndex:0]);
//Process seekvideo(0); to get number... use NSScanner
NSString* seekVideoString = [arrayList objectAtIndex:0];
NSString* numberString = [seekVideoString stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSLog(@"Number : %@",numberString);
// if you want numberString converted to integer then [numberString intValue]
}
}