gpt4 book ai didi

java - 使用 xmlpullparser 解析 xml 时在列表中获取相同的值

转载 作者:行者123 更新时间:2023-11-30 03:04:55 25 4
gpt4 key购买 nike

我正在尝试解析 Assets 文件夹中存在的 xml 文件。一切看起来都很好并且没有错误,但问题是它为 xml 中的每个节点的列表提供相同的值。因为我觉得它覆盖了以前的值。我被困在这里。即使经过大量谷歌搜索也找不到任何解决方案。任何帮助将不胜感激。

下面是代码:-

Product.java

package com.example.webservicedemo;

public class product {


private static String productname="";
private static String productcolor="";
private static String productquantity="";



public static String getProductname() {
return productname;
}
public static void setProductname(String productname) {
product.productname = productname;
}
public static String getProductcolor() {
return productcolor;
}
public static void setProductcolor(String productcolor) {
product.productcolor = productcolor;
}
public static String getProductquantity() {
return productquantity;
}
public static void setProductquantity(String productquantity) {
product.productquantity = productquantity;
}
}

下面是解析器的代码:-productXmlPullParser.java

package com.example.webservicedemo;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import android.content.res.AssetManager;
import android.widget.Toast;
import com.example.webservicedemo.*;
public class productXmlPullParser {

static final String KEY_PRODUCT = "product";
static final String KEY_NAME = "productname";
static final String KEY_COLOR = "productcolor";
static final String KEY_QUANTITY = "productquantity";


public static List<product> getproductsFromFile(Context ctx) {
String tagname;
List<product> Products;
Products = new ArrayList<product>();

product curproduct = null;
String curText = "";
InputStream is = null;
try {
is = ctx.getAssets().open("temp.xml");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();

}


try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();


BufferedReader reader = new BufferedReader(new InputStreamReader(is));

xpp.setInput(reader);

int eventType = xpp.getEventType();

while (eventType != XmlPullParser.END_DOCUMENT) {
tagname = xpp.getName();

switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase(KEY_PRODUCT)) {

curproduct = new product();
}
break;

case XmlPullParser.TEXT:
//grab the current text so we can use it in END_TAG event
curText = xpp.getText();


break;

case XmlPullParser.END_TAG:

if (tagname.equalsIgnoreCase(KEY_PRODUCT)) {
// if </site> then we are done with current Site
// add it to the list.
Products.add(curproduct);
// Toast.makeText(ctx, curproduct+"",3000).show();

} else if (tagname.equalsIgnoreCase(KEY_NAME)) {
// if </name> use setName() on curSite
product.setProductname(curText);
Toast.makeText(ctx, curText+"",3000).show();

} else if (tagname.equalsIgnoreCase(KEY_COLOR)) {
// if </link> use setLink() on curSite
curproduct.setProductcolor(curText);
Toast.makeText(ctx, curText+"",3000).show();
} else if (tagname.equalsIgnoreCase(KEY_QUANTITY)) {
// if </about> use setAbout() on curSite
curproduct.setProductquantity(curText);
Toast.makeText(ctx, curText+"",3000).show();

}

break;

default:
break;
}
//move on to next iteration
eventType = xpp.next();
}

} catch (Exception e) {
e.printStackTrace();
Toast.makeText(ctx, "error", 6000).show();
}

// return the populated list.
return Products;
}
}

下面是我的自定义数组适配器的代码:--

产品适配器

    package com.example.webservicedemo;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;



public class UsersAdapter extends ArrayAdapter<product> {

public UsersAdapter(Context context, int resource, List<product> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
}


@Override
public View getView(int pos, View convertView, ViewGroup parent){
RelativeLayout row = (RelativeLayout)convertView;
Log.i("Users", "getView pos = " + pos);
if(null == row){
LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (RelativeLayout)inflater.inflate(R.layout.listitem, null);
}

TextView Email = (TextView)row.findViewById(R.id.txtEmail);
TextView Password = (TextView)row.findViewById(R.id.txtPassword);
TextView Deviceid = (TextView)row.findViewById(R.id.txtDeviceid);
Email.setText(getItem(pos).getProductname());
Password.setText(getItem(pos).getProductcolor());
Deviceid.setText(getItem(pos).getProductquantity());

return row;

}

}

下面是我试图在列表中获取结果值的主要 Activity

MainActivity.java

package com.example.webservicedemo;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.example.webservicedemo.productXmlPullParser;;;

public class UserList extends Activity {
UsersAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xmlparsingdemo);


ListView lvUser=(ListView)findViewById(R.id.lvUsers);
mAdapter=new UsersAdapter(getApplicationContext(),R.layout.listitem,productXmlPullParser.getproductsFromFile(getApplicationContext()));
lvUser.setAdapter(mAdapter);
Toast.makeText(getApplicationContext(), mAdapter.getCount()+"",3000).show();

}
}

这是我要解析的 xml:--

temp.xml

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<productname>Jeans</productname>
<productcolor>red</productcolor>
<productquantity>5</productquantity>
</product>
<product>
<productname>Tshirt</productname>
<productcolor>blue</productcolor>
<productquantity>3</productquantity>
</product>
<product>
<productname>shorts</productname>
<productcolor>green</productcolor>
<productquantity>4</productquantity>
</product>

</products>

但输出类似于下面的截图:-

enter image description here

经过大量努力,我找不到问题所在。

如有任何帮助,我们将不胜感激。

最佳答案

您的类“产品”中存在问题。字段是静态的,每个字段包含所有实例的最后一个值。删除“static”修饰符,一切都会正常工作。

关于java - 使用 xmlpullparser 解析 xml 时在列表中获取相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21969112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com