当前位置: 动力学知识库 > 问答 > 编程问答 >

xml - Android SAXParser using Simple Framework

问题描述:

I want to parse a xml: http://www.sjc.com.vn/xml/tygiavang.xml

The item i want to get is: buy="32.800" sell="33.070" type="Vng SJC 1L" and updated time

I use Simple Framework to parse but i can not get the element i want.

Here is my class:

ratelist.java

 @Root(name = "ratelist", strict = false)

public class Ratelist{

@Attribute

private String updated;

@Element

private String city;

public String getUpdated(){

return updated;

}

public String getCity(){

return city;

}

}

city.java

 @Root(name = "city", strict = false)

public class City {

@Attribute

private String name;

@Element

private String item;

public String getName(){

return name;

}

public String getItem(){

return item;

}

}

item.java

 @Root(name = "item", strict = false)

public class Item {

@Attribute

private String buy;

@Attribute

private String sell;

@Attribute

private String type;

public String getBuy(){

return buy;

}

public String getSell(){

return sell;

}

public String getType(){

return type;

}

}

Roo_class.java

@Root

public class Root_class {

@Element

private String title;

@Element

private String url;

@Element

private Ratelist ratelist;

public String getTitle(){

return title;

}

public String getUrl(){

return url;

}

public Ratelist getRatelist(){

return ratelist;

}

}

and the main.java

public class main extends Fragment {

ListView listView;

String path = "http://www.sjc.com.vn/xml/tygiavang.xml";

MyAsyncTask myAsyncTask;

SwipeRefreshLayout swipeRefreshLayout;

ProgressDialog progressDialog;

ArrayList<HashMap<String, String>> data_listview = new ArrayList<HashMap<String, String>>();

String ngay;

String mua = "";

String ban = "";

String loai = "";

TextView tv_ban, tv_mua, tv_loai, tv_ngay;

Item item;

@Nullable

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_city_gold, container, false);

listView = (ListView) view.findViewById(R.id.listView);

tv_ban = (TextView) view.findViewById(R.id.textView2);

tv_mua = (TextView) view.findViewById(R.id.textView3);

tv_loai = (TextView) view.findViewById(R.id.textView4);

tv_ngay = (TextView) view.findViewById(R.id.textView13);

swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);

tv_ban.setText(R.string.sell);

tv_mua.setText(R.string.buy);

tv_loai.setText(R.string.type);

if (isNetworkConnected()==true) {

myAsyncTask = new MyAsyncTask();

myAsyncTask.execute();

}else {

Toast.makeText(getActivity(), R.string.requirenetwork, Toast.LENGTH_SHORT).show();

}

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

@Override

public void onRefresh() {

new CountDownTimer(1000, 500) {

@Override

public void onTick(long millisUntilFinished) {

if (isNetworkConnected() == true) {

myAsyncTask = new MyAsyncTask();

myAsyncTask.execute();

}

}

@Override

public void onFinish() {

swipeRefreshLayout.setRefreshing(false);

if (isNetworkConnected() == false) {

snackbar();

}

}

}.start();

}

});

return view;

}

class MyAsyncTask extends AsyncTask<Void, Void, Void> {

@Override

protected void onPreExecute() {

super.onPreExecute();

data_listview.clear();

progressDialog = ProgressDialog.show(getContext(), getActivity().getResources().getString(R.string.updating), getActivity().getResources().getString(R.string.wait));

}

@Override

protected Void doInBackground(Void... arg0) {

// TODO Auto-generated method stub

try {

URL url = new URL(path);

URLConnection connection = url.openConnection();

InputStream is = connection.getInputStream();

Serializer serializer = new Persister();

Ratelist data = serializer.read(Ratelist.class, is);

ban = item.getSell() + "\n";

mua = item.getBuy() + "\n";

loai = item.getType() + "\n";

//

HashMap<String, String> hashmap = new HashMap<String, String>();

hashmap.put("KEY_ID", ban);

hashmap.put("KEY_NAME", mua);

hashmap.put("KEY_BUY", loai);

data_listview.add(hashmap);

// ngay = data.get();

/* InputStream is2 = connection.getInputStream();

Serializer serializer1=new Persister();

ratelist data1=serializer1.read(ratelist.class,is2);

for (ratelist ratelist:data1.getCity()){

}*/

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

@Override

protected void onPostExecute(Void result) {

// TODO Auto-generated method stub

super.onPostExecute(result);

String text = getResources().getString(R.string.update);

tv_ngay.setText(text + ": " + ngay);

ListAdapter adapter = new SimpleAdapter(getActivity(),

data_listview, //du lieu

R.layout.currency_mot_item, //lay out

new String[]{"KEY_ID", "KEY_NAME", "KEY_BUY"}, //from

new int[]{R.id.textView10, R.id.textView11, R.id.textView12}); //to

listView.setAdapter(adapter);

progressDialog.dismiss();

Toast.makeText(getActivity(), ban, Toast.LENGTH_SHORT).show();

}

}

网友答案:

You are calling serializer.read(Ratelist.class, is) but <ratelist> isn't the root of the XML.

Make another class Root with the title, url, and ratelist elements, use that in the call to serializer.read(), and you should be all set.

Also, you aren't using the annotations correctly and accounting for repeating tags by using lists.

@Root(name="root")
public class Root {

    @Element
    private String title;

    @Element
    private String url;

    @Element
    private Ratelist ratelist;

    public Ratelist getRatelist() {
        return ratelist;
    }
}


@Element(name="ratelist")
public class Ratelist {

    @Attribute
    private String updated;

    @ElementList
    private List<City> cities;

    public String getUpdated() {
        return updated;
    }

    public List<City> getCities() {
        return cities;
    }
}


@Element(name="city")
public class City {

    @Attribute
    private String name;

    @ElementList
    private List<Item> items;

    public String getName() {
        return name;
    }

    public List<Item> getItems() {
        return items;
    }
}


@Element(name="item")
public class Item {

    @Attribute
    private String buy;

    @Attribute
    private String sell;

    @Attribute
    private String type;

    public String getBuy() {
        return buy;
    }

    public String getSell() {
        return sell;
    }

    public String getType() {
        return type;
    }
}

    InputStream is2 = connection.getInputStream();
    Serializer serializer1 = new Persister();
    Root root = serializer1.read(Root.class, is2);
    Ratelist ratelist = root.getRatelist();

    Log.d(TAG, "Updated: " + ratelist.getUpdated());

    for (City city : ratelist.getCities()) {

        Log.d(TAG, "City: " + city.getName());

        for (Item item : city.getItems()) {
           Log.d(TAG, "Buy: " + item .getBuy());
           Log.d(TAG, "Sell: " + item .getSell());
           Log.d(TAG, "Type: " + item .getType());
        }
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: