1

I am trying to convert a string I load from an xml file to a float so i can set a slider value to 86.

When I use this code, it prints 0.0 ? but it has to be 86 !

Does somebody know why and how I can fix this ?

    var strXMLData:String = ""
    var currentElement:String = ""
    var passData:Bool=false
    var passName:Bool=false
    var parser = NSXMLParser()
    var string = ""

     let urlToSend: NSURL = NSURL(string: url)!
            // Parse the XML
            parser = NSXMLParser(contentsOfURL: urlToSend)!
            parser.delegate = self

            let success:Bool = parser.parse()

            if success {
                print("parse success!")

                print(strXMLData)

                string = strXMLData

                var float = (string as NSString).floatValue
                VolumeSlider.setValue(float)
                print(float)



            } else {
                print("parse failure!")
            }




            if(WCSession.isSupported()){
                self.session = WCSession.defaultSession()
                self.session.delegate = self
                self.session.activateSession()

    }
    }




    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
        currentElement=elementName;
        if(elementName=="e2current")
        {
            if(elementName=="e2current"){
                passName=true;

            }
            passData=true;
        }

    }
    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        currentElement="";
        if(elementName=="e2current")
        {
            if(elementName=="e2current"){
                passName=false;
            }
            passData=false;
        }

    }

    func parser(parser: NSXMLParser, foundCharacters string: String) {
        if(passName){
            strXMLData=strXMLData+"\n\n"+string
        }

        if(passData)
        {
            print(string)
        }
    }

    func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
        NSLog("failure error: %@", parseError)
    }
9
  • @coder1000 where did you go ? Commented May 13, 2016 at 19:39
  • I am here. No worries. Sadly, I don't see what's the issue yet :/ Commented May 13, 2016 at 19:41
  • well oke, if you get a idea let me know ;) ! Commented May 13, 2016 at 19:44
  • Of Course ! I will. :) Commented May 13, 2016 at 19:46
  • I am confused with what you are actually trying to do. You get a URL response, and need to parse out a string value that represents a float value? So the string would say "86.0"? Commented May 13, 2016 at 19:47

3 Answers 3

1

I've used this solution earlier. Don't know why your code didn't work, but at least this get's the job done...only with several lines of code.

var textNumber = "123456.789"
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
let theNumber = numberFormatter.numberFromString(textNumber)
var floatValue = Double(theNumber!) //123456.789
Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure that the number your are receiving are not 0.0?
it is a good code, when you use it how you do. but keep in mind i have to go from string to float from the received string of the xml file
I use this to receive a number stored as a String. Then I use this code to convert it to a number. Do you know what value your received xml is?
1

After going through your parsing code, we can basically simplify it into the following:

Set passData to true when we are inside the element:

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
    if (elementName == "e2current")  {
        passData = true;
    }
}

Set passData to false when we are not inside the element:

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    if (elementName == "e2current") {
        passData = false;
    }
}

Save the value when we are inside the element

func parser(parser: NSXMLParser, foundCharacters string: String) {
    if (passData){
        strXMLData = string
    }
}

Note that when you start adding \n\n to the string, you won't be able to parse that value.

A more sensible implementation would probably be:

var elementStack: [String] = []

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
    elementStack.append(elementName)
}

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    elementStack.removeLast()
}

func parser(parser: NSXMLParser, foundCharacters string: String) {
    if (elementStack.last == "e2current") {
        strXMLData = string
    }
}

Comments

1

I got it working guys !

I had to change the value when I received it in the viewDidLoad but in the func parser(parser: NSXMLParser, foundCharacters string: String) {

thank you guys but i got it working know.

 func parser(parser: NSXMLParser, foundCharacters string: String) {
    if(passName){
        strXMLData=strXMLData+"\n\n"+string
    }

    if(passData)
    {
        print(string)

        let numberFormatter = NSNumberFormatter()
        numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
        let theNumber = numberFormatter.numberFromString(string)
        let floatValue = Float(theNumber!)

        print(floatValue)

        VolumeSlider.setValue(floatValue)

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.