大家好我尝试解析存储在我的android项目
中的字符串变量中的xml数据MainActivity
public class MainActivity extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetPropertyService gpservice= new GetPropertyServiceImpl();
Property p;
try {
p=gpservice.getProperty();
List<PropertyDetails> s = p.getProperty();
Log.e("SIZE OF S IS",""+s.size());
//t.setText(""+s.size());
for(int i=0;i<s.size();i++)
{
Log.e("PR_ID",s.get(i).getPR_ID());
Log.e("PR_NAME",s.get(i).getPR_NAME());
Log.e("IMAGE",s.get(i).getIMAGE());
Log.e("PR_UNITS",s.get(i).getPR_UNITS());
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
GetPropertyService .java
public interface GetPropertyService
{
Property getProperty() throws Exception;
}
GetPropertyServiceImpl.java
public class GetPropertyServiceImpl implements GetPropertyService{
String smp = "<Property><PropertyDetails><PR_ID>58</PR_ID><PR_NAME>SKY LINE</PR_NAME><IMAGE>~/developer_admin/propertyimages/090520131119518_MImage.png</IMAGE><PR_UNITS>272</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>59</PR_ID><PR_NAME>Winter Mist</PR_NAME><IMAGE>~/developer_admin/propertyimages/200520131154299_MImage.jpg</IMAGE><PR_UNITS>35</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>63</PR_ID><PR_NAME>Q</PR_NAME><IMAGE>~/developer_admin/propertyimages/07062013953201_MImage.png</IMAGE><PR_UNITS>6</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>64</PR_ID><PR_NAME>dfytfy</PR_NAME><IMAGE>~/developer_admin/propertyimages/220520131737864_MImage.jpg</IMAGE><PR_UNITS>16</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>65</PR_ID> <PR_NAME>rtert</PR_NAME><IMAGE>~/developer_admin/propertyimages/270520131435682_MImage.jpg</IMAGE><PR_UNITS>16</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>66</PR_ID><PR_NAME>123</PR_NAME><IMAGE>~/developer_admin/propertyimages/04062013955569_MImage.jpg</IMAGE><PR_UNITS>30</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>67</PR_ID><PR_NAME>GFHGH</PR_NAME><IMAGE>~/developer_admin/propertyimages/04062013958563_MImage.jpg</IMAGE><PR_UNITS>16</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>68</PR_ID><PR_NAME>1234R</PR_NAME><IMAGE>~/developer_admin/propertyimages/04062013101373_MImage.gif</IMAGE><PR_UNITS>30</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>69</PR_ID><PR_NAME>GFHGH11111</PR_NAME><IMAGE>~/developer_admin/propertyimages/04062013103287_MImage.jpg</IMAGE><PR_UNITS>16</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>70</PR_ID><PR_NAME>HJKJK</PR_NAME><IMAGE>~/developer_admin/propertyimages/04062013109884_MImage.png</IMAGE><PR_UNITS>35</PR_UNITS></PropertyDetails><PropertyDetails><PR_ID>71</PR_ID><PR_NAME>fghfg</PR_NAME><IMAGE>~/developer_admin/propertyimages/06062013172473_MImage.jpg</IMAGE><PR_UNITS>16</PR_UNITS></PropertyDetails></Property>";
private Property domainObject;
@Override
public Property getProperty() throws Exception {
Serializer serializer = new Persister();
InputStream is = new ByteArrayInputStream(smp.getBytes());
domainObject = serializer.read(Property.class,
is, false);
return domainObject;
}
}
PropertyDetails.java
@Root
public class PropertyDetails extends BaseResponse{
/**
*
*/
private static final long serialVersionUID = 1L;
@Element
private String PR_ID;
@Element
private String PR_NAME;
@Element
private String IMAGE;
@Element
private String PR_UNITS;
public String getPR_ID() {
return PR_ID;
}
public void setPR_ID(String pR_ID) {
PR_ID = pR_ID;
}
public String getPR_NAME() {
return PR_NAME;
}
public void setPR_NAME(String pR_NAME) {
PR_NAME = pR_NAME;
}
public String getIMAGE() {
return IMAGE;
}
public void setIMAGE(String iMAGE) {
IMAGE = iMAGE;
}
public String getPR_UNITS() {
return PR_UNITS;
}
public void setPR_UNITS(String pR_UNITS) {
PR_UNITS = pR_UNITS;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
Property.java
@Root
public class Property extends BaseResponse{
private static final long serialVersionUID = 1L;
@ElementList (required=false)
private List< PropertyDetails > property;
public List<PropertyDetails> getProperty() {
return property;
}
public void setProperty(List<PropertyDetails> property) {
this.property = property;
}
}
BaseResponse.java
@Root
public class BaseResponse implements Serializable
{
private static final long serialVersionUID = 1L;
}
这些是我的设置,但我无法获取列表。什么我失踪了?任何人都可以指出我
答案 0 :(得分:1)
“Property”!= property,XML区分大小写,因此您需要元素列表具有@ElementList(entry =“Property”,required = false,inline = true)