Laurent-- Sorry to be pesty about this XML thing, but I have Objective-C like this: NSString* xml = @"<people><person><name>steve</name><address>123 main</address></person><person><name>bob</name><address>345 First</address></person></people>"; NSError* error = [NSError alloc]; NSXMLDocument* xmlDoc = [[NSXMLDocument alloc] initWithXMLString:xml options:NSXMLDocumentTidyXML error:&error]; NSLog(@"Created XML Document %@", xmlDoc); NSLog(@"Root element is %@", [xmlDoc rootElement]); NSArray* nodes = [xmlDoc nodesForXPath:@"//person" error:&error]; NSLog(@"nodes are %@", nodes); for(int i = 0; i < [nodes count]; i++) { NSXMLElement* node = [nodes objectAtIndex:i]; NSLog(@"Node %d is %@", i, node); NSArray *name = [[node nodesForXPath:@"//name" error:&error] objectAtIndex:0]; NSArray *address = [[node nodesForXPath:@"//address" error:&error] objectAtIndex:0]; NSLog(@"Name: %@", [name stringValue]); NSLog(@"Address: %@", [address stringValue]); } } Which behaves exactly as one might expect. It creates an XML document on which I can apply XPath queries. Using the nightly build from 5-December, the following MacRuby is different: error = Pointer.new_with_type("@") s = NSMutableString.new("<people><person><name>steve</name><address>123 main</address></person><person><name>bob</name><address>345 First</address></person></people>") xmlDoc = NSXMLDocument.alloc.initWithData(s, options:1 << 10, error:error) <== Fails NSLog("Created XML Document %@", xmlDoc) NSLog("Root element is %@", xmlDoc.rootElement) This produces the output: initializing <people><person><name>steve</name><address>123 main</address></person><person><name>bob</name><address>345 First</address></person></people> 2009-12-06 12:39:50.318 macruby[5229:903] Created XML Document 2009-12-06 12:39:50.321 macruby[5229:903] Root element is (null) Note the following: * initWithString is simply unrecognized as a method * NSXMLDocumentTidyXML constant is not defined so I just transcribed the equivalent bitshift * The resultant XML document is null I'm not filing this as a bug because I think it might be a failure on my part to understand how this set of methods maps onto MacRuby. Thanks, Steve On Dec 5, 2009, at 4:22 PM, Laurent Sansonetti wrote:
Hi Steve,
On Dec 5, 2009, at 1:45 PM, s.ross wrote:
My code receives XML data from a Web Service API call that is in UTF8 encoding. This winds up in a string.
return_data = NSURLConnection.sendSynchronousRequest(@request, returningResponse: response, error: error) str = NSString.alloc.initWithData(return_data, encoding: NSUTF8StringEncoding) puts "******* response encoding it #{str.encoding}"
The result of the puts above is 'MACINTOSH'.
I suspect the encoding of the string is not UTF-8, because when I try to parse the XML using REXML, I get:
RegexpError: too short multibyte code
This occurs way in REXML:
/Library/Frameworks/MacRuby.framework/Versions/0.5/usr/lib/ruby/1.9.0/rexml/text.rb:132:in `check:'
In any case, my questions are:
1) If anyone has run across this what did you do?
I don't believe REXML works. In any case, I would recommend to not use it. Since you're already using Cocoa, why not giving NSXMLDocument a try?
2) Why might the encoding be MACINTOSH and not UTF-8, as specified in the initWithData method call?
#encoding returns the fastest encoding available for the receiver. You may specify UTF-8 during the string creation, but if Cocoa can pick a smaller encoding at runtime (like ASCII) it will.
This is different from the Ruby 1.9 semantics and we have a plan to fix that in 0.6.
3) Suggestions?
See my comment in 1) :)
Laurent _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel