代码之家  ›  专栏  ›  技术社区  ›  Warrior

如何在iphone中解析带有属性的标签的内容?

  •  0
  • Warrior  · 技术社区  · 14 年前

    我是iphone开发新手。我想从url的HTML文件中解析和检索特定内容。我有一个来自这个链接的示例代码 http://blog.objectgraph.com/index.php/2010/02/24/parsing-html-iphone-development/

     NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding];
     TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];  
     NSArray *elements  = [xpathParser search:@"//h3"]; // get the page title
     TFHppleElement *element = [elements objectAtIndex:0];
     NSString *h3Tag = [element content];  
     NSLog(h3Tag);
     mLabel.text = h3Tag;
    

    他们正在检索源的h3标签中的内容,它正确显示“联系我们”。我已经将url更改为堆栈溢出并搜索了标题,它正确地检索了标题。但我无法检索标签属性的内容。请帮帮我。请引导我。谢谢

    1 回复  |  直到 14 年前
        1
  •  0
  •   Bryan Clark    14 年前

    您可以在查询中使用//h3[@attribute='something']

    可以使用[element attributes]获取与该特定属性关联的所有元素。