-1

I'm working on an app in Swift 3.0 that parses an XML file and displays some of the attributes via labels. Most of it is working properly, except for when I try to parse the part of the XML that contains a <link href = "..."/> tag. When I attempt to do this, the app crashes and I get a "Fatal error: index out of range" message. Here's the XML I'm using: https://alerts.weather.gov/cap/fl.php?x=0 And here's my code:

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
    if(elementName == "cap:event") {
        titleFound = true
    }
    if(elementName == "cap:expires") {
        descFound = true
    }
    if(elementName == "cap:severity") {
        sevFound = true
    }
    if(elementName == "summary") {
        summFound = true
    }
    if(elementName == "cap:effective") {
        effFound = true
    }
    if(elementName == "cap:urgency") {
        urgFound = true
    }
    if(elementName == "cap:certainty") {
        certFound = true
    }
    if(elementName == "link") {
        linkFound = true
    }
}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    if(elementName == "cap:event") {
        titleFound = false
    }
    if(elementName == "cap:expires") {
        descFound = false
    }
    if(elementName == "cap:severity") {
        sevFound = false
    }
    if(elementName == "summary") {
        summFound = false
    }
    if(elementName == "cap:effective") {
        effFound = false
    }
    if(elementName == "cap:urgency") {
        urgFound = false
    }
    if(elementName == "cap:certainty") {
        certFound = false
    }
    if(elementName == "link") {
        linkFound = false
    }
}

The element I want is the link href tag from inside the <entry> tag. I'm not sure how to go about properly parsing this part since it's not formatted like the other tags (<cap:event>, <cap:severity>, etc.) Can anyone shed some light on this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

1

In your didStartElement method, you can access that attribute through the parameter attributeDict which should return ["href":"www.whatever.com"]

Found the answer here

Is this for Tanksale's CS320 Midterm? Regardless, good luck!

Community
  • 1
  • 1
Rachel Harvey
  • 1,719
  • 2
  • 15
  • 23
0

This is a work around but check the xml doc the id tag contains the url as well.

I was having the same problem. Chirp Chirp!