Thanks for the responses. In the past I have always used NSXMLParser to take
control of the structures built during parsing.
However, for the past couple years I have used the DOM approach with NSXMLNode
and its descendants. After some experimentation with very large XML files I
have found that the DOM approach, though more expensive in time and space, is,
in Apple's implementation, extremely fast, and with large RAMs and plenty of
virtual memory, XML files of many, many megabytes are easily handled. Note,
however, that I am exclusively a Mac OS X enterprise developer, not an iOS guy.
So I can afford to waste memory on a DOM.
The clincher is the availability of the "nodesForXPath:error:" method on the
NSXMLNode class. For my recent applications the convenience of this method has
outweighed any advantages that SAX-type parsing might bring to bear. I could
use NSXMLParser to build NSXMLNode-based DOM's, and then used the nodesForXPath
method, but that's a bit of a stretch.
In terms of the original question I posed here, the following code takes care
of my "problem":
// Create a file URL for the file and then create an XML document from that URL.
NSURL* url = [NSURL fileURLWithPath: path];
NSString* string = [NSString stringWithContentsOfURL: url encoding:
NSUTF8StringEncoding error: &error];
// Add a new root element to the string in case there isn't one.
string = [NSString stringWithFormat: @"<root>%@</root>", string];
// Create an XML document from the string.
NSXMLDocument* xmlDocument = [[NSXMLDocument alloc] initWithXMLString: string
options: 0 error: &error];
Many thanks for taking the time to think about this.
Tom Wetmore, CBW, DeadEnds Software
On Oct 22, 2012, at 1:02 PM, koko wrote:
> /*************************************************************************
> Called to parse an element. We make a selector from the element name and
> then if we respond to selector it is called from here. Selectors we respond
> to have an attribute dictionary
> *************************************************************************/
> - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
> namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
> attributes:(NSDictionary *)attributeDict
> {
> m_selString = [NSString stringWithFormat:@"%@:",elementName];
>
> SEL selector = NSSelectorFromString(m_selString);
>
> if([self respondsToSelector:selector])
> [self performSelector:selector withObject:attributeDict];
> }
>
> On Oct 21, 2012, at 4:02 PM, Graham Cox wrote:
>
>>
>> On 21/10/2012, at 9:50 PM, Thomas Wetmore <[email protected]> wrote:
>>
>>> Is there a way to easily parse an XML file consisting of a sequence of top
>>> level elements?
>>
>>
>> What about NSXMLParser? This gives you finer-grained access to the XML
>> without expecting a specific structure (other than valid XML).
>>
>> --Graham
>>
>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list ([email protected])
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>> https://lists.apple.com/mailman/options/cocoa-dev/koko%40highrolls.net
>>
>> This email sent to [email protected]
>>
>
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]