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

3 Tips for Writing an Event Planning Proposal

Let’s talk about planning and writing plans today...

How to improve product activity? There are these 6 operating routines

In fact, I haven’t used QQ to chat for a long tim...

Testin APPBase: A head-to-head battle in the collaborative office app market

Whether the collaborative office APP itself is st...

My 30 operating notes, you may need

01 Chat with a product manager . He said that it ...

Why does Baidu's driverless car make President Xi linger on it?

At the World Internet Conference in Wuzhen these ...

Baidu searched for 18 details of ocpc

Baidu Big Search ocpc was launched in the second ...

We talk about operations every day, so what kind of data does operations need?

1. What exactly is the operation doing? Before ge...

Why does Party A ask Party B to focus on creativity and less on strategy?

Many advertising companies often run into problem...

Xiaohongshu Hot Note Titles (Tutorial Included)

There are too few eyes in the notes and the numbe...