Become a MacRumors Supporter for $25/year with no ads, private forums, and more!

Monkaaay

macrumors 6502
Original poster
Jun 19, 2006
258
0
Richmond, VA
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.
 

mduser63

macrumors 68040
Nov 9, 2004
3,041
30
Salt Lake City, UT
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.
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
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];
        }

    }

}
 

Monkaaay

macrumors 6502
Original poster
Jun 19, 2006
258
0
Richmond, VA
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;
    }
}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
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 :)
 

Praseodym

macrumors newbie
May 20, 2010
1
0
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
 

piyushseem

macrumors newbie
Oct 6, 2010
1
0
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 !!! :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.