Android XML parsing Magic XStream Four: parsing complex XMl files into objects

Source: Internet
Author: User

Preface: For XStream do not understand, please see:

Android XMl parsing Magic XStream: Parse aa.xml file under Asset folder in Android project

Android XML parsing Magic XStream II: Converting objects to XML

Android XML parsing Magic XStream Three: Transforming complex objects into XML

1. Document Preparation

Put a pre-written XML file in the Asset folder of the Android project directory.

The contents of the file are:

<Blog>  < Age>30</ Age>  <name>Jhon</name>  < Person>    <PAge>36</PAge>    <PName>Saliy</PName>  </ Person></Blog>

2, Javabeen

 PackageCom.android10; Public classPerson {String pName;        String PAge;  PublicString Getpname () {returnPName; }     Public voidsetpname (String pName) { This. PName =PName; }     PublicString GetPage () {returnPAge; }     Public voidsetpage (String pAge) { This. PAge =PAge; } }

 PackageCom.android10; Public classProduct {PrivateString name; PrivateString age; Privateperson person ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getage () {returnAge ; }     Public voidsetage (String age) { This. Age =Age ; }     PublicPerson Getperson () {returnPerson ; }     Public voidSetperson (person person) { This. person =Person ; }}


3. Main methods

 PackageCom.android10;Importjava.io.IOException;ImportJava.io.InputStream;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportCom.thoughtworks.xstream.XStream; Public classMainactivityextendsActivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //get the data flow in the resourceString fileName = "Aa.xml";//file name        Try{InputStream in=getresources (). Getassets (). open (FileName); XStream XStream=NewXStream (); // This blog logo must be and XML to keep, otherwise it will error Xstream.alias ("blog", Product.class ); Product Product=(Product) xstream.fromxml (in); System.out.println ("SSS" + product.getname () +product.getage ()); } Catch(IOException e) {e.printstacktrace (); }      }}


4. Operation result

Product Jhon30 Person saliy36

Android XML parsing Magic XStream Four: parsing complex XMl files into objects

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.