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

5 steps to make your Douyin operation more efficient

If you ask Aunt Lisa, does it cost money to make ...

In 2020, how to use short videos to empower brands?

We have been engaged in marketing for ten years a...

How does operations perform data analysis? 3 ideas and 8 methods!

I've been reading "Chief Growth Officer&...

rich! capricious! : 5 best ways to promote paid apps

How to promote APP through mobile advertising pla...

Want high-resolution remote sensing images? Balloons can also help!

Recently, Urban Sky, a U.S. stratospheric balloon...

Strategies and methods for operating hot products on Xiaohongshu

We love and hate Xiaohongshu . What we love is th...

No. 1 in hot search, Zhong Xuegao responded!

Yesterday at noon, @钟薛高 issued a statement, Ranke...

How to buy traffic and acquire users on Douyin at low cost?

Tik Tok is still in its wild growth period, with ...