2

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?

Massimo
  • 3,436
  • 4
  • 40
  • 68
  • you can refer the so answer http://stackoverflow.com/questions/6774600/ipad-xml-parsing-with-attributes-deal for parsing the attributes – Lochana Ragupathy Mar 18 '13 at 09:42
  • I don't want to parse the attributes of each element, I want to parse those of the tag , which normally is the first tag of the xml file... I see that this tag is not reported by NSXMLParser (neither the tag nor its attributes) – Massimo Mar 18 '13 at 10:14
  • 2
    The encoding should be irrelevant. NSXMLParser reads it and converts all XML data accordingly to NSString. For other tags like the version, you are probably out of luck. If you really need this information you might have to use a lower level XML library. – Martin R Mar 18 '13 at 10:22
  • I need those information because after parsed the file I might want to serialize it again on another xml file, and then I would like to rewrite also those information on the new xml file... – Massimo Mar 18 '13 at 10:25
  • 1
    @Massimo: So if your input XML file uses ISO-8859-1, then you want to create a new XML file using the same encoding? Or does your created XML file always use UTF-8 (which is the best way)? – Martin R Mar 18 '13 at 10:54

1 Answers1

2

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.

gaige
  • 17,263
  • 6
  • 57
  • 68