有 Java 编程相关的问题?

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

java从长字符串中提取文本并存储在变量中

我正在为学校做一个安卓项目。我们在editText中得到转义xml,我可以从editText中读取文本,但我想从中获取值。例如,在editText中:

<ResponseSummary> 
<ID>spq789pie</ID> 
<No>305</No> 
<ItemCount>1</ItemCount> 
<Total>1.00</Total> 
</ResponseSummary> 

我计划做的是String response = edtResponse.getText().toString()从editText中捕获txt。我想做的是这样的:

string test = (some expression that searches text and return a value text from a string)
if (test = "ID"){setID(ID);}
else if(No = "No"){setNo(No);}
else if(ItemCount = "ItemCount"){setItemCount(ItemCount);}
else if(Total = "Total"){setTotal(Total);}

基于上述xml,我希望最后的值如下所示:

ID = spq789pie
No = 305
ItemCount = 1
Total = 1.00

如果你能帮我,我会非常感激。我读过regex的作品,但我对regex不是很在行。如果有其他方法,我也会这么做

谢谢你的帮助


共 (3) 个答案

  1. # 1 楼答案

    你可以用这个:

    Matcher m = Pattern.compile("(?:<ResponseSummary>|(?!^)\\G)\\s*"
                              + "&lt;(?<key>(?>[^&]++|&(?!gt;))*)&gt;"
                              + "(?<value>(?>[^&]++|&(?!lt;))*)"
                              + "&lt;/\\1&gt;").matcher(txt);
    while (m.find()) {
        System.out.println(m.group("key") + " = " + m.group("value"));
    }
    

    \G锚代表字符串的开始或最后一个匹配的结束。负前瞻(?!^)用于排除第一种情况

  2. # 2 楼答案

    如果希望将其作为字符串使用,而不是像savanto建议的那样使用XML,可以这样做(使用尽可能简单的正则表达式):

        String[] lines = response.split("[\n]");
            // you split the string to lines (new line char is the thing that splits the string)
    
        String[] line = null;
        String key = null;
        String closureKey = null;
        String strValue = null;
    
        for (int i = 0; i < lines.length; ++i) {
            line = lines[i].split("[&][lg][t][;]");
                // you split every single line with specific string:
                // first char is '&', second char is either 'l' or 'g'
                // third char is 't', forth is ';'
    
            key = line[1];
                // line array will start and end with an empty string so you take the element with index 1, NOT 0
    
            if (i == 0)
            {
                // do so something with the parent key
            }
            else if (i == lines.length - 1)
            {
                // check if parent closure key is correct
            }
            else
            {
                strValue = line[2];
                closureKey = line[3];
    
                if (key.equals("ID"))
                {
                    // do something; value is in strValue
                }
                else if (key.equals("ItemCount"))
                {
                    // do something; value is in strValue
                }
                //and so on...
    
    
                if (!closureKey.equals("/" + key))
                {
                    // something is wrong with this child's closure key
                }
            }
        }
    
  3. # 3 楼答案

    由于您的数据已经是XML格式,我建议使用XML解析器,而不是使用正则表达式。Android有一个名为XmlPullParser的内置解析器,非常容易使用

    下面是关于Android上XML解析的更深入的指南:Parsing XML Data | Android Developers