Last active
December 31, 2015 11:18
-
-
Save Daij-Djan/7978408 to your computer and use it in GitHub Desktop.
gets the root node's name of a given document. Nice for quickly checking if a given XML document at least starts with the content you expect! (I use it with NSOpenPanel on OSX as well as View Controllers opening user content on IOS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "XMLUtils.h" | |
#import <libxml/xmlreader.h> | |
@implementation XMLUtils | |
//... | |
+ (NSString*)rootNodeNameFromURL:(NSURL*)url { | |
NSString* obj = nil; | |
xmlTextReaderPtr reader = xmlReaderForFile( url.absoluteString.UTF8String, | |
NULL, | |
(XML_PARSE_NOBLANKS | XML_PARSE_NOCDATA | XML_PARSE_NOERROR | XML_PARSE_NOWARNING)); | |
if(reader != nil) { | |
int ret = xmlTextReaderRead(reader); | |
if(ret == 1) { | |
obj = [NSString stringWithCString:(const char*)xmlTextReaderConstLocalName(reader) | |
encoding:NSUTF8StringEncoding]; | |
} | |
xmlFreeTextReader(reader); | |
} | |
return obj; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment