Parsing XML in objective-c

Discussion in 'Mac Programming' started by Monkaaay, Oct 20, 2006.

  1. macrumors 6502

    Monkaaay

    Joined:
    Jun 19, 2006
    Location:
    Richmond, VA
    #1
    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.
     
  2. macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #2
    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.
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
  4. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #4
    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];
            }
    
        }
    
    }
    
     
  5. thread starter macrumors 6502

    Monkaaay

    Joined:
    Jun 19, 2006
    Location:
    Richmond, VA
    #5
    Ok, so using these two links:

    http://developer.apple.com/documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingParser.html
    http://developer.apple.com/document...nts.html#//apple_ref/doc/uid/20002265-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;
        }
    }
    
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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 :)
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    So you want to parse RSS? Why not use the free, open sourced, BSD licensed class that powers NetNewsWire? You can get it here.
     
  8. thread starter macrumors 6502

    Monkaaay

    Joined:
    Jun 19, 2006
    Location:
    Richmond, VA
    #8
    I'm definitely going to look into that today. Thank you!
     
  9. macrumors newbie

    Joined:
    Jul 9, 2008
    #9
    Check out MWFeedParser, it's a really clean and easy to use RSS/Atom Parser for iPhone. Hopefully it might be of some use!
     
  10. macrumors newbie

    Joined:
    May 20, 2010
    #10
    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/cocoa-tutorial-libxml-and-xmlreader/

    libxml2 comes with every mac preinstalled.

    Cheers
    Thomas
     
  11. macrumors newbie

    piyushseem

    Joined:
    Oct 6, 2010
    Location:
    India
    #11
    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 !!! :)
     

Share This Page