Search code examples
iosxmluitableviewswift4parsexml

why parsing xml file in swift 4 will return incomplete?


so here is the codes that I used for parsing xml and the texts that I received in the print and the table view were incomplete

func parser(_ parser: XMLParser, foundCharacters string: String) {
    let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

    print(data)
    if data.count != 0 {
        switch termsItem {

        case "title" : termsTitle = data
        case "body" : termsTexts = data
            default :
            break


        }


    }
}

and I change this code to this below and now the print of the xml is correct But I can't see any thing in the table view

func parser(_ parser: XMLParser, foundCharacters string: String) {
    let data = string

    print(data)
    if data.count != 0 {
        switch termsItem {

        case "title" : termsTitle = data
        case "body" : termsTexts = data
            default :
            break


        }


    }
}

here is the other codes But I didn't changed them

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {


    termsItem = elementName

}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {


    if elementName == "item" {

        var theItems = Item()
        theItems.title = termsTitle
        theItems.body = termsTexts
        print(theItems)
        print(theItems.title)
        print(termsTitle)
        tableViewterms.append(theItems)

    }

    DispatchQueue.main.async {

        self.otherTopicsTableView.reloadData()



    }

  }
}

Solution

  • so I had to append the data and then remove them now this code is working well

     func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
    
        termsItem = elementName
    
    }
    
    func parser(_ parser: XMLParser, foundCharacters string: String) {
        let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
    
        if data.count != 0 {
            switch termsItem {
    
            case "title" : termsTitle = data
            case "body" : termsTexts.append(contentsOf: data)
                default :
                break
    
            }
        }
    }
    
    
    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    
    
        if elementName == "item" {
    
            var theItems = Item()
            theItems.title = termsTitle
            theItems.body = termsTexts
            print(theItems.body)
            termsTexts.removeAll()
            tableViewterms.append(theItems)
    
        }
    
        DispatchQueue.main.async {
    
            self.otherTopicsTableView.reloadData()
    
        }
    
    }