Friday, September 16, 2011

Simpler and Easier XML Parsing in Android

 

xml_5+android

 

XML Parsing Problem

Surprisingly, XML parsing is not as trivial as one might think in Android. XPath and XML Serialization is only supported in API level 8 or above, which drives many developers to uses third party libraries for XML related tasks.

AQuery XML Usage

One of the key feature of AQuery is asynchronous http fetch, which often is used to invoke REST type of services over the internet. These types of API/services, such as Facebook, Twitter, or Picasa (example provided in this post) usually serves results in XML and/or JSON formats for apps to consume.

For this purpose, AQuery wants to provide a simple, easy, and light-weight method to parse these XML responses. Such method must be independent on third party library and supports API level 4.

XmlDom

Here we introduce AQuery’s XmlDom. [source] [javadoc] [download]

XmlDom is a specialized class for simple and easy XML parsing, designed to be used in basic Android api 4+ runtime without any dependency, and integrated directly with AQuery’s ajax method.

Simple

Per AQuery’s philosophy, we want to make our tasks as simple as possible. Here’s an example that get an XML of featured images from Google’s Picasa service, parse the response, and display one of the featured image it in our app.

This is done with AQuery with just 10 lines of code.

public void xml_ajax(){        
    String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=8";        
    aq.ajax(url, XmlDom.class, this, "picasaCb");        
}
 
public void picasaCb(String url, XmlDom xml, AjaxStatus status){
 
    List<XmlDom> entries = xml.tags("entry");        
    List<String> titles = new ArrayList<String>();
    
    String imageUrl = null;
    
    for(XmlDom entry: entries){
        titles.add(entry.text("title"));
        imageUrl = entry.tag("content", "type", "image/jpeg").attr("src");
    }
        
    aq.id(R.id.image).image(imageUrl);
    
}}
 
 
Constructors
 
 
XmlDom(String str)
 
XmlDom(byte[] data)
 
XmlDom(InputStream is)
 
XmlDom(Element element)
Methods

[javadoc]

tag(), tags()

//return a list of "entry" nodes
List<XmlDom> entries = xml.tags("entry");  
 
child(), children()
 
//child is similar to tag, except it only return child nodes immediately under the parent
XmlDom title = entry.child("title");
 
text()
 
//extract the text value of the title tag
String text = title.text();
 
attr()
 
//extract the src attribute of a content tag
String image = content.attr("src");
Serialization (beta)
//Convert a XmlDom object to a XML string
String rawXml = xml.toString();
Light Weight

[source]

There’s only 1 class with compressed size of ~3kb. Feel free to download the source and use it even without AQuery core lib.

API Demo

Checkout our API demo app to see this example in action.

AQuery

For more Android development goodies, check out our AQuery home page.

3 comments:

  1. If you aren't positive about which jurisdiction is most suited to your wants and budget we’ll be happy assist you|that will assist you|that can help you} make the best selection. Click on the Contact Us button beneath or choose “Schedule a Consultation with us” so we will to} listen to your small business idea, understand your aims and create a plan to get your project began. 솔카지노 The course of of establishing an online playing firm in Costa Rica consists of incorporating a business entity which specifies its purpose as online playing. It takes round 5 to seven days to finish the method and there are not any paid-up capital deposits, licensing charges, or betting or gaming tax of any sort.

    ReplyDelete
  2. Bonus Abuse is the process of signing as much as} a service multiple of} instances using advertising 카지노사이트 presents normally meant only for new users. It goes by many names together with promo abuse, bonus hunting, bonus whoring, casino whoring, and bonus abusing. Bonus granted is an instantaneous bonus for use on casino merchandise on the Site. An immediate bonus are bonus funds which are be} awarded in a restricted state and additionally be} wagered with instantly upon grant. In this regard, everyone wins – the participant will get rewarded for becoming a member of, and the casino reaps the benefits of getting new members. Naturally, all of those deals come with their own particular phrases and rules, which you need to} at all times be conscious of.

    ReplyDelete
  3. VIP rewards embrace extra bonus funds, entry to exclusive reside games, promo offers, event invitations, and a private account manager. At RedKings, the 점보카지노 extra you play, the extra VIP points you earn for thrilling rewards and ample money opportunities. This on-line on line casino website offers even more advantages to Grosvenor members, together with the power to switch money between the web platform and physical Grosvenor casinos.

    ReplyDelete

AQuery Demo App