有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓上的java改造2和xml

我必须创建什么类来接收一个XML元素

我正在从API接收XML:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cat</string>

我有课:

@Root(strict = false)
public class Translation {

    @Element(name = "string")
    private String string;

    public String getString(){
        return string;
    }
    public Translation() {

    }
}

以及捕捉错误:

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=string, required=true, type=void) on field 'string' private java.lang.String com.antonioleiva.mvpexample.app.main.Utils.Network.Translation.Translation.string for class com.antonioleiva.mvpexample.app.main.Utils.Network.Translation.Translation at line 1

共 (2) 个答案

  1. # 1 楼答案

    如果您习惯于对xml进行改进,那么就将xml转换为json

     public class Main {
    
    public static int PRETTY_PRINT_INDENT_FACTOR = 4;
    public static String TEST_XML_STRING =
        "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
    
    public static void main(String[] args) {
        try {
            JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
            String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
            System.out.println(jsonPrettyPrintString);
        } catch (JSONException je) {
            System.out.println(je.toString());
        }
    }
    }
    

    输出将是

    {"test": {
        "attrib": "moretest",
        "content": "Turn this to JSON"
    }}
    
  2. # 2 楼答案

    你可以看看这个link。这个示例项目清楚地说明了如何在改造中使用SOAP