Android XMl parsing magic xstream 1: parse the aa. xml file in the asset folder of the android project, xstreamandroid

Source: Internet
Author: User

Android XMl parsing magic xstream 1: parse the aa. xml file in the asset folder of the android project, xstreamandroid

Introduction

XStream is an open-source project and a simple and practical class library used to serialize the conversion between objects and XML objects.

Parses the content of an XML file into an object or serializes an object into an XML file.

 

1. Download tool xstream

Download the latest version address: https://nexus.codehaus.org/content/repositories/releases/com/thoughtworks/xstream/

After downloading the jar package, import it to your android project.

 

2. The aa. xml file in the asset folder

<? Xml version = "1.0" encoding = "UTF-8"?>
<Product>
<Name> jike </name>
<Age> 30 </age>
</Product>

 

 

3. Entity

 

package com.android10;public class Product {    private String name ;        private String age  ;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }}

 

4. Implementation Code

Package com. android10; import java. io. IOException; import java. io. inputStream; import android. app. activity; import android. OS. bundle; import com. thoughtworks. xstream. XStream; public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the data stream String fileName = "aa. xml "; // file name try {InputStream in = getResources (). getAssets (). open (fileName); XStream xstream = new XStream (); xstream. alias ("product", Product. class); Product product = (Product) xstream. fromXML (in); System. out. println ("sss" + product. getName () + product. getAge ();} catch (IOException e) {e. printStackTrace ();}}}

 

5. Running result
Sss jike30

 

6. Project

Http://download.csdn.net/detail/yanzi2015/8797641

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.