Re: Parsing XML Files Consisting of a Sequence of Top Level Elements
Re: Parsing XML Files Consisting of a Sequence of Top Level Elements
- Subject: Re: Parsing XML Files Consisting of a Sequence of Top Level Elements
- From: Thomas Wetmore <email@hidden>
- Date: Mon, 22 Oct 2012 13:56:05 -0400
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@hidden> 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@hidden)
>>
>> 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:
>>
>> This email sent to email@hidden
>>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden