hpple parse xml , [element content] always be null

786 Views Asked by At

1.this is my code

NSStringEncoding encoding2 = NSUTF8StringEncoding;
// i am from china
NSString *c1 = @"<html><head><title A='sss'>sss</title></head><body><a href='sssssss这个中文就么哟sa哈aa'>asas</a></body></html>";
htmlData = [c1 dataUsingEncoding:encoding2];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements  = [xpathParser searchWithXPathQuery:@"//a"]; 
TFHppleElement *element = [elements objectAtIndex:0];
NSString *myTitle = [element content];
NSLog(@"%@",element.tagName);
NSLog(@"%@",myTitle);
NSString *attr = [element objectForKey:@"href"];
NSLog(@"%@",attr);
  1. this is the out put

    **
    2012-08-12 14:59:34.624 HHXY[1506:f803] a
    2012-08-12 14:59:34.625 HHXY[1506:f803] (null)
    2012-08-12 14:59:34.625 HHXY[1506:f803] sssssss这个中文就么哟sa哈aa
    **
    

a easy test but the content(myTitle) always be null , i don't know why? help me.be greatly appreciated

1

There are 1 best solutions below

0
On

I had a similar problem.

Try to replace this line:

NSString *myTitle = [element content];

with next:

NSString *myTitle = [[element firstChild] content];

or change your regular expression:

@"//a" 

with

@"//a/text()"

I hope that one of these ways will help you!