MYUtilities / MYXMLUtils.m

Jens Alfke 87ce300 

Jens Alfke 5f25fbb 
Jens Alfke 87ce300 















































//
//  MYXMLUtils.m
//  MYUtilities
//
//  Created by Jens Alfke on 11/3/09.
//  Copyright 2009 Jens Alfke. All rights reserved.
//

#import "MYXMLUtils.h"

#import "Logging.h"


@implementation NSXMLElement (MYUtilities)


/* Follow an XPath, returning the result as a single string */
- (NSString*) my_stringAtXPath: (NSString*) xpath error: (NSError**)error
{
    NSArray *nodes = [self nodesForXPath: xpath error: error];
    if (!nodes) {
        // Can't avoid QName errors so don't report them
        if (!error || [[*error description] rangeOfString: @"can't resolve QName for"].length==0)
            Warn(@"XPath error for '%@': %@", xpath, (error ?*error :nil));
        return nil;
    }
    if( [nodes count] == 0 ) {
        if (error) *error = nil;
        return nil;
    }
    if( [nodes count] > 1 )
        Warn(@"stringAtXPath: Got %u results for '%@'", [nodes count],xpath);
    return [[nodes objectAtIndex: 0] stringValue];
}


/* Follow an XPath, returning the result as a single URL */
- (NSURL*) my_URLAtXPath: (NSString*) xpath error: (NSError**)error
{
    NSString *str = [self my_stringAtXPath: xpath error: error];
    if( ! str )
        return nil;
    NSURL *url = [NSURL URLWithString: str];
    if( ! url ) {
        Warn(@"Invalid URL <%@> for '%@'",str,xpath);
        if (error) *error = nil;
    }
    return url;
}


@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.