Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read and write XML in Android

Shulou Source: shulou.com Published: 2022-05-31 23:36:55 09月21日 Update

This article mainly introduces the relevant knowledge of how to read and write XML in Android, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will have something to gain after reading this Android article on how to read and write XML. Let's take a look.

Android News Reader

The application will take an RSS feed from the popular Android developer site Androidster and parse it into a set of simple Java objects that you can use to build an Android ListView (see the download section for source code). This is a typical polymorphic behavior-providing different implementations of the same behavior (different XML parsing algorithms). Listing 1 shows how to build this model using an interface in the Java code.

Listing 1. XML feed parser interface

Java Code:

Package eoe.android; import java.util.List; public interface FeedParser {List

< Message >

Parse ();}

Java Code:

Public class Message implements Comparable

< Message >

{static SimpleDateFormat FORMATTER = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss Z"); private String title; private URL link; private String description; private Date date; / / getters and setters omitted for brevity public void setLink (String link) {try {this.link = new URL (link);} catch (MalformedURLException e) {throw new RuntimeException (e);}} public String getDate () {return FORMATTER.format (this.date) } public void setDate (String date) {/ / pad the date if necessary while (! date.endsWith ("00")) {date + = "0";} try {this.date = FORMATTER.parse (date.trim ());} catch (ParseException e) {throw new RuntimeException (e) } @ Override public String toString () {/ / omitted for brevity} @ Override public int hashCode () {/ / omitted for brevity} @ Override public boolean equals (Object obj) {/ / omitted for brevity} / / sort by date public int compareTo (Message another) {if (another = = null) return 1; / / sort descending, most recent first return another.date.compareTo (date) }} this is the end of the article on "how to read and write XML in Android". Thank you for reading! I believe you all have a certain understanding of "how to read and write XML in Android". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Code knowledge difference content object interface feed list article behavior polymorphism same hot people value typical application simple operation article news Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Shulou Tech Info MySQL vpn