使用 gdata xml 解析带有名称空间的 xml

发布于 2025-01-01 06:15:39 字数 709 浏览 2 评论 0原文

我正在开发一个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);

我的数组为空,

我的 xml 是这样的:

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

我确信我的问题与命名空间有关,但我不知道该怎么做?

谢谢你的回答

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2025-01-08 06:15:39

使用 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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文