Lucrul cu DDXMLElement si DDXMLNode – Swift

Se da urmatorul XML generat:


Vreau sa scot din XML nodul "message": "bubu"
1. do {
completeMessage = try element.nodes(forXPath: "body")
} catch _ {
completeMessage = [DDXMLNode]()

In acest moment completeMessage este sub forma vectoriala:
completeMessage - [{"message":"bubu","name":"tester","contact_id":"7","to":"tester"}]
2. let mesg = (completeMessage.first)?.child(at: 0)
let finalMessage = ((mesg?.stringValue)?.convertToDictionary())?["message"]

Extrag primul si singurul element din vector, il convertesc in dictionar si extrag campul “message”
finalMessage este in acest moment Optional(“bubu”) insa tipul este Any?
3. let bodyMessage = finalMessage as? String ?? ""
print("bodyMessage - \(bodyMessage)")

In acest moment am valoarea sub forma de string si anume “bubu”

Share Button


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.