1
<html>
  <head>
    <title>this value i want to grab</title>
  </head>
  <body> </body>
</html>

I have this NSString html, and I want to grab value in tag title? Can anybody help me?

vaultah
  • 44,105
  • 12
  • 114
  • 143
vman
  • 65
  • 2
  • 5
  • Possible duplicate of http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone – C0deH4cker Jun 19 '12 at 05:12
  • You can use Objective-C-HTML-Parser. Check [here](https://github.com/zootreeves/Objective-C-HMTL-Parser) for more detail. – Tamarous Jan 02 '17 at 12:09

1 Answers1

4

Use NSXMLParser, together with NSXMLParserDelegate.

In your case it is really simple. You just watch for @"title" in parser:didStartElement and in parser:foundCharacters: add the content to your own string variable, finalizing it on parser:didEndElement:.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • 1
    XHTML is becoming the standard (HTML in proper XML syntax), but even normal html can usually be parsed as XML. No reason to downvote this answer. – C0deH4cker Jun 19 '12 at 05:18
  • 1
    This will work very well. The structure of XML and HTML is similar. I have used this in the past successfully. Don't understand the down-vote. :-( – Mundi Jun 19 '12 at 05:18
  • No reason to downvote. It's yet better/more elegant than running some messy JavaScript in a web view. Wasting his shot. +1. –  Jun 19 '12 at 05:34
  • XML and HTML may look similar, but HTML parsers operate under [significantly different rules than XML parsers](http://wiki.whatwg.org/wiki/HTML_vs._XHTML). – 一二三 Jun 19 '12 at 12:35
  • @一二三 In the very document you reference it is stated that modern HTML (HTML5) is indeed XML. I do not see how the differences mentioned in the document would lead to errors with `NSXMLParser`. Software is about solving practical problems, not conceptual fundamentalism. – Mundi Jun 19 '12 at 12:47
  • The document is somewhat outdated in that respect: HTML5 comes in [two syntax flavours](http://www.w3.org/TR/html5/introduction.html#html-vs-xhtml), only one of which is a type of XML. Things `NSXMLParser` will not handle include [void elements](http://www.w3.org/TR/html-markup/syntax.html#syntax-elements) (`
    `, ``, etc.) and other tag omission rules (like those on [`

    `](http://www.w3.org/TR/html-markup/p.html#p-tags)). Use of these features exists, is recommended by the W3C, and is not "conceptual fundamentalism".

    – 一二三 Jun 19 '12 at 13:10