Re: Parsing XML Files Consisting of a Sequence of Top Level Elements
/* 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 t...@verizon.net 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 (Cocoa-dev@lists.apple.com) 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 k...@highrolls.net ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com
Re: Parsing XML Files Consisting of a Sequence of Top Level Elements
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 t...@verizon.net 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 (Cocoa-dev@lists.apple.com) 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 k...@highrolls.net ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com
Parsing XML Files Consisting of a Sequence of Top Level Elements
I am using NSXMLDocument to parse XML files. Some of the files are not legal XML because they contain a sequence of top level elements (legal XML must have a unique root element). Currently I handle this issue by programmatically adding a root element to surround the entire file before parsing. This seems ugly and heavy-handed. Is there a way to easily parse an XML file consisting of a sequence of top level elements? I don't actually need the NSXMLDocument object for anything. If there were an easy way to parse the files to an array of NSXMLElements it would be fine. Thanks, Tom Wetmore ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com
Re: Parsing XML Files Consisting of a Sequence of Top Level Elements
On 10/21/12 12:50 PM, Thomas Wetmore wrote: I am using NSXMLDocument to parse XML files. Some of the files are not legal XML because they contain a sequence of top level elements (legal XML must have a unique root element). Currently I handle this issue by programmatically adding a root element to surround the entire file before parsing. This seems ugly and heavy-handed. Is there a way to easily parse an XML file consisting of a sequence of top level elements? I don't actually need the NSXMLDocument object for anything. If there were an easy way to parse the files to an array of NSXMLElements it would be fine. If you find one, I'd be interested to hear about it. I had the same problem and solved it the same way. Regards Markus -- __ Markus Spoettl ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com
Re: Parsing XML Files Consisting of a Sequence of Top Level Elements
Doing it programmatically is trivial so I'll stick with it. Tom Wetmore, CBW, DeadEnds Software On Oct 21, 2012, at 6:50 AM, Thomas Wetmore wrote: I am using NSXMLDocument to parse XML files. Some of the files are not legal XML because they contain a sequence of top level elements (legal XML must have a unique root element). Currently I handle this issue by programmatically adding a root element to surround the entire file before parsing. This seems ugly and heavy-handed. Is there a way to easily parse an XML file consisting of a sequence of top level elements? I don't actually need the NSXMLDocument object for anything. If there were an easy way to parse the files to an array of NSXMLElements it would be fine. Thanks, Tom Wetmore ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com
Re: Parsing XML Files Consisting of a Sequence of Top Level Elements
On 21/10/2012, at 9:50 PM, Thomas Wetmore t...@verizon.net 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 (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com