android XMl 解析神奇xstream 二: 把對象轉換成xml

來源:互聯網
上載者:User

標籤:

前言:對xstream不理解的請看:android XMl 解析神奇xstream 一: 解析android項目中 asset 檔案夾 下的 aa.xml 檔案

 

1、Javabeen 代碼

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;    }}

 

2、操作執行個體

package com.android10;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 );        Product product = new Product() ;        product.setName( "jhon" ) ;        product.setAge( "30" );        System.out.println("sss " + product.getName() + product.getAge()  );        //把對象轉化為 XML        XStream xstream1 = new XStream();            String string = xstream1.toXML( product ) ;        System.out.println( "sss"+ string );         //把對象轉化為 XML,並且設定別名        XStream xstream2 = new XStream();            xstream2.alias( "blog" , Product.class) ;   //修改別名        String string2 = xstream2.toXML( product ) ;        System.out.println( "sss"+ string2 );     }}

 

3、運行效果

    <com.android10.Product>  

       <age>30</age>

      <name>jhon</name>

  </com.android10.Product>

 

   <blog>

      <age>30</age>

      <name>jhon</name>

  </blog>

 

4、注意事項

在測試的時候,發現  System.out.println( "sss"+ string );   和   System.out.println( "sss"+ string2 );

輸出的是:

 sss<com.android10.Product>
 sss<blog>

開始以為是哪裡寫錯了,最後在 debug 斷點調試的時候,發現 string 的值:

<com.android10.Product>  

       <age>30</age>

      <name>jhon</name>

  </com.android10.Product>

得出的結論是,eclipse控制台無法輸出 xml格式的 字串

 

android XMl 解析神奇xstream 二: 把對象轉換成xml

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.