1

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

Community
  • 1
  • 1
rwarner
  • 577
  • 2
  • 4
  • 14
  • What do yo mean by "they seem to grab information outside of the tags? – ajuc Mar 01 '11 at 20:27
  • I just meant like, from my research online I've noticed that a lot of the methods for grabbing xml were "looking" for the outside tags and returning/seeking the information between tags. But in my situation I have nothing between tags, they are INSIDE tags. – rwarner Mar 01 '11 at 20:34
  • why don't u just use xpath: /eventMetrics/day/@totalCount – vtd-xml-author Mar 01 '11 at 21:29
  • @vtd-xml-author - Does Android have the javax.xml.xpath APIs? – bdoughan Mar 01 '11 at 21:43
  • yes, it seems that it does. http://developer.android.com/reference/javax/xml/xpath/package-summary.html – vtd-xml-author Mar 02 '11 at 01:46
  • You answer can be found here: http://stackoverflow.com/questions/5162063/http-request-for-xml-file – Foggzie Feb 02 '12 at 16:44

4 Answers4

4

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

Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71
4

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");
    }
}
Brent Worden
  • 10,624
  • 7
  • 52
  • 57
  • 1
    @kentoe - Check out one of my answers to a previous question to see how to create an XMLReader: http://stackoverflow.com/questions/3405702/using-sax-to-parse-common-xml-elements/3409270#3409270 – bdoughan Mar 01 '11 at 21:05
2

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");
  }
}
Mike Yockey
  • 4,565
  • 22
  • 41
0

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());
                    }
        }
    }
vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30