使用 GDataXML 获取 XML 响应值

发布于 2024-12-25 10:21:57 字数 596 浏览 5 评论 0原文

在 HTTP Post 之后,我检索到如下所示的 xml 响应:

<result value="OK">
    <user id="1">
            <name>admin</name>
            <rank>0</rank>
            <picture_count>0</picture_count>
            <comment_count>0</comment_count>
            <has_profile>1</has_profile>
    </user>
</result>

我想提取用户 ID,但我不知道如何执行此操作。 我尝试使用 GDataXML 解析器,因为它已经集成在我的项目中,但是 我不知道如何获取 html 标签内的值。

我希望你能帮助我。如果 XMLParser 没有解决方案,您会推荐正则表达式吗?在这种情况下,我希望有一个正则表达式的解决方案,我对此不太擅长:)

提前致谢。

after a HTTP Post I retrieve an xml response like the following:

<result value="OK">
    <user id="1">
            <name>admin</name>
            <rank>0</rank>
            <picture_count>0</picture_count>
            <comment_count>0</comment_count>
            <has_profile>1</has_profile>
    </user>
</result>

I'd like to extract the user ID, but I don't know how to do this.
I tried with GDataXML Parser, because this is already integrated in my project, but
I don't know how to get a value inside a html tag.

I hope you can help me. If there is no solution with XMLParser, would you recommend regular expressions? In this case, I would appreciate a solution for the regex expression, I'm not very good at this :)

Thanks in advance.

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

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

发布评论

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

评论(1

野生奥特曼 2025-01-01 10:21:57

这是我用来检索用户属性的代码。有用。

NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];

NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData 
                                                           options:0 error:&error];

NSArray *userElements = [doc.rootElement elementsForName:@"user"];

for (GDataXMLElement *userEl in userElements) {

    // Attribute
    NSString *attribute = [(GDataXMLElement *) [userEl attributeForName:@"id"] stringValue];        
    NSLog(@"attribute for user is %@", attribute);

    // Name
    NSArray *names = [userEl elementsForName:@"name"];
    if (names.count > 0) {
        GDataXMLElement *name = (GDataXMLElement *) [names objectAtIndex:0];

        NSLog(@"name for user is %@", name.stringValue);
    }

    // Rank
    NSArray *ranks = [userEl elementsForName:@"rank"];
    if (ranks.count > 0) {
        GDataXMLElement *rank = (GDataXMLElement *) [ranks objectAtIndex:0];

        NSLog(@"rank for user is %@", rank.stringValue);
    }

    // Do the same for other tags...     
}

[doc release];
[xmlData release];

文件 test.xml 与您从 xml 响应中检索到的文件相同。所以你需要更改这段代码的第二行。也许你可以调用类NSDatainitWithData方法。

您可以将此代码放在您想要的位置。也许您可以创建另一个类作为集中式解析器。希望它有帮助

编辑

将这两行放在前面以供说明。

NSString *valAttribute = [(GDataXMLElement *) [doc.rootElement attributeForName:@"value"] stringValue];        
NSLog(@"value attribute for result is %@", valAttribute);

解释很简单。使用 doc.rootElement,您可以检索整个 xml 文档,从

This is the code I use to retrieve the user attribute. It works.

NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];

NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData 
                                                           options:0 error:&error];

NSArray *userElements = [doc.rootElement elementsForName:@"user"];

for (GDataXMLElement *userEl in userElements) {

    // Attribute
    NSString *attribute = [(GDataXMLElement *) [userEl attributeForName:@"id"] stringValue];        
    NSLog(@"attribute for user is %@", attribute);

    // Name
    NSArray *names = [userEl elementsForName:@"name"];
    if (names.count > 0) {
        GDataXMLElement *name = (GDataXMLElement *) [names objectAtIndex:0];

        NSLog(@"name for user is %@", name.stringValue);
    }

    // Rank
    NSArray *ranks = [userEl elementsForName:@"rank"];
    if (ranks.count > 0) {
        GDataXMLElement *rank = (GDataXMLElement *) [ranks objectAtIndex:0];

        NSLog(@"rank for user is %@", rank.stringValue);
    }

    // Do the same for other tags...     
}

[doc release];
[xmlData release];

The file test.xml is the same that you retrieve from the xml response. So you need to change the second line of this snippet of code. Maybe you can call initWithData method of class NSData.

You can put this code where you want. Maybe you can create an other class that works as a centralized parser. Hope it helps

EDIT

Put these two lines before for instruction.

NSString *valAttribute = [(GDataXMLElement *) [doc.rootElement attributeForName:@"value"] stringValue];        
NSLog(@"value attribute for result is %@", valAttribute);

The explanation is quite simple. With doc.rootElement you retrieve the entire xml document, from <result> to </result>

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