如何使用SimpleXML 2.7框架序列化android.location.address对象?

时间:2013-09-11 23:08:34

标签: android xml-serialization simple-framework

我正在尝试使用android.location.Address序列化Simple 2.7.1对象。

我尝试过创建转换器......

public class AddressConverter implements Converter<Address> {

@Override
public Address read(InputNode node) throws Exception {
...
//Not included because this part works!!!!

}

@Override
public void write(OutputNode node, Address addr) throws Exception {
node.setAttribute("Locale", addr.getLocale().toString().replace("_", "-"));

OutputNode child = null;

if (addr.getMaxAddressLineIndex() > 0) {
    for (int index = 0; index <= addr.getMaxAddressLineIndex(); index++) {
    child = node.getChild("AddressLine");

    child.setAttribute("Index", String.valueOf(index));
    child.setValue(addr.getAddressLine(index));
    }
}

if (addr.getFeatureName() != null && addr.getFeatureName().length() > 0) {
    child = node.getChild("FeatureName");
    child.setValue(addr.getFeatureName());
    child.commit();
}

if (addr.getPremises() != null && addr.getPremises().length() > 0) {
    child = node.getChild("Premises");
    child.setValue(addr.getPremises());
    child.commit();
}

if (addr.getSubThoroughfare() != null && addr.getSubThoroughfare().length() > 0) {
    child = node.getChild("SubThoroughfare");
    child.setValue(addr.getSubThoroughfare());
    child.commit();
}

if (addr.getThoroughfare() != null && addr.getThoroughfare().length() > 0) {
    child = node.getChild("Thoroughfare");
    child.setValue(addr.getThoroughfare());
    child.commit();
}

if (addr.getSubLocality() != null && addr.getSubLocality().length() > 0) {
    child = node.getChild("SubLocality");
    child.setValue(addr.getSubLocality());
    child.commit();
}

if (addr.getLocality() != null && addr.getLocality().length() > 0) {
    child = node.getChild("Locality");
    child.setValue(addr.getLocality());
    child.commit();
}

if (addr.getSubAdminArea() != null && addr.getSubAdminArea().length() > 0) {
    child = node.getChild("SubAdminArea");
    child.setValue(addr.getSubAdminArea());
    child.commit();
}

if (addr.getAdminArea() != null && addr.getAdminArea().length() > 0) {
    child = node.getChild("AdminArea");
    child.setValue(addr.getAdminArea());
    child.commit();
}

if (addr.getPostalCode() != null && addr.getPostalCode().length() > 0) {
    child = node.getChild("PostalCode");
    child.setValue(addr.getPostalCode());
    child.commit();
}

if (addr.getCountryCode() != null && addr.getCountryCode().length() > 0) {
    child = node.getChild("CountryCode");
    child.setValue(addr.getCountryCode());
    child.commit();
}

if (addr.getCountryName() != null && addr.getCountryName().length() > 0) {
    child = node.getChild("CountryName");
    child.setValue(addr.getCountryName());
    child.commit();
}

if (addr.getLatitude() != 0) {
    child = node.getChild("Latitude");
    child.setValue(String.valueOf(addr.getLatitude()));
    child.commit();
}

if (addr.getLongitude() != 0) {
    child = node.getChild("Longitude");
    child.setValue(String.valueOf(addr.getLongitude()));
    child.commit();
}

if (addr.getPhone() != null && addr.getPhone().length() > 0) {
    child = node.getChild("Phone");
    child.setValue(addr.getPhone());
    child.commit();
}

node.commit();
}

@SuppressLint("DefaultLocale")
private Locale getLocale(String strLocale) {
String[] splits = strLocale.split("-", 3);

switch (splits.length) {
case 3:
    return new Locale(splits[0].toLowerCase(), splits[1].toUpperCase(), splits[2].toUpperCase());
case 2:
    return new Locale(splits[0].toLowerCase(), splits[1].toUpperCase());
default:
    return new Locale(splits[0].toLowerCase());
}
}

}

我无法让write来编写子元素。

有人可以帮助我吗?!

关于如何使用OutputNode创建子节点的任何建议也将非常感激,因为您可以看到我已经尝试过文档,但我无法弄清楚!!!

1 个答案:

答案 0 :(得分:0)

我发现这会对android.location.Address对象进行序列化,但会改变对象放在XML中的顺序!!!

我很抱歉,如果有人试图解决这个问题,我只能为浪费你的时间而道歉!