Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 20, 2006, 04:46 PM   #1
Monkaaay
macrumors 6502
 
Join Date: Jun 2006
Location: Richmond, VA
Parsing XML in objective-c

Given an NSMutableData object full of data in XML format, how can I load an XML reader object and iterate through it?

In .NET I'd load an XmlDocument object and go from there. Can anyone help me understand the correct object in objective-c/cocoa? Thanks.
Monkaaay is offline   0 Reply With Quote
Old Oct 20, 2006, 04:53 PM   #2
mduser63
macrumors 68040
 
mduser63's Avatar
 
Join Date: Nov 2004
Location: Salt Lake City, UT
Send a message via AIM to mduser63
I'm interested in the answer as well. I likely have a project coming up that will make very heavy use of an XML database that I need to be able to read and write.
mduser63 is offline   0 Reply With Quote
Old Oct 20, 2006, 05:06 PM   #3
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Apple Documentation

Basically lookup NSXMLParser
robbieduncan is offline   0 Reply With Quote
Old Oct 20, 2006, 05:11 PM   #4
Sayer
macrumors 6502a
 
Sayer's Avatar
 
Join Date: Jan 2002
Location: Austin, TX
I don't know about straight-ObjC, but here is the Core Foundation equivalent:

Code:
NSData *data = [mURLHandle resourceData];	// if foreground, this will block 'til loaded.

CFXMLTreeRef    cfXMLTree;

cfXMLTree = CFXMLTreeCreateFromData(kCFAllocatorDefault, (CFDataRef)data, NULL, kCFXMLParserSkipWhitespace, kCFXMLNodeCurrentVersion);

{

    CFXMLTreeRef    xmlTreeNode;
    CFXMLNodeRef    xmlNode;
    int             childCount;
    int             index;
    CFXMLElementInfo    *docInfoPtr;
    NSDictionary*        attributes;
    NSString*            value;
    NSComparisonResult    checkAlert;

    // Get a count of the top level node’s children.
    childCount = CFTreeGetChildCount(cfXMLTree);

    // Print the data string for each top-level node.
    for (index = 0; index < childCount; index++) {

        xmlTreeNode = CFTreeGetChildAtIndex(cfXMLTree, index);
								
        xmlNode = CFXMLTreeGetNode(xmlTreeNode);
								
        value = (NSString*) CFXMLNodeGetString(xmlNode);
								
        checkAlert = [value caseInsensitiveCompare:gAlertXMLTagStr];
								
        if (checkAlert == NSOrderedSame) {
								
            docInfoPtr = (CFXMLElementInfo*)CFXMLNodeGetInfoPtr(xmlNode);

            attributes = (NSDictionary*)docInfoPtr->attributes;
								

            value = [attributes objectForKey:gAlertXMLAttrStr];
        }

    }

}
__________________
Obama is a true statesman whose experience as a state senator, half-term US Senator & guest lecturer in a Constitutional Law class has fully prepared him to take control of our nuclear arsenal.-Me
Sayer is offline   0 Reply With Quote
Old Oct 20, 2006, 06:19 PM   #5
Monkaaay
Thread Starter
macrumors 6502
 
Join Date: Jun 2006
Location: Richmond, VA
Ok, so using these two links:

http://developer.apple.com/documenta...ingParser.html
http://developer.apple.com/documenta...002265-1001887

plus some networking code I found previously, I'm able to parse an RSS feed. However, I can't seem to get the inner text of an element. Anyone have a clue?

Code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ( [elementName isEqualToString:@"title"])
    {
	NSLog(@"Title: %@", elementName);
        return;
    }
}
Monkaaay is offline   0 Reply With Quote
Old Oct 20, 2006, 08:08 PM   #6
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Eek!

If you're requiring 10.4 or greater, use NSXML and associated classes (NSXMLDocument, NSXMLNode, NSXMLElement, etc). Those are DOM based, which IMO, is far easier to work with than SAX (NSXMLParser).

If you need 10.3, or 10.2 and up, use XMLTree. I have a heavily modified version of XMLTree that supports entities and writing XML, and it works fabulously
kainjow is offline   0 Reply With Quote
Old Oct 21, 2006, 01:31 AM   #7
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
So you want to parse RSS? Why not use the free, open sourced, BSD licensed class that powers NetNewsWire? You can get it here.
robbieduncan is offline   0 Reply With Quote
Old Oct 21, 2006, 07:52 AM   #8
Monkaaay
Thread Starter
macrumors 6502
 
Join Date: Jun 2006
Location: Richmond, VA
I'm definitely going to look into that today. Thank you!
Monkaaay is offline   0 Reply With Quote
Old May 19, 2010, 04:52 PM   #9
Bisbo
macrumors newbie
 
Join Date: Jul 2008
Check out MWFeedParser, it's a really clean and easy to use RSS/Atom Parser for iPhone. Hopefully it might be of some use!
Bisbo is offline   0 Reply With Quote
Old May 20, 2010, 02:13 AM   #10
Praseodym
macrumors newbie
 
Join Date: May 2010
For speed use xmlTextReader

I also recently parsed a 4.1MB XML file with approx. 12000 entries. I first used NSXMLDocument to parse the entries into an array. It took 1.5s, which was too slow. Then I used the C-API libxml2 and from it the xmlTextReader and the parsing went down on the same file to 300ms.

dTrace can come in very handy if you want to benchmark specific functions of your code.

To get your feet wet with libxml2 there is this blogpost explaining how to use it: http://www.cimgf.com/2008/08/18/coco...and-xmlreader/

libxml2 comes with every mac preinstalled.

Cheers
Thomas
Praseodym is offline   0 Reply With Quote
Old Oct 6, 2010, 06:31 AM   #11
piyushseem
macrumors newbie
 
Join Date: Oct 2010
Location: India
How to write XML file by xcode

Hi to every one !!!

I wanna create a XML file programmatically for backup my core data. Please guide me or gives simple sample code for it.

Thanks in advance !!!
piyushseem is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
RSS parsing help (simple) johnnyfla123 iPhone/iPad Programming 12 Nov 29, 2013 10:16 AM
Parsing hidden WebView items. mycompuser iPhone/iPad Programming 1 Aug 2, 2013 12:20 AM
iOS app crashing while parsing XML in TBXML RookieAppler iPhone/iPad Programming 1 Jan 29, 2013 05:20 PM
parsing and displaying in tableview jennylcf iPhone/iPad Programming 2 Jan 28, 2013 06:31 AM
parsing XML in iphone? hajira iPhone 0 Sep 24, 2012 04:51 AM

Forum Jump

All times are GMT -5. The time now is 05:45 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC