用java解析http返回的xml
因此,我尝试搜索并搜索如何执行此操作,但我不断看到许多我需要的复杂答案。我基本上使用 Flurry Analytics API 从 HTTP 请求返回一些 xml 代码,这就是它返回的内容。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<eventMetrics type="Event" startDate="2011-2-28" eventName="Tip Calculated" endDate="2011-3-1" version="1.0" generatedDate="3/1/11 11:32 AM">
<day uniqueUsers="1" totalSessions="24" totalCount="3" date="2011-02-28"/>
<day uniqueUsers="0" totalSessions="0" totalCount="0" date="2011-03-01"/>
<parameters/>
</eventMetrics>
我想要得到的只是totalCount 数字,Java 中的totalCount 为3 到int 或string。我研究了不同的 DOM 和 SAX 方法,它们似乎获取标签之外的信息。有什么办法我可以在标签内获取totalCount吗?
谢谢,
更新
我找到了这个网址 -http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser/
这帮助了我考虑它是在android中。但我感谢所有回复我并帮助我的人。我检查了每个答案,它对了解正在发生的事情有一点帮助。但是,现在我似乎无法从我的 url 中获取 xml,因为它需要先进行 HTTP post,然后才能获取 xml。当它从我的 url 获取 xml 时,它只是说找不到文件。
更新2
我已经把所有的事情都整理好了,现在阅读它并从Flurry Analytics获取xml(如果有人偶然发现这个问题,可以作为参考)
So I've tried searching and searching on how to do this but I keep seeing a lot of complicated answers for what I need. I basically am using the Flurry Analytics API to return some xml code from an HTTP request and this is what it returns.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<eventMetrics type="Event" startDate="2011-2-28" eventName="Tip Calculated" endDate="2011-3-1" version="1.0" generatedDate="3/1/11 11:32 AM">
<day uniqueUsers="1" totalSessions="24" totalCount="3" date="2011-02-28"/>
<day uniqueUsers="0" totalSessions="0" totalCount="0" date="2011-03-01"/>
<parameters/>
</eventMetrics>
All I want to get is that totalCount number which is 3 with Java to an int or string. I've looked at the different DOM and SAX methods and they seem to grab information outside of the tags. Is there someway I can just grab totalCount within the tag?
Thanks,
Update
I found this url -http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser/
That helped me considering it was in android. But I thank everyone who responded for helping me out. I checked out every answer and it helped out a little bit for getting to understand what's going on. However, now I can't seem to grab the xml from my url because it requires an HTTP post first to then get the xml. When it goes to grab xml from my url it just says file not found.
Update 2
I got it all sorted out reading it in now and getting the xml from Flurry Analytics (for reference if anyone stumbles upon this question)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
totalCount
就是我们所说的属性。如果您使用org.w3c.dom
API,则调用 getAttribute("totalCount") 在适当的元素上。totalCount
is what we call an attribute. If you're using theorg.w3c.dom
API, you call getAttribute("totalCount") on the appropriate element.如果您使用 SAX 处理程序,请重写 startElement 回调方法以访问属性:
If you are using an SAX handler, override the startElement callback method to access attributes:
JDOM 示例。请注意使用 SAXBuilder 来加载文档。
A JDOM example. Note the use of SAXBuilder to load the document.
我认为最简单的方法是使用XPath,下面是一个基于vtd-xml的示例。
I think that the simplest way is to use XPath, below is an example based on vtd-xml.