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