Re: Need sample code...XML parsing...
Re: Need sample code...XML parsing...
- Subject: Re: Need sample code...XML parsing...
- From: Matt Neuburg <email@hidden>
- Date: Tue, 17 Sep 2002 16:43:16 -0700
On Tue, 17 Sep 2002 16:05:31 -0500, Albert Atkinson
<email@hidden> said:
>
Here is what I need, I have an XML file at www.mysite.com/xmlfile.xml
>
that I need my app to download and parse.
>
>
Here is what it looks like:
>
>
<?xml version="1.0"?>
>
<mydata>
>
<name>Me!</name>
>
<email>My Email</email>
>
</mydata>
>
>
This is just a stripped down file but the real one contains actual
>
information. Now, I want to parse this data and insert it into a text
>
field. The data from "name" goes into NameTextField and the data from
>
"email" go into EmailTextField
This is a particularly simple situation provided that, as in your example,
the following facts are true: (1) You already know the structure of the
xml; (2) There are no duplications of tag names at the same level - this is
legal, obviously, and is one of the reasons you can't use any combination
of array and dictionary to read arbitrary xml into a higher-level
collection.
So, that being so, here's your code! At the end, you'll have a dictionary
whose @"name" key and @"email" key contain @"Me!" and @"My Email", bzw.
Since you know how to deal with a dictionary, the problem is solved.
// two XML parsing utilites
// primitive but good enough for our purposes!
// given a tree, look to see if it has a subtree that's an element node of
given name
// if so, return it; otherwise return nil
- (CFXMLTreeRef) diveIntoTree: (CFXMLTreeRef) tree elementName: (NSString*)
name {
CFXMLTreeRef subTree;
CFXMLNodeRef node;
CFXMLNodeTypeCode nodeType;
int i,u = CFTreeGetChildCount(tree);
for (i=0; i<u; i++) {
subTree = CFTreeGetChildAtIndex(tree, i);
node = CFXMLTreeGetNode(subTree);
nodeType = CFXMLNodeGetTypeCode(node);
if (nodeType == kCFXMLNodeTypeElement) {
CFStringRef str = CFXMLNodeGetString(node);
if ([(NSString*) str isEqualToString: name])
return subTree;
}
}
return nil;
}
// given a tree assumed to be have subtrees that are element nodes wrapping
text...
// return dictionary of element names and text
// of course this would break if two elements had the same name (legal in
xml)...
// but we aren't worried about that today
- (NSMutableDictionary*) buildDictFromTree: (CFXMLTreeRef) tree {
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithCapacity: 20];
CFXMLTreeRef subTree;
CFXMLNodeRef node;
CFXMLNodeTypeCode nodeType;
int i,u = CFTreeGetChildCount(tree);
for (i=0; i<u; i++) {
subTree = CFTreeGetChildAtIndex(tree, i);
node = CFXMLTreeGetNode(subTree);
nodeType = CFXMLNodeGetTypeCode(node);
if (nodeType == kCFXMLNodeTypeElement) {
CFStringRef str = CFXMLNodeGetString(node);
//NSLog((NSString*)str);
int ii, uu = CFTreeGetChildCount(subTree);
for (ii = 0; ii<uu; ii++) {
CFXMLTreeRef subSubTree = CFTreeGetChildAtIndex(subTree, ii);
CFXMLNodeRef subNode = CFXMLTreeGetNode(subSubTree);
CFXMLNodeTypeCode subNodeType = CFXMLNodeGetTypeCode(subNode);
if (subNodeType == kCFXMLNodeTypeText) {
CFStringRef str2 = CFXMLNodeGetString(subNode);
//NSLog((NSString*)str2);
[dict setObject: (NSString*) str2 forKey: (NSString*) str];
}
}
}
}
return dict;
}
// and now, how to call them!
NSData* myxml = /* YOUR DATA GOES HERE */
CFXMLTreeRef tree =
CFXMLTreeCreateFromData(NULL,
(CFDataRef) [myxml dataUsingEncoding:NSUTF8StringEncoding],
NULL, kCFXMLParserSkipWhitespace, kCFXMLNodeCurrentVersion);
CFXMLTreeRef nomad;
NSMutableDictionary* dict=nil;
nomad = [self diveIntoTree: tree elementName: @"mydata"];
if (nomad)
dict = [self buildDictFromTree: nomad];
m.
--
matt neuburg, phd = email@hidden,
http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart.
http://www.tidbits.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.