XML 4 parsing examples

XML 4 parsing examples

Functional classification: Tools

Supported platforms: Android

Operating environment: Eclipse

Development language: Java

Development tool: Eclipse

Source code size: 13.01MB

Source code introduction

A summary example of how XML files are parsed in Android.

It includes the specific implementation steps of parsing the sample XML file using DOM parsing, DOM4J parsing, pull parsing and SAX parsing, with detailed comments. This example parses the XML file into entity classes and displays them through pstView.

Note: dom4j parsing requires an additional class library: dom4j-1.6.1.jar. The compressed package provides a complete dom4j class library. If you only need to do parsing, just import dom4j-1.6.1.jar.

Source code running screenshot

    Clicking different buttons will parse in different ways, but the display effect is the same

    After the analysis is completed, it will be displayed through pstview

    Click on an entry to display detailed information (from the XML file)

Source code snippet

    /**Android has built-in pull parsing, which saves more memory than DOM parsing.*/  

    pubpc   class XmlPullParserUtil {

      

    pubpc   static Arraypst<cdentity> pullParser(InputStream in){

    Arraypst<cdentity> cds = null ;

    try {

    //Build the pull parsing factory  

    XmlPullParserFactory factory;

    factory = XmlPullParserFactory.newInstance();

    //Build the pull parser object  

    XmlPullParser parser = factory.newPullParser();

    //Set the data source of the parser  

    parser.setInput( new InputStreamReader(in));

    //Get the event and start parsing  

    int eventType = parser.getEventType();

    //The CD object to be generated  

    CDEntity entity = null ;

    //Loop through the XML document until you reach the end of the document  

    while (eventType != XmlPullParser.END_DOCUMENT){

    switch (eventType) {

    //Construct the Arraypst object at the beginning of the xml document.  

    case XmlPullParser.START_DOCUMENT:

    cds = new Arraypst<cdentity>();

    break ;

    //Judge the tag name at the beginning of the tag  

    case XmlPullParser.START_TAG:

    String name = parser.getName();

    //When the tag name is CD, construct a CD object  

    if ( "CD" .equals(name)){

    entity = new CDEntity();

    //If the tag is title, then cd must not be empty, and the obtained text is the cd title, then set the title attribute of cd to the text of the title tag  

    } else   if ( "TITLE" .equals(name)){

    entity.setTitle(parser.nextText());

    } else   if ( "ARTIST" .equals(name)){

    entity.setArtist(parser.nextText());

    } else   if ( "COUNTRY" .equals(name)){

    entity.setCountry(parser.nextText());

    } else   if ( "COMPANY" .equals(name)){

    entity.setCompany(parser.nextText());

    } else   if ( "PRICE" .equals(name)){

    entity.setPrice(Float.parseFloat(parser.nextText()));

    } else   if ( "YEAR" .equals(name)){

    entity.setYear(Integer.parseInt(parser.nextText()));

    }

    break ;

    //After the tag ends, determine what the end tag is. If the cd tag ends, the generation of the cd object is complete and it should be added to Arraypst  

    case XmlPullParser.END_TAG:

    if ( "CD" .equals(parser.getName())){

    cds.add(entity);

    }

    break ;

    }

    //After the loop is finished, the label needs to be set to the next label to avoid an infinite loop  

    eventType = parser.next();

    }

    } catch (XmlPullParserException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    }

    System.out.println(cds);

    return cds;

    }

    }

    </cdentity></cdentity></cdentity>

Source code download address: http://down..com/data/1968715

<<:  MultiChoiceAdapter—Easy to make ListView support multiple selections

>>:  FunDapter - Adds various structured data to ListView.

Recommend

Is the Coocaa art TV that sells for 8,000 yuan really just a passing fad?

Art is a general term for talent and technology, ...

Where to open the Kuaishou shopping cart?

This article mainly introduces the relevant infor...

Nuozadu: A milestone project of China's earth-rock dam

Catching up with the 10th anniversary of the powe...

Learn the best practices of modern Android development in one article

Author: Wang Peng, Sun Yongsheng What is MAD? ​​h...

How does the APP achieve automatic renewal?

01. Goal Implement the function of automatic memb...

Fed up with Windows blue screen, which Linux system is best for gaming?

Over the past few months, we have tried multiple ...

Apple's atypical innovation: making money from user loyalty

The industry knows that Apple has always used so-...

A practical guide to user growth

User growth is no longer a new concept. Many comp...