I need to check if an element exists in my XML that's retrieved from calling a web service. I need to run a certain function if a particular element does not exist.
Here is what I have so far. This works if all of these elements are there. I need to make sure the "MembershipNumber" element exists.
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
attributes = attributeDict as NSDictionary
element = elementName
if ((elementName as NSString).isEqual(to: "MembershipInfo")) {
paidYear = ""
county = ""
name = ""
dueDateString = ""
membershipNumber = ""
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if element == "MembershipNumber" {
membershipNumber.append(string)
print("NUMBER: \(membershipNumber)")
}
if element == "County" {
let counties = Counties().counties
county.append(counties[string]!)
print("COUNTY: \(county)")
}
if element == "PaidYear" {
paidYear.append(string)
print("PAID YEAR: \(paidYear)")
}
if element == "DueDate" {
dueDateString.append(string)
print("Due Date: \(dueDateString)")
}
if element == "Name" {
if name == "" {
name.append(string)
print("NAME: \(name)")
}
}
}
Store the names of all elements inside
didStartElementin an array until this method calledthen check the array content for that element name