Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > XML Parsing

XML Parsing
Thread Tools
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Oct 14, 2003, 01:13 AM
 
Ok, I have searched and searched, and I have been completely unsuccessful at finding some kind of instructions for using the XML-Parser I KNOW is part of OS X. I know it's part of CoreFoundation, but i can't figure out how to use it...
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
Dedicated MacNNer
Join Date: May 2003
Location: Atlanta, GA
Status: Offline
Reply With Quote
Oct 14, 2003, 09:38 AM
 
Originally posted by cheerios:
Ok, I have searched and searched, and I have been completely unsuccessful at finding some kind of instructions for using the XML-Parser I KNOW is part of OS X. I know it's part of CoreFoundation, but i can't figure out how to use it...
Here's an example that might help:

NSData *xmlData;
NSMutableDictionary *myDict;

//add some data to the dictionary

xmlData = [NSPropertyListSerialization dataFromPropertyList:myDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

[xmlData writeToFile:[[NSBundle mainBundle] pathForResource:@"my" ofType:@"plist"] atomically:YES];

----

The above example would write the contents of myDict out to an xml file (named "my.plist"). To read back the data in said file, use something like:

NSMutableDictionary *myDict;

myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"my" ofType:@"plist"]];

As long as your dictionary is properly formatted; this should work perfectly!
Alex

G7 Software: home Tetrinet Aqua
-----
"Utopia" 1Ghz TiBook SuperDrive w/ 1Gb RAM.
     
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Oct 14, 2003, 01:08 PM
 
That looks like it's close to what I'm searchin' for. What I'm doin' now is making a string w/ init with contents of file, and makingranges, and walking throguh the file character by character until I find the open and close tags. *eek*

Problem being: NSDictionary's are HashTables (to borrow terminology i'm more familiar with). so... what does it do when I have 2 tags w/ the same name? that's perfectly legal XML, but you're not allowed to have 2 identical keys in a HashTable...

edit: to clarify, I don't need to write XML files, just read em, and they'll look something like this:
Code:
<?xml version="1.0" encoding="ISO8859-1" ?> <dataroot> <fsm> <description>finite state machine that only accepts strings that do not end with ab</description> <alphabet>ab</alphabet> <acceptstates> <state>0</state><state>1</state> </acceptstates> <nextstatetable> <tablerow> <state>1</state><state>0</state> </tablerow> <tablerow> <state>0</state><state>2</state> </tablerow> <tablerow> <state>1</state><state>0</state> </tablerow> </nextstatetable> </fsm> </dataroot>
so those state tags... are they gonna be a problem?
(Last edited by cheerios; Oct 14, 2003 at 01:23 PM. )
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Oct 14, 2003, 01:43 PM
 
Are you looking for something like this?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Oct 14, 2003, 06:07 PM
 
been there, seen that. problem is, they have 1 example, and no documentation about what CFURLCreateDataAndPropertiesFromResource() and CFXMLTreeCreateFromData() take as arguments. This is what pisses me off about apple's documentation! what KIND of URL does the function want? I tried giving it an NSString I tried giving it a c-string, I tried giving it an NSURL... it wants a URL. wtf????
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Oct 14, 2003, 07:38 PM
 
Originally posted by cheerios:
been there, seen that. problem is, they have 1 example, and no documentation about what CFURLCreateDataAndPropertiesFromResource() and CFXMLTreeCreateFromData() take as arguments. This is what pisses me off about apple's documentation! what KIND of URL does the function want? I tried giving it an NSString I tried giving it a c-string, I tried giving it an NSURL... it wants a URL. wtf????
From a look at the API listing (available on your hard drive here if you've got the dev tools installed), it appears to want a CFURL.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Oct 14, 2003, 07:45 PM
 
yeah, i finally came to that conclusion... *blush* so... current problem involves the fact I can't call CFTree functions on a CFXMLTree ( which Apple does in it's source code. I dunno if I'm doin' something stupid, or what, but it Signal 10 error's every time i call a CFTree function. code & output follow...

Code:
#import "xml.h" @implementation xml +(void)parseXML: (NSString *)path { NSLog(@"I am here!"); CFXMLTreeRef cfXMLTree; CFDataRef xmlData; CFURLRef pathURL = CFURLCreateFromFileSystemRepresentation ( kCFAllocatorDefault, [path cString], [path cStringLength], NO); // Load the XML data using its URL. NSLog(@"about to load the XML file"); CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, pathURL, &xmlData, NULL, NULL, NULL); // Parse the XML and get the CFXMLTree. NSLog(@"loaded XML, about to parse it!"); cfXMLTree = CFXMLTreeCreateFromData(kCFAllocatorDefault, xmlData, pathURL, kCFXMLParserSkipWhitespace, kCFXMLNodeCurrentVersion); NSLog(@"finished parsing"); //int childCount = CFTreeGetChildCount(cfXMLTree); NSLog(@"childcount"); int index; CFXMLTreeRef xmlTreeNode; CFXMLNodeRef xmlNode; NSLog(@"about to for-loop"); for (index = 0; index < 2; index++) { NSLog(@"just about to call CFTree"); xmlTreeNode = CFTreeGetChildAtIndex(cfXMLTree, index); NSLog(@"called CFTree, calling CFXML"); xmlNode = CFXMLTreeGetNode(xmlTreeNode); CFShow(CFXMLNodeGetString(xmlNode)); //NSLog("%s\n",CFXMLNodeGetString(xmlNode)); } } @end

2003-10-14 17:28:24.790 experiment[13414] I am here!
2003-10-14 17:28:24.799 experiment[13414] about to load the XML file
2003-10-14 17:28:24.807 experiment[13414] loaded XML, about to parse it!
2003-10-14 17:28:24.816 experiment[13414] finished parsing
2003-10-14 17:28:24.824 experiment[13414] childcount
2003-10-14 17:28:24.832 experiment[13414] about to for-loop
2003-10-14 17:28:24.840 experiment[13414] just about to call CFTree

experiment has exited due to signal 10 (SIGBUS).
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
Forum Regular
Join Date: Apr 2003
Status: Offline
Reply With Quote
Oct 14, 2003, 11:17 PM
 
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Oct 15, 2003, 12:26 AM
 
Originally posted by cheerios:
yeah, i finally came to that conclusion... *blush* so... current problem involves the fact I can't call CFTree functions on a CFXMLTree ( which Apple does in it's source code.
I have a very strong feeling you're getting a NULL somewhere in there -- quite possibly right at the beginning with the CFURL. Try putting tests for NULL after your creation functions and you'll probably find the culprit.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Fresh-Faced Recruit
Join Date: Apr 2001
Status: Offline
Reply With Quote
Oct 16, 2003, 03:05 PM
 
     
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Oct 21, 2003, 01:19 AM
 
thanks all... finally got it worked out a couple hours ago (writing a parser in another language for another class took precidence for awhile!). problem: had an un-closed tag that was crashing the parsing function. KaBoom, that's all she wrote. BAD APPLE! NO ERROR MESSAGES! (done complaining now). that article posted by johnt519 was especially helpful <does a happy dance of joy>
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Oct 21, 2003, 02:59 PM
 
Is the NSPropertyListSerialization all I need to save my custom objects as readable XML?

Here is a sample of my custom object:

@interface Markets : NSObject {
NSString *marketName;
NSString *marketOverview;
NSMutableArray *positionersArray;
}

and the positionersArray contain another custom object

@interface Positioners : NSObject {

NSString *position;
NSMutableArray *chartersArray;

}

I have been searching for a simple way of
storing my custom objects as readable XML.

Key-Coding stores it in a XML but the file
that it creates is not readable.

Any help would be appreciated....




Originally posted by cheerios:
thanks all... finally got it worked out a couple hours ago (writing a parser in another language for another class took precidence for awhile!). problem: had an un-closed tag that was crashing the parsing function. KaBoom, that's all she wrote. BAD APPLE! NO ERROR MESSAGES! (done complaining now). that article posted by johnt519 was especially helpful <does a happy dance of joy>
     
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Oct 21, 2003, 05:30 PM
 
by readable, do you mean by a human? or by code?
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Oct 21, 2003, 05:46 PM
 
by human...

The reason being is that the XML file will also be placed in a database, FileMaker I think and the XML will undergo an XSLT to fit the FileMaker ER.

thanks.



Originally posted by cheerios:
by readable, do you mean by a human? or by code?
     
Forum Regular
Join Date: Oct 2001
Status: Offline
Reply With Quote
Oct 21, 2003, 07:03 PM
 
i think the easiest thing is to convert your custom objects to a representation made up of only NSDictionary, NSString, NSArray, etc. objects in order for NSPropertyListSerialization to work correctly. you would create a dictionary to represent your Markets object with keys to represent the instance variables marketName, marketOverview, and positionersArray. You would do the same for the Positioners object. The Markets dictionaries would be put into an NSArray and you would serialize that array with - (BOOL) writeToFile: (NSString *) path atomically: (BOOL) flag to get something like the following:

Code:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>marketName</key> <string></string> <key>marketOverview</key> <string></string> <key>positionersArray</key> <array> <dict> <key>chartersArray</key> <array> <string></string> </array> <key>position</key> <string></string> </dict> </array> </dict> </array> </plist>
that file could then be entered into your database and processed.

kido
     
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Oct 22, 2003, 10:37 AM
 
Thanks kido,

I guess I'll have to do some rework on my custom class objects.

Originally posted by kido:
i think the easiest thing is to convert your custom objects to a representation made up of only NSDictionary, NSString, NSArray, etc. objects in order for NSPropertyListSerialization to work correctly. you would create a dictionary to represent your Markets object with keys to represent the instance variables marketName, marketOverview, and positionersArray. You would do the same for the Positioners object. The Markets dictionaries would be put into an NSArray and you would serialize that array with - (BOOL) writeToFile: (NSString *) path atomically: (BOOL) flag to get something like the following:

Code:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>marketName</key> <string></string> <key>marketOverview</key> <string></string> <key>positionersArray</key> <array> <dict> <key>chartersArray</key> <array> <string></string> </array> <key>position</key> <string></string> </dict> </array> </dict> </array> </plist>
that file could then be entered into your database and processed.

kido
     
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Nov 21, 2003, 11:32 AM
 
If I rework my classes as simply instances of
NSArrays and NSDictionaries, I then would have to rework the code where the controller adds the instances to the appropriate container correct as opposed to the classes themselves adding their related objects.

I guess I'm not clearly bridging how to create object representations from that shown in the tutorial books to representing them as NSArrays and NSDictionaries that add/delete/modify their related objects.

I would appreciate any clarification...

Do I make my classes as subclasses of NSArray, NSDictionary in order to write their data as human readable XML?

Thanks in advance...
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2