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

6
  • What do yo mean by "they seem to grab information outside of the tags? Commented Mar 1, 2011 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. Commented Mar 1, 2011 at 20:34
  • why don't u just use xpath: /eventMetrics/day/@totalCount Commented Mar 1, 2011 at 21:29
  • @vtd-xml-author - Does Android have the javax.xml.xpath APIs? Commented Mar 1, 2011 at 21:43
  • yes, it seems that it does. developer.android.com/reference/javax/xml/xpath/… Commented Mar 2, 2011 at 1:46

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there anymore information on the org.w3c.dom? I have my Document doc = db.parse(in); where in is my xml url. But I'm having trouble branching off from my created variables.
Dude, come on. I just gave you a link to the documentation.
Sorry, I totally spaced on the hyperlink. My bad.
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");
    }
}

1 Comment

@kentoe - Check out one of my answers to a previous question to see how to create an XMLReader: stackoverflow.com/questions/3405702/…
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");
  }
}

Comments

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());
                    }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.