使用 GDataXML 获取 XML 响应值
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我用来检索用户属性的代码。有用。
文件 test.xml 与您从 xml 响应中检索到的文件相同。所以你需要更改这段代码的第二行。也许你可以调用类
NSData
的initWithData
方法。您可以将此代码放在您想要的位置。也许您可以创建另一个类作为集中式解析器。希望它有帮助
编辑
将这两行放在前面以供说明。
解释很简单。使用
doc.rootElement
,您可以检索整个 xml 文档,从
到This is the code I use to retrieve the user attribute. It works.
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 classNSData
.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.
The explanation is quite simple. With
doc.rootElement
you retrieve the entire xml document, from<result>
to</result>