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

How much does it cost to develop a movie mini app in Zigong?

Zigong movie applet development price 1. Display ...

El Niño is coming, are you ready?

According to the latest monitoring by the Nationa...

A rough man's home version of hip camp 2.0

A rough man's family version of hip camp 2.0 ...

Where is the most effective place to place the Wenchang Tower in 2020?

In life, we see some families placing the Feng Sh...

After watching 1,000 videos, we summarized 10 ways to make money on Douyin!

In the past two days, I have watched more than 1,...

Do we have to be better ourselves? Perfectionism is not "perfect"...

In daily life, perfectionism is often seen as a p...

Watsons crosses borders to attract fans, teach you how to play fan marketing

In brand marketing , fans are not only high-quali...

Tik Tok marketing promotion, how can one video gain 480,000 followers in 2 days?

Faced with the trend of Douyin, the actual situat...

Anxiety is not what you think

© Unsplash Leviathan Press: Danish philosopher Sø...

How to add negative keywords in batches in Baidu bidding?

(1). How to add negative keywords in batches in B...