Last Updated: February 25, 2016
·
1.216K
· drdrej

Parse XML in Android(tm) with xml-drafts-framework

Xml-drafts-framework is a small framework to parse XML based on XML-pull-parser for Android. This framework is written by me and open-sourced under apache license on github. It aims to work with XML and to write less code as possible.

  1. you need an xml:

    <root>
    <item name="test-name" />
    </root>

  2. set up your parser-states:

    enum States implements Tag {
      ITEM( "item "),
      ROOT( "root", ITEM)     
     ...

    }

  3. write a parser based on states

    final XMLTagHandler tagHandler = 
                        new XMLTagHandler(States.ROOT, callback) {
    
      @Override
      protected void startTag(final Tag current, 
                         final XMLTagFacade facade)
              throws Exception {
    
          if( current == States.ITEM  ) {
                  System.out.println( "-- found an item: " 
                     + facade.getAttribute("name" ));
          }
      }
    
      @Override
      protected void closeTag(Tag current) 
              throws Exception {
          ;
      }
    };
  4. run a parser

    final InpuStream in = ...;
    final XMLParserLoop loop = XMLParserLoop.create( tagHandler  );
    
    loop.useInput( in );
    loop.run();

with greetings from Cologne,
Andreas Siebert, ask@touchableheroes.com
(aka DrDrej)