12

I am using TouchXml because of the given limitations of NSXML on the actual iPhone. Anyway, I'm just starting out with Objective-C, I come from a C# background, and felt like learning something new..anyhow.. here is my xml file...

<?xml version="1.0" encoding="utf-8"?>
<FundInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/webservices">
  <FundsReleaseDate>2009-02-11T00:00:00</FundsReleaseDate>
  <FundValue>7800</FundValue>
  <FundShares>1000</FundShares>
</FundInfo>

I'm trying to get 7800, i.e FundValue. Can someone point me in the correct direction, I am using the TouchXml CXMLDocument and myParser is of type CXMLDocument, I have tried

NSArray *nodes = [myParser nodesForXPath:@"//FundInfo" error:&err];

Basically nodes evaluates to nil

if ([nodes count] > 0 ) {
    amount = [nodes objectAtIndex:1];
}

UPDATE 1: I have abandoned parsing using XPath, and have replaced it with NSURLRequest, so now I have the entire XML in a string, but my rude introduction to objective-C continues...I just realized how spoiled I have become to the .NET BCL where things like Regex are so easily available.

UPDATE2: I've figured out how to use RegexKitLite for regex.

So the question now is, how do I get the "7800" from inside FundValue which is now one big string. I have confirmed I have it in my NSString by writing it using NSLog.

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
Ta01
  • 31,040
  • 13
  • 70
  • 99

3 Answers3

25

The problem is that your XML has a namespace, which means your XPath does not actually match. Unfortunately there is no default mapping, and XPath does not actually define a good way to handle this internally, so you can't fix it simply by changing the XPath. Instead you need to inform the interpreter how you want to map it in your XPath.

It looks like TouchXML implements support for this via:

- (NSArray *)nodesForXPath:(NSString *)xpath namespaceMappings:(NSDictionary *)inNamespaceMappings error:(NSError **)error;

So you can try something like:

NSDictionary *mappings = [NSDictionary dictionaryWithObject:@"http://tempuri.org/webservices" forKey:@"tempuri"];
[myParser nodesForXPath:@"//tempuri:FundInfo" namespaceMappings:mappings error:&err];
Louis Gerbarg
  • 43,356
  • 8
  • 80
  • 90
  • thanks for the reply, I'm getting a crash with the following exception message: [CXMLElement isEqualToString:]: unrecognized selector sent to instance – Ta01 Feb 09 '09 at 03:33
  • I bet this is all tied to the fact that the web service I'm trying to consume is .NET based, but unfortunately I have no control over, I've googled around and it seems that the xmlns attribute is the cause of all the problems. – Ta01 Feb 09 '09 at 13:53
  • I don't use TouchXML in any projects, though I do use libxml2 (which is what TouchXML is a wrapper for), so the underlying library does work. It would not shock me if this is not a well tested code path, you may need to fix something in TouchXML. – Louis Gerbarg Feb 09 '09 at 15:51
  • Thanks. exactly my problem, and this fixed it. – Greg Aug 18 '09 at 11:38
  • 1
    Thanks. I tried using this but I wasn't sure what the NSDictionary's key was for so I just entered anything. I kept using the same XPath though "/foo/bar" so it failed. I then realised that I had to use the key inside my XPath as you demonstrated (I didn't notice it at first), so I used "/ns:foo/ns:bar" where ns is the key I used, and it worked fine. Hope this helps. – aiham Apr 15 '11 at 03:52
0

I had a similar problem. ANY selection on an XML that has a namespace specified (xmlns="http://somenamespace") will result in no nodes found. Very strange considering it does support the additional namespace formats such as xmlns:somenamespace="http://somenamespace".

In any case, by far the easiest thing to do is do a string replace and replace xmlns="http://tempuri.org/webservices with an empty string.

Overall I like touchxml, but I can't believe this bug still exists.

-1

Try

NSArray *nodes = [myParser nodesForXPath:@"/FundInfo/*" error:&err];
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589