ios - Swift / How to parse the correct element from a web blog with NSXMLParser -


i'm trying download specific information web blog following code:

    func parser(parser: nsxmlparser, didstartelement elementname: string, namespaceuri: string?, qualifiedname qname: string?, attributes attributedict: [string : string]) {          if elementname == "item" {             currentevent = eventposts()             return         }          if elementname == "eventdate" {             parsedelement = "eventdates"         }         }      func parser(parser: nsxmlparser, foundcharacters string: string) {          let str = string.stringbytrimmingcharactersinset(nscharacterset.whitespaceandnewlinecharacterset())          if parsedelement == "eventdates" && str != "" {             if currentevent.eventdates.isempty {                            currentevent.eventdates = str                     print(currentevent.eventdates)         }     } 

the issue is, parser downloads 1 element "eventdates" web blog feed , 3 elements of "title" , don't why...

any idea how fix issue? appreciated.

edit: ouput is

04.03.16 (thats element of eventdate) djanes delight 17.03.16 (thats element of title) geiles leben ab 04.03.16 (thats element of title) wildstyle jeden samstag (thats element of title) 

but should be:

04.03.16 03.03.16 02.03.16 01.03.16 

it seems never reset parsedelement variable once used cause outer if statement in func parser true once has been set

func parser(parser: nsxmlparser, didstartelement elementname: string, namespaceuri: string?, qualifiedname qname: string?, attributes attributedict: [string : string]) {      if elementname == "item" {         currentevent = eventposts()         return     }      if elementname == "eventdate" {         parsedelement = "eventdates"     }     }  func parser(parser: nsxmlparser, foundcharacters string: string) {      let str = string.stringbytrimmingcharactersinset(nscharacterset.whitespaceandnewlinecharacterset())      if parsedelement == "eventdates" && str != "" {         if currentevent.eventdates.isempty {                        currentevent.eventdates = str             parsedelement = ""                 print(currentevent.eventdates)     } } 

while solution might work in basic case might want read on xml sax parsing. there lot of explanations java out there same principle.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -