如何格式化或解析SOAP原始对象

时间:2014-03-05 12:18:05

标签: java android ios regex soap

机器人

我在WSDL API中得到以下响应:

  

一个:10:{S:11: “sso_user_id”; S:6: “123345”; S:9: “姓名”; S:0: “”,S:8: “姓氏”; S:0 : “”; S:5: “abono”; S:0: “”; S:4: “散列”; S:32: “c2ff5bc4598d02160b57e2b3f28a3e0e”; S:5: “令牌”; S:32: “2da9ba3bcc52fdb047c3da5d91e3cdbd” ; S:5: “登录”; S:23: “sandor.fekete@inform.hu”; S:6: “曲奇”; S:232: “”; S:6: “访问”;一个:1: {S:4: “角色”; S:2: “否”;} S:5: “错误”; S:0: “”;}

我在响应标记之间取了字符串

现在这里是关于响应结构的信息:

S:11: “sso_user_id”; -  sso_user_id是长度为11个字符的密钥。

s:6:“123345”; - 123345是长度为6个字符的sso_user_id的值。

现在任何人都可以帮助我 PARSE FORMAT 或者让 REGEXP 来制作正常的字符串或易于理解的字符串。

注意: - 这不是JSON STRING。

IOS代码和逻辑也非常受欢迎。

1 个答案:

答案 0 :(得分:1)

我设法做到了:

public static void parseSOAPPrimitiveObject(String input, Object output)
        throws NumberFormatException, IllegalArgumentException,
        IllegalAccessException, InstantiationException {

    Class theClass = output.getClass();
    Field[] fields = theClass.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        Type type = fields[i].getType();
        fields[i].setAccessible(true);

        // detect String
        if (fields[i].getType().equals(String.class)) {
            String tag = "" + fields[i].getName() + "";
            if (input.contains(tag)) {
                String strValue = "";
                strValue = input.substring(input.indexOf(tag)
                        + tag.length() + 2);

                if (getValueLength(strValue) > 0) {
                    strValue = getValue(strValue);
                } else {
                    strValue = "";
                }

                fields[i].set(output, strValue);
            }
        }

        // detect int or Integer
        if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
            String tag = "" + fields[i].getName() + "";
            if (input.contains(tag)) {
                String strValue = "";
                strValue = input.substring(input.indexOf(tag)
                        + tag.length() + 2);
                fields[i].set(output, getValueLengthInt(strValue));
            }
        }
    }
}

public static String getValue(String substring) {

    String str = new String(substring);

    final Pattern pattern = Pattern.compile("\"(.+?)\"");
    final Matcher matcher = pattern.matcher(str);
    matcher.find();

    return matcher.group(0);
}

public static int getValueLength(String substring) {

    final Pattern pattern = Pattern.compile(":(.+?):");
    final Matcher matcher = pattern.matcher(substring);
    matcher.find();
    int count = 0;

    count = Integer.parseInt(matcher.group(1));

    return count;
}

public static int getValueLengthInt(String substring) {

    final Pattern pattern = Pattern.compile(":(.+?);");
    final Matcher matcher = pattern.matcher(substring);
    matcher.find();
    int count = 0;

    count = Integer.parseInt(matcher.group(1));

    return count;
}

对象类:

public class SSOuser {

String  sso_user_id;
String  firstname;
String  lastname;
String  abono;
String  hash;
String  token;
String  login;

String  cookie;

String  role;
String  error;

}

显示数据:

SSOuser ouser = getFormatedResponseData(result.toString());
String finalValues = "";                                    
finalValues = finalValues + "\n" + "fname= " + ouser.firstname;
finalValues = finalValues + "\n" + "lastname= " + ouser.lastname;
finalValues = finalValues + "\n" + "abono= " + ouser.abono;
finalValues = finalValues + "\n" + "hash= " + ouser.hash;
finalValues = finalValues + "\n" + "role= " + ouser.role;
finalValues = finalValues + "\n" + "sso_user_id= " + ouser.sso_user_id;
finalValues = finalValues + "\n" + "token= " + ouser.token;
finalValues = finalValues + "\n" + "login= " + ouser.login;
finalValues = finalValues + "\n" + "error= " + ouser.error;
// finalValues=finalValues+
// "\n"+"cookie= " + output.cookie;
final String val = finalValues;

runOnUiThread(new Runnable() {
    public void run() {
    txtValues.setText(val);}});