Skip to content

Instantly share code, notes, and snippets.

@Daij-Djan
Last active December 31, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daij-Djan/7978408 to your computer and use it in GitHub Desktop.
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)
#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