4

i am developping an ios application and i am parsing my xml with gdataxml, but i am doing it wrong, my nslog is null

NSError *error = nil;
GDataXMLDocument *xmlResult = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
if (error) {
    NSLog(@"%@",error);
}

NSLog(@"%@",xmlResult.rootElement); my root element is perfect, the error is with tempArray

NSArray *tempArray = [xmlResult nodesForXPath:@"//message/error/value" error:&error];

NSLog(@"mon array %@",tempArray);

my array is null,

my xml is like this :

<message xmlns="http://.....Api" xmlns:i="http://www.w3.org/....">
<error i:nil="true"/>
<value>

i am sur that my problème is with the namespace, but i don't how to do it ?

thanks for your answer

samir
  • 4,501
  • 6
  • 49
  • 76
  • any idea why this did't work ? – samir Feb 01 '12 at 17:55
  • Have you already fixed the problem here? There is a bunch of things that I don't know about GDataXML so this is really interesting. – yoninja Feb 22 '12 at 16:46
  • hello samir, i am not able to get the elements from nodesForXPath , it returns an error like XPath error : Undefined namespace prefix xmlXPathEval: evaluation failed i need your help... stuck here from last 3 days and i dont want to used namespace paramerter so is it possible for me to do that ? – Apple Jun 21 '13 at 10:06

1 Answers1

9

After some testing with GDataXMLNode, here is my answer:

NSArray *tempArray = [xmlResult nodesForXPath:@"//_def_ns:message/_def_ns:error/_def_ns:value" error:&error];

You can see this comment in GDataXMLNode.h:

// This implementation of nodesForXPath registers namespaces only from the
// document's root node.  _def_ns may be used as a prefix for the default
// namespace, though there's no guarantee that the default namespace will
// be consistenly the same namespace in server responses.

It states that you can actually use _def_ns as your namespace. However, you can also set your own namespace in case there are other namespaces in your document.

NSDictionary *myNS = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"http://.....Api", @"ns1",
                      @"http://.....Other_Api", @"ns2", nil];
NSArray *tempArray = [xmlResult nodesForXPath:@"//ns1:message/ns1:error/ns1:value" namespaces:myNS error:&error];
yoninja
  • 1,952
  • 2
  • 31
  • 39
  • hello yoninja, i got error like XPath error : Undefined namespace prefix xmlXPathEval: evaluation failed. when i used nodesForXPath. – Apple Jun 21 '13 at 09:54
  • Hi @TapanNathvani, can you show me how you did it? Or maybe you can post a question regarding this. Thanks! – yoninja Jun 28 '13 at 05:30
  • hello yonija,.. i solved that issue. Thanks for your reply. Here is my question link. http://stackoverflow.com/questions/17232712/gdataxmldocument-nodesforxpath-return-an-error Thanks for your reply – Apple Jun 28 '13 at 05:33