views:

744

answers:

1

Hi all,

I am stuck with some TouchXML code. Please help.

I have the following code to get the data from an xml webservice:

NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


NSString *data = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];

NSLog(@"String data: %@ \n", data);

//Do the parsing

CXMLDocument *document = [[[CXMLDocument alloc] initWithData:urlData encoding:NSUTF8StringEncoding options:0 error:&error] autorelease];  
NSLog (@"Document  :%@ \n",[document stringValue]);

The string data does have the content from the service, but how come the CXMLDocument object does not contain anything? Someone can tell me why?

2009-12-30 18:21:59.467 MyAdvancedBlog[3425:207] String data: <?xml version="1.0" encoding="utf-8"?>

<Post xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"&gt;
  <IdPostazione>42</IdPostazione>
  <StringID>HOANG</StringID>
  <Name>CASSA2</Name>
  <TerminalValid>true</TerminalValid>
  <NeedSession>false</NeedSession>
</Post>   

2009-12-30 18:21:59.469 MyAdvancedBlog[3425:207] Document  :(null) 
+2  A: 

TouchXML's documentation says that CXMLDocument should act just like NSXMLDocument. So, the reference for initWithData:options:error: might help.

It says the result will be nil if it's unsuccessful, and error will then contain more info. Is it nil?

You might consider using NSXMLDocument for the moment, and see if they really do act the same. If they don't, file a bug with TouchXML.

You could also use initWithXMLString:options:error: along with that string you already decoded.

Edit: Even better. Here's example code for using NSXMLDocument. In theory, it should work for CXMLDocument as well.

Kevin Conner
Hi Kevin, NXMLDocument is not available in iPhoneOS as it says in the documentation: The Foundation framework provides support for XML parsing through the NSXMLParser class. However, other XML parsing classes (including NSXMLDocument, NSXMLNode, and NSXMLElement) are not available in iPhone OS. In addition to the NSXMLParser class, you can also use the libXML2 library, which provides a C-based XML parsing interface.
sfa
I have already tried initWithXMLString but with no luck, it gives me the same nil object.
sfa