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

Teasing Microsoft Cortana, it will answer you like this

Microsoft's personal voice assistant Cortana i...

Methodology for practical operation of short video

As usual, I have summarized some practical experi...

As Siberia continues to warm, is the disappearance of the tundra inevitable?

Siberian tundra could disappear within 500 years?...

Toxic, harmful and disease-causing! Throw away these plastic bags at home

Our daily lives seem to be inseparable from plast...

Photo browsing artifact source code sharing

Source code introduction: Share a beautiful pictu...

Introduction to iQIYI’s effective promotion products

There is no doubt that iQiyi has taken the top sp...

Tips and algorithms for creating popular short videos!

01 Changes in the communication model: from manua...

After studying 10 big Douyin accounts, I found 8 Douyin promotion routines

Brother Xian has been feeling rather depressed la...

World's most complete dinosaur embryo discovered

After three years of detailed research, scientist...

Helmets are far from enough. Virtual reality is about comprehensive strength.

In 2015, with the bombardment of information from...