• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Parsing XML
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parsing XML


  • Subject: Re: Parsing XML
  • From: Mark Dawson <email@hidden>
  • Date: Fri, 1 Apr 2005 20:09:20 -0800

I've saved out my document as an XML file correctly, as far as I can tell (although the error, list below happens, it SEEMs like a well-formed line) I'm having problems reading it back in, though. The problem has to do with a saved array of objects. The objects "write" themselves out correctly, by implementing the "- (NSString *)description" method (returning "{1, 1}" type values-- #, #). However, I'm not sure how to read the array back in.

The code I started with does:

- (NSDictionary *)myDocumentDictionaryFromData:(NSData *)data {
NSString *string = [[NSString allocWithZone:[self zone]] initWithData:data encoding:NSASCIIStringEncoding];
NSDictionary *doc = [string propertyList];


the [string propertyList]; line is the one that errors with the array descriptors (in the console, I get a "XML parser error:
Unexpected character { at line 1
Old-style plist parser error:
Expected terminating ')' for array at line 19" for the line:
Points = ({199, 148}, {90, 219}, {196, 273}, {270, 222});


What I don't know how to do is tell the parser how to deal with the key "Points". I've written a parser for the object (converting the {199, 148} back into an NSPoint), but I don't know how to get it invoked…

Thanks for any suggestions,

That data is not property list compatible because of the curly braces. You need to wrap each point in quotes:


Points = ("{199, 148}", "{90, 219}", "{196, 273}", "{270, 222}");
That worked! I now have an array of strings like ' "{199, 148}" (quotes in the strings).

and then parse each string when you load it back in.
That makes sense.

Alternatively you could use custom XML instead of property lists and use CF/NSXMLParser to read it in. (The NS version is only available on Panther and later.) Then you can have XML like:


	<PARENT_OBJ>
		<POINT X="199" Y="148" />
		<POINT X="90" Y="219" />
		<POINT X="196" Y="273" />
		<POINT X="270" Y="222" />
	</PARENT_OBJ>

and the parser does all of the parsing for you (oddly enough). You don't have to tweeze out the curly braces and the commas, worry about whitespace if someone hand tweaks the plist file, etc.

That might be a future direction.  Thanks!

Mark

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Parsing XML (From: Mark Dawson <email@hidden>)
 >Re: Parsing XML (From: Frank Midgley <email@hidden>)

  • Prev by Date: Re: Parsing XML
  • Next by Date: Progress Sheet
  • Previous by thread: Re: Parsing XML
  • Next by thread: Progress Sheet
  • Index(es):
    • Date
    • Thread