I am beginner of xcode , have some experience of other program language.
I would like to know how to load xml .
I made following class to load xml
class hoge : NSObject, NSXMLParserDelegate
{
init() {
super.init()
let url = NSURL(fileURLWithPath: "roper1.xml" )
let xmlParser = NSXMLParser( contentsOfURL: url )
if xmlParser != nil {
xmlParser!.delegate = self;
xmlParser!.parse()
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
print("Element's name is \(elementName)")
}
}roper1.xml
<?xml version="1.0" ?>
<data>
<m n="idle" f="12" l="1">
<f p="pt1" c="1" fp="0" ud="0" r="0" a="1.0" lt="0" lc="0" ax="0" ay="0" se="0" />
<f p="pt2" c="1" fp="0" ud="0" r="0" a="1.0" lt="0" lc="0" ax="0" ay="0" se="0" />
<f p="pt1" c="1" fp="1" ud="0" r="0" a="1.0" lt="0" lc="0" ax="1" ay="0" se="0" />
<f p="pt2" c="1" fp="1" ud="0" r="0" a="1.0" lt="0" lc="0" ax="1" ay="0" se="0" />
</m>
</data>I added one xml file named [roper1.xml] to the project.
However no elementName was printed .
I am wondering even NSURL works fine to load xml file or not.
print( url ) returns [ url roper1.xml -- file:/// ] but when I set a wrong file name that is not exist, result is same. no error occur.
Could you advice any hint How to confirm NSURL works fine and why parser function won't work?
It looks right to me. I'm doing something similar except I initialize it with NSData instead of contentsOfFile and it works.
Have you confirmed that the parser is not nil after you initialize it? Also implement parser:parseErrorOccured to see if any errors are being generated.