Swift - Parse in HTML

Asked

Viewed 129 times

1

Currently I have a project for IOS 9 where I have a Uiwebview component, I am recovering a web page and presenting within this component.

But my goal is to take only the first form that is presented within the HTML, that is, I want to return only the HTML of the first form and present it in the formatted Uiwebview as a web page. Any idea how to do that?

  • in Portuguese it is very difficult to find good materials, but in this link here you will find what you are looking for and in a very didactic way, I think: http://www.raywenderlich.com/14172/how-to-parse-html-on-ios I hope you help.

  • Also here: http://stackoverflow.com/questions/28261570/parsing-html-in-swift-other-than-using-regex

  • I need something that is purely in Swift, the material can be in Portuguese, English or Spanish

1 answer

3


Hello.

You can use the framework hpple for this. The framework is built in Objective-C, but you can use it in Swift. I made a simple example, which loads an HTML page with two Forms (distributed together with the app), parses the HTML and shows only the first form.

This is the HTML page:

<!DOCTYPE HTML>
<html>
    <head>
        <title>Test page with 2 forms</title>
    </head>
    <body>
        <h1>Form 1</h1>
        <form id="form1" action="your_action_here">
            <input type="text" value="You name here">
            <input type="submit">
        </form>

        <h1>Form 2</h1>
        <form id="form2">
            <input type="text" value="You name here">
            <input type="submit">
        </form>
    </body>
</html>

I made a simple Viewcontroller, which finds and loads the first HTML form in the viewDidLoad method.

import UIKit
import hpple

class ViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let htmlFile = NSBundle.mainBundle().pathForResource("index", ofType: "html", inDirectory: "www") {
            let htmlData = NSData(contentsOfFile: htmlFile)
            let htmlDocument = TFHpple(HTMLData: htmlData)
            if let htmlElements = htmlDocument.searchWithXPathQuery("//form[@id='form1']") as? [TFHppleElement] {
                if let firstForm = htmlElements.first?.raw {
                    webView.loadHTMLString(firstForm, baseURL: nil)
                }
            }
        } else {
            print("File not found")
        }
    }
}

You can see the complete source code in this repository on Github. I used the Cocoapods to add the hpple dependency. If you want more information about Cocoapods, you can see this tutorial.

  • If the form has an action filled, when clicking the Submit button the form will be sent and I will have the return given by the web page? This is referring to an internet link.

  • 1

    I did not run the test, but I believe so. However, you should consider a few things: 1. The extracted form will not appear formatted. You will need to create a page and add the CSS that came from the source page to display the HTML correctly. 2. If the page has some dynamic behavior, you will have to load Javascript as well. I suggest you do this in another way, simulated a post for the action that is described in the form. This is something more robust than showing a piece of the page. I suggest you talk to the page owner to see if you can really use this technique.

  • Perfect. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.