Search code examples
iosobjective-cxmlxml-parsingnsxmlparser

NSXMLParser, how to know what encoding and version are written in the <?xml> tag (if present)


As the title says, when I parse a file using the NSXMLParser parser, is there a way to know if the tag is present? And, if yes, how can I obtain the values of its attributes (if present)?

I tried all the methods of the NSXMLParserDelegate, but none of them solves my problem... Should I parse that tag manually?


Solution

  • Theoretically, you should receive a -foundProcessingInstructionWithTarget:data: delegate callback, but in practice that doesn't happen (at least on iOS, I didn't check OS X).

    If you want to definitively check, you're going to need to parse the beginning of the file yourself, looking for the XML directive. A short read and scan with NSScanner will probably be sufficient for that (by using NSScanner instead of a simple string test, you'll get better white space handling).

    Obviously, this isn't the answer you were originally looking for, but since the XML is going to have a declared character set anyway, you might want to just write it out with a utf-8 declaration and presume that anything which is reading your modified file (since you don't need to rewrite if it is identical), can also read the processing directive and handle the character set correctly.