android 使用Java库xstream示例

来源:互联网 发布:铁路造价软件 编辑:程序博客网 时间:2024/05/22 15:20

android 使用Java库xstream示例。这是Java解析xml文件的库,基于Java7基础上的。那么在android上也是一样适用的。下面就说说有两种形式的xml解析以及存储。

本文博客的下载地址:http://download.csdn.net/download/qq_16064871/10012084

1、如图



2、格式一

这是一种很常见的xml存储方式,我之前写过这种的存储,当时是使用自带的api进行存储。

android 自定义xml存储方式以及存储位置

现在就是实体类。

<ConfigTest1>  <TestFour>    <displayAll>true</displayAll>    <index>400</index>    <promptBound>7897.0</promptBound>  </TestFour>  <TestOne>    <displayAll>true</displayAll>    <index>100</index>    <promptBound>15.9</promptBound>  </TestOne>  <TestThree>    <displayAll>true</displayAll>    <index>300</index>    <promptBound>7868.0</promptBound>  </TestThree>  <TestTwo>    <displayAll>true</displayAll>    <index>200</index>    <promptBound>125.9</promptBound>  </TestTwo></ConfigTest1>


代码实现

实体类定义如下

package com.example.xstream.config;import com.thoughtworks.xstream.annotations.XStreamAlias;/** * */@XStreamAlias("ConfigTest1")public class ConfigTest1 {    public ConfigTest1() {        testOne = new TestOne();        testTwo = new TestTwo();        testThree = new TestThree();        testFour = new TestFour();    }    @XStreamAlias("TestOne")    public TestOne testOne;    @XStreamAlias("TestTwo")    public TestTwo testTwo;    @XStreamAlias("TestThree")    public TestThree testThree;    @XStreamAlias("TestFour")    public TestFour testFour;    public static class TestOne {        public int index;        public double promptBound = 1;        public boolean displayAll = true;    }    public static class TestTwo {        public int index;        public double promptBound = 1;        public boolean displayAll = true;    }    public static class TestThree {        private int index;        private double promptBound = 1;        private boolean displayAll = true;        public int getIndex() {            return index;        }        public void setIndex(int index) {            this.index = index;        }        public double getPromptBound() {            return promptBound;        }        public void setPromptBound(double promptBound) {            this.promptBound = promptBound;        }        public boolean isDisplayAll() {            return displayAll;        }        public void setDisplayAll(boolean displayAll) {            this.displayAll = displayAll;        }    }    public static class TestFour {        private int index;        private double promptBound = 1;        private boolean displayAll = true;        public int getIndex() {            return index;        }        public void setIndex(int index) {            this.index = index;        }        public double getPromptBound() {            return promptBound;        }        public void setPromptBound(double promptBound) {            this.promptBound = promptBound;        }        public boolean isDisplayAll() {            return displayAll;        }        public void setDisplayAll(boolean displayAll) {            this.displayAll = displayAll;        }    }}

实体类管理类,以及存储加载

package com.example.xstream.config;import com.thoughtworks.xstream.XStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;/** * * @author Administrator */public class ConfigTestManager1 {    //单例模式类对象    private volatile static ConfigTestManager1 mConfigManager = null;    private static ConfigTest1 config;    private static XStream xStream;    //单例模式获取实例    public static ConfigTestManager1 GetInstance() {        synchronized (ConfigTestManager1.class) {            if (mConfigManager == null) {                mConfigManager = new ConfigTestManager1();                xStream = new XStream();                xStream.alias("ConfigTest1", ConfigTest1.class);                xStream.autodetectAnnotations(true);                if (config == null)                    config = new ConfigTest1();            }        }        return mConfigManager;    }    //打开程序工程配置    public void LoadConfig() {        readXmlProjectConfig(FilePathManage.GetInstance().getDataDirectory(), "ConfigTest1.xml");    }    //保存程序工程配置    public void SaveConfig() {        writeXmlProjectConfig(FilePathManage.GetInstance().getDataDirectory(), "ConfigTest1.xml");    }    //获取配置    public ConfigTest1 getConfig() {        return config;    }    //读写配置文件    private void readXmlProjectConfig(String strPath, String strName) {        config = null;        try {            FileInputStream inputStream = new FileInputStream(new File(strPath, strName));            config = (ConfigTest1) xStream.fromXML(inputStream);        } catch (Exception e) {            e.printStackTrace();        }        if (config == null) {            config = new ConfigTest1();            SaveConfig();        }    }    private void writeXmlProjectConfig(String strPath, String strName) {        try {            FileOutputStream outputStream = new FileOutputStream(new File(strPath, strName));            xStream.toXML(config, outputStream);        } catch (FileNotFoundException e) {            e.printStackTrace();        }    }    public int getTestOneindex(){        return config.testOne.index;    }    public double getTestOnepromptBound(){        return config.testOne.promptBound;    }    public boolean getTestTwodisplayAll(){        return config.testTwo.displayAll;    }}

3、格式二

<ConfigSystems>  <Group Name="Argentina (POSGAR94)">    <System Name="beijing">      <Ellipsoid Name="xian" a="6378140.0" f="298.257"/>      <Projection FalseEast="500000.0" CuttingRadius="0.0" AxisDirection="0"/>    </System>  </Group></ConfigSystems>

实体类

package com.example.xstream.config;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamAsAttribute;@XStreamAlias("ConfigSystems")public class ConfigSystems {    @XStreamAsAttribute()    @XStreamAlias("Group")    public Group group = new Group();    public ConfigSystems.Group getSystemGroup() {        return group;    }    public void setCoordinateSystemGroup(ConfigSystems.Group stemGroup) {        group = stemGroup;    }    @XStreamAlias("Group")    public static class Group {        @XStreamAsAttribute()        @XStreamAlias("Name")        public String Name="beijing54";        @XStreamAlias("System")        public System System =new System();        public String getName() {            return Name;        }        public void setName(String name) {            Name = name;        }        public ConfigSystems.System getSystem() {            return System;        }        public void setCoordinateSystem(ConfigSystems.System system) {            System = system;        }    }    @XStreamAlias("System")    public static class System {        @XStreamAsAttribute()        @XStreamAlias("Name")        public String Name="";        @XStreamAlias("Ellipsoid")        public Ellipsoid Ellipsoid = new Ellipsoid();        @XStreamAlias("Projection")        public Projection Projection= new Projection();        public String getName() {            return Name;        }        public void setName(String name) {            Name = name;        }        public ConfigSystems.Ellipsoid getEllipsoid() {            return Ellipsoid;        }        public void setEllipsoid(ConfigSystems.Ellipsoid ellipsoid) {            Ellipsoid = ellipsoid;        }        public ConfigSystems.Projection getProjection() {            return Projection;        }        public void setProjection(ConfigSystems.Projection projection) {            Projection = projection;        }    }    public static class Ellipsoid {        @XStreamAsAttribute()        @XStreamAlias("Name")        public String Name ="bj54";        @XStreamAsAttribute()        @XStreamAlias("a")        public double a = 6378245.0;        @XStreamAsAttribute()        @XStreamAlias("f")        public double f = 298.3;        public String getName() {            return Name;        }        public void setName(String name) {            Name = name;        }        public double getA() {            return a;        }        public void setA(double a) {            this.a = a;        }        public double getF() {            return f;        }        public void setF(double f) {            this.f = f;        }        @Override        public String toString() {            return "Ellipsoid{" +                    "Name='" + Name + '\'' +                    ", a=" + a +                    ", f=" + f +                    '}';        }    }    public static class Projection {        @XStreamAsAttribute()        @XStreamAlias("AxisDirection")        public int AxisDirection;        @XStreamAsAttribute()        @XStreamAlias("CuttingRadius")        public double CuttingRadius;        @XStreamAsAttribute()        @XStreamAlias("FalseEast")        public double FalseEast = 500000;        public int getAxisDirection() {            return AxisDirection;        }        public void setAxisDirection(int axisDirection) {            AxisDirection = axisDirection;        }        public double getCuttingRadius() {            return CuttingRadius;        }        public void setCuttingRadius(double cuttingRadius) {            CuttingRadius = cuttingRadius;        }        public double getFalseEast() {            return FalseEast;        }        public void setFalseEast(double falseEast) {            FalseEast = falseEast;        }        @Override        public String toString() {            return "Projection{" +                    "AxisDirection=" + AxisDirection +                    ", CuttingRadius=" + CuttingRadius +                    ", FalseEast=" + FalseEast +                    '}';        }    }}

实体类管理类,以及存储加载

package com.example.xstream.config;import android.os.Environment;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;import com.thoughtworks.xstream.io.xml.XppDriver;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class ConfigSystemManager {    //单例模式类对象    private volatile static ConfigSystemManager mConfigManager = null;    private XStream xStream = null;    //单例模式获取实例    public static ConfigSystemManager GetInstance() {        synchronized (ConfigSystemManager.class) {            if (mConfigManager == null) {                mConfigManager = new ConfigSystemManager();                mConfigManager.xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));                mConfigManager.xStream.alias("ConfigTest1", ConfigSystems.class);                mConfigManager.xStream.autodetectAnnotations(true);            }        }        return mConfigManager;    }    public boolean LoadformFile(String strPath, String strName) {        if (strPath.isEmpty() || strName.isEmpty()) return false;        ConfigSystems configSystems = null;        FileInputStream inputStream = null;        try {            inputStream = new FileInputStream(new File(Environment.getExternalStorageDirectory(), strName));            configSystems = (ConfigSystems) xStream.fromXML(inputStream);            inputStream.close();        } catch (Exception e) {            e.printStackTrace();        }        if (configSystems == null) {            configSystems = new ConfigSystems();            writeXmlProjectConfig(FilePathManage.GetInstance().getDataDirectory(), "ConfigSystems.xml");        }        return true;    }    private void writeXmlProjectConfig(String strPath, String strName) {        try {            ConfigSystems configSystems = new ConfigSystems();            FileOutputStream outputStream = new FileOutputStream(new File(strPath, strName));            xStream.toXML(configSystems, outputStream);        } catch (FileNotFoundException e) {            e.printStackTrace();        }    }    public boolean SaveasFile(String strPath, String strName) {        if (strPath.isEmpty() || strName.isEmpty()) return false;        ConfigSystems configSystems = new ConfigSystems();        ConfigSystems.Group group = configSystems.getSystemGroup();        group.setName("Argentina (POSGAR94)");        ConfigSystems.System system = group.getSystem();        system.setName("beijing");        //Ellipsoid        ConfigSystems.Ellipsoid ellipsoid = system.getEllipsoid();        ellipsoid.setName("xian");        ellipsoid.setA(6378140.0);        ellipsoid.setF(298.257);        //Projection        ConfigSystems.Projection projection = system.getProjection();        projection.setAxisDirection(0);        projection.setCuttingRadius(0);        projection.setFalseEast(500000);


4、简单调用示例代码

package com.example.xstream;import android.app.Activity;import android.content.Intent;import android.graphics.SweepGradient;import android.os.Bundle;import android.view.View;import com.example.xstream.config.ConfigSystemManager;import com.example.xstream.config.ConfigTestManager1;import com.example.xstream.config.FilePathManage;public class MainActivity extends Activity implements View.OnClickListener{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_test1);        findViewById(R.id.button).setOnClickListener(this);        findViewById(R.id.button2).setOnClickListener(this);        findViewById(R.id.button3).setOnClickListener(this);        findViewById(R.id.button4).setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.button:                ConfigTestManager1.GetInstance().LoadConfig();                break;            case R.id.button2:                ConfigTestManager1.GetInstance().getConfig().testOne.index = 100;                ConfigTestManager1.GetInstance().getConfig().testOne.promptBound = 15.9;                ConfigTestManager1.GetInstance().getConfig().testOne.displayAll = true;                ConfigTestManager1.GetInstance().getConfig().testTwo.index = 200;                ConfigTestManager1.GetInstance().getConfig().testTwo.promptBound = 125.9;                ConfigTestManager1.GetInstance().getConfig().testTwo.displayAll = true;                ConfigTestManager1.GetInstance().getConfig().testThree.setIndex(300);                ConfigTestManager1.GetInstance().getConfig().testThree.setDisplayAll(true);                ConfigTestManager1.GetInstance().getConfig().testThree.setPromptBound(7868);                ConfigTestManager1.GetInstance().getConfig().testFour.setIndex(400);                ConfigTestManager1.GetInstance().getConfig().testFour.setDisplayAll(true);                ConfigTestManager1.GetInstance().getConfig().testFour.setPromptBound(7897);                ConfigTestManager1.GetInstance().SaveConfig();                break;            case R.id.button3:                ConfigSystemManager.GetInstance().LoadformFile(FilePathManage.GetInstance().getDataDirectory(), "ConfigSystems.xml");                break;            case R.id.button4:                ConfigSystemManager.GetInstance().SaveasFile(FilePathManage.GetInstance().getDataDirectory(), "ConfigSystems.xml");                break;        }    }}

5、注意事项

存储和读写权限记得加上

如果有混淆,记得注意去掉混淆。




原创粉丝点击