Swift 3 XCode 8.3.3 XML解析

[英]Swift 3 XCode 8.3.3 XML Parsing


I am working on trying to parse some XML data to a tableView and I keep coming up with my string being empty, thus not allowing me to populate a tableView. I have checked all over online trying to find good example on how to achieve this but everything seems to be out of date due to swift changing all the time.

我正在嘗試將一些XML數據解析為tableView,並且我不斷提出我的字符串為空,因此不允許我填充tableView。我已經在網上查了一下,試圖找到如何實現這個目標的好例子,但由於一直在迅速改變,一切似乎已經過時了。

<?xml version="1.0" encoding="UTF-8"?>
<client region = "Midwest">
<person
name = "Pete Seeger"
profession = "Musician"
dob = "May 3, 1919">
<child
name = "Daniel">
</child>
<child
name = "Mika">
</child>
<child
name = "Tinya">
</child>
</person>
</client>

Next is my Client.swift file

接下來是我的Client.swift文件

class Client{
var clientName: String = String()
var clientProfession: String = String()
var clientDob: String = String()}

and in my ViewController I have declared these variables

在我的ViewController中,我已經聲明了這些變量

var client: [Client] = []
var eName: String = String() //element Name
var cName: String = String()
var cProfession: String = String()
var cDOB: String = String()

Preceding is my didStart/didEnd/and foundCharacters

前面是我的didStart / didEnd /和foundCharacters

 func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
    //this finds the starting tag of <person>
    eName = (elementName as NSString) as String
    if eName == "person"{
        print("we found start tag of person")
        cName = String()
        cProfession = String()
        cDOB = String()
}

 func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
    //this looks for the end tag </person>
    if eName == "person"{
        print("we found End tag of person")
        let client = Client() //letting the client = to w.e
        print(client, "printing client in the didEndElement")
        client.clientName = cName
        print(cName, "This is the cName though nothing will show I bet...")
        client.clientProfession = cProfession
        client.clientDob = cDOB
    }
func parser(_ parser: XMLParser, foundCharacters string: String)
{

let foundChar = string.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)

    print(foundChar, "This is foundChar for foundCharacters")
    if(foundChar.isEmpty){
        print("its empty")
        }
        else{
            if eName == "name"{
                cName += foundChar
                cProfession += foundChar
                cDOB += foundChar
        }

    }

I am finding the beginning tags and closing tags for person, but once it gets to the foundCharacters it seems that it is returning empty. Am I overlooking something here?

我找到了人的開始標記和結束標記,但是一旦它到達foundCharacters,它似乎返回空。我在這里俯瞰什么嗎?

1 个解决方案

#1


0  

You are looking for your data at the wrong place.

您正在錯誤的地方尋找您的數據。

parser(_:foundCharacters:) function is getting called for text that is in between open and close tags.

正在調用解析器(_:foundCharacters :)函數,用於打開和關閉標記之間的文本。

For the following example XML:

對於以下示例XML:

<name>Pete Seeger</name>

parser(_:foundCharacters:) function will be called with foundCharacters = "Pete Seeger"

將使用foundCharacters =“Pete Seeger”調用解析器(_:foundCharacters :)函數

The data that you are looking for is in element's attributes. Just check the attributeDict parameter in parser(_:didStartElement:namespaceURI:qualifiedName:attributes:) function.

您要查找的數據位於元素的屬性中。只需檢查解析器中的attributeDict參數(_:didStartElement:namespaceURI:qualifiedName:attributes :)函數。

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
    //this finds the starting tag of <person>
    eName = (elementName as NSString) as String
    if eName == "person"{
        print("we found start tag of person")
        cName = attributeDict["name"] ?? ""
        cProfession = attributeDict["profession"] ?? ""
        cDOB = attributeDict["dob"] ?? ""
    }
}

Also, if you are interesting in mapping the XML into swift objects consider XMLMapper. (uses the same technique as the ObjectMapper)

此外,如果您有興趣將XML映射到swift對象,請考慮使用XMLMapper。 (使用與ObjectMapper相同的技術)

Hope this helps.

希望這可以幫助。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/11/15/72fab354bf24bca308df780edeeaf964.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com