用java解析http返回的xml

发布于 2024-10-20 08:29:48 字数 1303 浏览 3 评论 0原文

因此,我尝试搜索并搜索如何执行此操作,但我不断看到许多我需要的复杂答案。我基本上使用 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(如果有人偶然发现这个问题,可以作为参考)

HTTP 请求 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)

HTTP request for XML file

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

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

发布评论

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

评论(4

南渊 2024-10-27 08:29:48

totalCount 就是我们所说的属性。如果您使用 org.w3c.dom API,则调用 getAttribute("totalCount") 在适当的元素上。

totalCount is what we call an attribute. If you're using the org.w3c.dom API, you call getAttribute("totalCount") on the appropriate element.

北方的韩爷 2024-10-27 08:29:48

如果您使用 SAX 处理程序,请重写 startElement 回调方法以访问属性:

public void startElement (String uri, String name, String qName, Attributes atts)
{
    if("day".equals (qName)) {
      String total = attrs.getValue("totalCount");
    }
}

If you are using an SAX handler, override the startElement callback method to access attributes:

public void startElement (String uri, String name, String qName, Attributes atts)
{
    if("day".equals (qName)) {
      String total = attrs.getValue("totalCount");
    }
}
誰ツ都不明白 2024-10-27 08:29:48

JDOM 示例。请注意使用 SAXBuilder 来加载文档。

URL httpSource = new URL("some url string");
Document document = SAXBuilder.build(httpSource);
List<?> elements = document.getDescendants(new KeyFilter());

for (Element e : elements) {
  //do something more useful with it than this
  String total = (Element) e.getAttributeValue("totalCount");
}

class KeyFilter implements Filter {
  public boolean matches (Object obj) {
    return (Element) obj.getName().equals("key");
  }
}

A JDOM example. Note the use of SAXBuilder to load the document.

URL httpSource = new URL("some url string");
Document document = SAXBuilder.build(httpSource);
List<?> elements = document.getDescendants(new KeyFilter());

for (Element e : elements) {
  //do something more useful with it than this
  String total = (Element) e.getAttributeValue("totalCount");
}

class KeyFilter implements Filter {
  public boolean matches (Object obj) {
    return (Element) obj.getName().equals("key");
  }
}
淡水深流 2024-10-27 08:29:48

我认为最简单的方法是使用XPath,下面是一个基于vtd-xml的示例。

  import com.ximpleware.*; 
    public class test {
        public static void main(String[] args) throws Exception {
            String xpathExpr = "/eventMetrics/day/@totalCount";
            VTDGen vg = new VTDGen();
            int i = -1;

            if (vg.parseHttpUrl("http://localhost/test.xml", true)) {
                VTDNav vn = vg.getNav();
                AutoPilot ap = new AutoPilot();
                ap.selectXPath(xpathExpr);
                ap.bind(vn);
                System.out.println("total count "+(int)ap.evalXPathtoDouble());
                    }
        }
    }

I think that the simplest way is to use XPath, below is an example based on vtd-xml.

  import com.ximpleware.*; 
    public class test {
        public static void main(String[] args) throws Exception {
            String xpathExpr = "/eventMetrics/day/@totalCount";
            VTDGen vg = new VTDGen();
            int i = -1;

            if (vg.parseHttpUrl("http://localhost/test.xml", true)) {
                VTDNav vn = vg.getNav();
                AutoPilot ap = new AutoPilot();
                ap.selectXPath(xpathExpr);
                ap.bind(vn);
                System.out.println("total count "+(int)ap.evalXPathtoDouble());
                    }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文