【问题标题】:Extract string between xml tags in android without parsing the xml在android中提取xml标签之间的字符串而不解析xml
【发布时间】:2013-03-14 05:13:58
【问题描述】:

我有一个像这样的非常简单的 XML:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://icacec.com/">TRUE,Hrithik Sharma,201301-11</string>

现在,我只想在 3 个单独的变量中提取 TRUEHrithik Sharma201301-11 .

我可以像这样根据 "," 拆分字符串:

String[] parts = responseBody.split(",");
String response_auth = parts[0];
String user_name = parts[1];    
String user_number=parts[2];

但我面临的问题是,字符串没有被独立提取。更准确地说,没有 XML 标记。我应该如何做到这一点?

【问题讨论】:

  • 先剥离标签?
  • 使用indexOfsubstring函数解析当前字符串来设置测试
  • 子字符串可能不会被使用,因为标签之间的内容可能会有所不同
  • 对代码的任何帮助都会有很大帮助!
  • @kittu88 : 为什么子字符串方法不起作用?试试这个方法:首先提取 indexof com/"&gt; substring 和 indexof &lt;/s substring 然后使用 substring 方法提取最终字符串

标签: java android regex string split


【解决方案1】:

这可以解决这个简单的情况,但不解析你打算用其他条件做什么?

public static void main(String[] args) {
    String raw = "<string xmlns=\"http://icacec.com/\">TRUE,Hrithik Sharma,201301-11</string>";
    raw = raw.substring(0, raw.lastIndexOf("<"));
    raw = raw.substring(raw.lastIndexOf(">") + 1, raw.length());
    String [] contents = raw.split(",");
    for (String txt : contents)
        System.out.println(txt);
}

【讨论】:

  • 毫无疑问,这是各种 xml 数据的解决方案。我给出的只是那个特定的字符串
【解决方案2】:

除非你真的知道你在 XML 中得到了什么,否则这是非常不鼓励的

响应正文:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://icacec.com/">TRUE,Hrithik Sharma,201301-11</string>

代码:

String[] parts = responseBody.split(">");
String tagsFirst= parts[0];
String usefull = parts[2];    

String[] actualBody = usefull.split("<");

String content = actualBody[0];
String[] contentParts=content.split(",");
//now you can have the three parts:
String truefalse=contentParts[0];
String name=contentParts[1];
String date=contentParts[2];

【讨论】:

  • 字符串 truefalse=content[0];给出一个错误,它说,表达式的类型必须是数组类型,但它解析为字符串
  • 你要使用哪个字符串?我提到的那个??..在内容上设置断点并调试并告诉我你在actualBody数组中得到了什么
  • @Nezam:OP 是对的。 contentString,但您使用数组索引 [] 来访问它。
  • @nhahtdh 我很糟糕,我没有注意到错误.. 叹息我是第一个回答的人......虽然可能很匆忙:微笑:
【解决方案3】:

试试这个正则表达式 -

"<string xmlns=\"http://icacec.com/\">(.+),(.+),(.+)</string>"

捕获组 1、2 和 3 将包含您的三个项目,即:

Pattern pattern = Pattern.compile("<string xmlns=\"http://icacec.com/\">(.+),(.+),(.+)</string>");
Matcher matcher = pattern.matcher("<string xmlns=\"http://icacec.com/\">TRUE,Hrithik Sharma,201301-11</string>");

if(matcher.matches())
{
    System.out.println("Bool: " + matcher.group(1));
    System.out.println("Name: " + matcher.group(2));
    System.out.println("Date: " + matcher.group(3));
}

【讨论】:

    【解决方案4】:

    尝试拆分如下:

    String[] strTemp = strXMLOutput.split("<TagName>");
    strTemp = strTemp[1].split("</TagName>");
    String strValue = strTemp[0]
    

    100% 会起作用。

    【讨论】:

      【解决方案5】:

      你可以试试这个:

      import java.util.regex.Matcher;
      import java.util.regex.Pattern;
      
      
      public class Test {
      
          public static void main(String[] args) {
              String responseBody = null;
              String inputString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://icacec.com/\">TRUE,Hrithik Sharma,201301-11</string>";
              String regex = "<string[^>]*>(.+?)</string\\s*>";
              Pattern pattern = Pattern.compile(regex);
              Matcher matcher = pattern.matcher(inputString);
              while(matcher.find()) {
                  responseBody = matcher.group(1);
                  System.out.println(responseBody);
              }
      
              String[] splits = responseBody.split(",");
              System.out.println(splits[0]);/*prints TRUE*/
              System.out.println(splits[1]);/*prints Hrithik Sharma*/
              System.out.println(splits[2]);/*201301-11*/
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-22
        • 1970-01-01
        • 2012-02-28
        • 1970-01-01
        相关资源
        最近更新 更多