3

I am a newbie on Swift and Swift-HTML-Parser. I am using Swift-HTML-Parser from : https://github.com/tid-kijyun/Swift-HTML-Parser

I need some help for below question.

    let myURLString = "http://MytestingWebsite.com/MyAds.html"
    let myURL = NSURL(string: myURLString)

    var error: NSError?

    let myHTMLString = NSString(contentsOfURL: myURL, encoding: NSUTF8StringEncoding, error: &error)

    if let error = error {

        //for below question 2
        println("Error : \(error)")

    } else {

        // code

    }

1) How to get the Src of the Image if there is a) only one image b) a collection of images?  

      < image Src="....."/>

2) What to use to show message when there is an error?


Adding the the required files as per the above link for Swift-HTML-Parser :

1. Add a Bridging file. click Project and goto Building Setting 
Use search: type in Bridging 
Double click on the result : Objective-C Bridging Header 
and Click (+) at the Top to add.

Error msg show: When adding file name with (-) Like Swift-HTML-Parser-Bridging-Header.h 

So, I add SwiftHTMLParserBridgingHeader

2) Copy the File Swift-HTML-Parser-Bridging-Header.h and rename it as SwiftHTMLParserBridgingHeader.h in the project file.

3) Copy HTMLParser.Swift and HTMLNode.swift

4) Have added the reference Libxml2.dylib

When compile, (2) and (3) have red dot. Am I doing something wrong?



0

1 Answer 1

2

This have have a emum for each node that you can search in the HTML

public enum HTMLNodeType : String {
    case HTMLUnkownNode     = ""
    case HTMLHrefNode       = "href"
    case HTMLTextNode       = "text"
    case HTMLCodeNode       = "code"
    case HTMLSpanNode       = "span"
    case HTMLPNode          = "p"
    case HTMLLiNode         = "li"
    case HTMLUiNode         = "ui"
    case HTMLImageNode      = "image"
    case HTMLOlNode         = "ol"
    case HTMLStrongNode     = "strong"
    case HTMLPreNode        = "pre"
    case HTMLBlockQuoteNode = "blockquote"
}

From the example they have in the website:

var err : NSError?
//myHTMLString is the value you retrive from the website
var parser     = HTMLParser(html: myHTMLString, error: &err)
if err != nil {
    //This will log the error and exit the app
    //You probably should display an alert to the user
    println(err)
    exit(1)
}

var bodyNode   = parser.body

if let inputNodes = bodyNode?.findChildTags(HTMLNodeType.HTMLImageNode) {
    for node in inputNodes {
        println(node.contents)
        //this should display the address where the Image is
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I could not test it as I had problem to install the Swift-HTML-Parser-Header.h as required. 1) In Project, goto Build Settings, use search to get Objective-C Bridging header. Then Double click the result and click (+) to add . Got error msg: Swift-HTML-Parser-Header.h isnt a valid name for build setting... 2) Copy the HTMLParser & Node, but they all have red dot sign. What seems to be the problem?
I don't think you need a bridge files as the library is in swift 1.2 already
It seems that this is a wrap around libxml2 so you need to install that too
See my update for how I added the bridging header file. These HTMLParser and HTMLNode.Swift have red dot even I delete the Bridging header file. It wont compile. Do you have this problem? I am using Xcode6.3 the latest.
It work for well form HTML. In my case, I have no control over the Not-well form HTML source. So, this is not working. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.