1

I'm working on an iPad project that is using functions in a WebService. Handling webservice connection, data etc works find. But i'm not able to parse the result SOAP using TouchXML. Getting the nodes out of the xml always returns 0 length.

The output xml:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<soap:Body>
<LogOnResponse xmlns="http://coreservices.org/v1">
<LogOnResult>
<Id>1c0e9ad0-a3be-4cd0-8f0d-0616a63a4e28</Id>
<userId>2</userId>
<user>
<ID>2
<Name>Beheerder
<emailAddress />
<cultureName>nl-NL</cultureName>
</user>
</LogOnResult>
</LogOnResponse>
</soap:Body>
</soap:Envelope>

parser code:


 NSData *aData = [[NSData alloc] initWithBytes:[webData mutableBytes]
length:[webData length]]; NSString *xmlData = [[NSString alloc] initWithData:aData
encoding:NSASCIIStringEncoding]; NSLog(@"%@", xmlData); [xmlData release]; CXMLDocument *domUserIdentity = [[[CXMLDocument alloc] initWithData:aData
options:0
error:nil]
autorelease]; [aData release]; NSArray *nodesList = [domUserIdentity nodesForXPath:@"//LogOnResult" error:nil]; // 0 length for (CXMLElement *resultElement in nodesList) { for (int counter = 0; counter NSString *elemName = [[resultElement childAtIndex:counter] name]; NSString * elemValue = [[[resultElement childAtIndex:counter]
stringValue]
stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]]; [elemName release]; [elemValue release]; } } [nodesList release];

Any idea what did i do wrong?

Thank's alot in advance.

Inoel

Aurum Aquila
  • 9,126
  • 4
  • 25
  • 24
Inoel
  • 51
  • 4
  • 11

2 Answers2

1

Try using the NSXMLDocument, it should work as well as CXMLDocument. Here are some docs: link

Max
  • 16,679
  • 4
  • 44
  • 57
  • NSXMLDocument is a cocoa specific thing right? i don't think i can use this with iOS SDK. I can't find any library nor framework to use this. – Inoel Jan 25 '11 at 15:05
  • 1
    I realize it's late, but the KissXML library (http://code.google.com/p/kissxml/) duplicates the NSXMLDocument classes in iOS (plus adds some features). I'm not associated with the library, but find it very helpful in lots of my own code! – Jeff Hay May 07 '11 at 23:50
1

I recommend using an XPath Parser. You aren't doing anything too heavy, so the speed hit is well worth it. My personal choice is TFHpple. There are slightly faster and more precise solutions out there, but I find TFHpple's simplicity hard to beat.

Example:

NSData *data = [[NSData alloc] initWithContentsOfFile:@"example.html"]; 
// Create parser xpathParser = [[TFHpple alloc] initWithXMLData:data];
NSArray *elements = [xpathParser search:@"//LogOnResult"];
TFHppleElement *element = [elements objectAtIndex:0];
// Get the text within the cell tag NSString *content = [element content];
[xpathParser release]; [data release];

Obviously you'd have to write some code to save the parts you want. Looking at this data, I'd probably make a custom data holder class that you can access with properties. You can parse your XML into that and then save it as a property in the view controller that will be accessing it.

Happy coding!

Aurum Aquila
  • 9,126
  • 4
  • 25
  • 24
  • TFHpple? is this new kind of xml parser? how reliable is this? That's what i'm trying todo with TouchXML. xpath, but it doesn't give the result i wanted. :( – Inoel Jan 25 '11 at 15:08
  • TouchXML is a pain in the ass to set up. That's why I use TFHpple. It uses the same Xpath expressions, but it's much easier to get setup. The readme on the GitHub page is literally all you'll ever need. – Aurum Aquila Jan 25 '11 at 15:20