4

iOS アプリケーションを開発していて、gdataxml を使用して xml を解析していますが、やり方が間違っています。nslog が null です

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

NSLog(@"%@",xmlResult.rootElement); 私のルート要素は完璧です、エラーは tempArray にあります

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

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

私の配列はnullです、

私のxmlは次のようなものです:

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

私の問題は名前空間にあると思いますが、どうすればいいですか?

ご回答有難うございます

4

1 に答える 1

9

GDataXMLNode でいくつかのテストを行った後、これが私の答えです。

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

このコメントは 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.

_def_nsを名前空間として実際に使用できると述べています。ただし、ドキュメントに他の名前空間がある場合は、独自の名前空間を設定することもできます。

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];
于 2012-02-23T11:04:45.350 に答える