XMLデータをtableViewに解析しようとしています。文字列が空になっていて、tableViewにデータを入れることができません。私はこれを達成するための良い例を見つけるためにオンラインですべてをチェックしましたが、すべての時間が素早く変化するため、すべてが古くなっているようです。 Swift 3 XCode 8.3.3 XMLの解析
<?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>
次
は私のClient.swiftファイルclass Client{
var clientName: String = String()
var clientProfession: String = String()
var clientDob: String = String()}
で、私のViewControllerに私は、これらの変数
var client: [Client] = []
var eName: String = String() //element Name
var cName: String = String()
var cProfession: String = String()
var cDOB: String = String()
先行である私の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
}
}
私は見つけましたpersonの開始タグと終了タグを送信しますが、一度それがfoundCharactersに到達すると空に戻ります。私はここで何か見落としていますか?
これを試してみるとよいでしょう:https://github.com/tadija/AEXML – Daniel
これはあなたのシナリオで役立つかもしれません:https://stackoverflow.com/questions/29937456/swift-parsing-attribute-name -for-given-elementname – Amit
これらの例はすべてNSXMLの関数と呼び出しを使用していますが、NSを削除できる必要があり、正しく機能するはずであると理解していますが、鉱山は進歩なしに設定してください。 – Kolorbox