Cannot load xml

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?

Answered by @ben in 67048022

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.

Accepted Answer

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.

I added one code after initialized.

print("parser", xmlParser?.description)


the result printed was

parser Optional("<NSXMLParser: 0x17651050>")


and I added following function

func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
   print("error")
}


"error" was not printed.

and any other errors are not shown.

I don't think the problem is with the parser code, that should work fine. Are you trying to include the XML file inside the compiled app? If so, make sure roper1.xml is in your project's Copy Bundle Resources build phase, then use NSBundle to retrieve your file and form the url, something like this:


guard let url = NSBundle.mainBundle().URLForResource("roper1", withExtension: "xml") else { return }
let xmlParser = NSXMLParser( contentsOfURL: url )

if xmlParser != nil {
  xmlParser!.delegate = self;
  xmlParser!.parse()
}

Yes! I could print elementName in console by changing the code.


//let url = NSURL(fileURLWithPath: "roper1.xml" )
guard let url = NSBundle.mainBundle().URLForResource("roper1", withExtension: "xml") else { return }


Nothing I have changed Copy Bundle Resources settings , but there is the idea that I dragged xml file into a atlas folder with other image files first, after that I dragged the xml file out from the folder. file reference may became something wrong .


Anyway I could find the clues, I really would like to say thank you.

Cannot load xml
 
 
Q