【问题标题】:Parse a line using Pattern.compile使用 Pattern.compile 解析一行
【发布时间】:2012-03-23 22:12:49
【问题描述】:

我正在尝试在 Java 中解析以下行 myline 并且它不断抛出 null 值。

这是我尝试获取“000000010”的方法。

myline = "<status> <id>000000010</id> <created_at>2012/03/11</created_at> <text>@joerogan Played as Joe Savage Rogan in Undisputed3 Career mode, won Pride GP, got UFC title shot against Shields, lost 3 times, and retired</text> <retweet_count>0</retweet_count> <user> <name>Siggi Eggertsson</name> <location>Berlin, Germany</location> <description></description> <url>http://www.siggieggertsson.com</url> </user></status>"
p = Pattern.compile("(?i)<id.*?>(.+?)</id>", Pattern.DOTALL);
m = regex.matcher(myline);
id =m.group(1);

有什么建议吗?

【问题讨论】:

  • 使用正则表达式从 XML 文档中提取数据是个坏主意。查看 XML 解析器。
  • @user1289238 请您接受答案,谢谢。

标签: java xml parsing design-patterns matcher


【解决方案1】:

强烈推荐使用 XML 解析器。 Java中内置了一个,这是您问题的示例解决方案。为简单起见省略了异常处理程序。

DocumentBuilderFactory factory = DocumentBuilderFactory
        .newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
String input = "<status> <id>000000010</id> <created_at>2012/03/11</created_at> <text>@joerogan Played as Joe Savage Rogan in Undisputed3 Career mode, won Pride GP, got UFC title shot against Shields, lost 3 times, and retired</text> <retweet_count>0</retweet_count> <user> <name>Siggi Eggertsson</name> <location>Berlin, Germany</location> <description></description> <url>http://www.siggieggertsson.com</url> </user></status>";
Document document = builder.parse(new InputSource(new StringReader(
        input)));
String value = document.getElementsByTagName("id").item(0)
        .getTextContent();
System.out.println(value);

【讨论】:

  • 问题是我实际上不是在处理 XML 文件,它是一个包含 XML 输入的文本文件。所以我不认为使用 XML 解析器会起作用吗?
【解决方案2】:

您不应该首先使用正则表达式来解析 XML。

但除此之外,您没有正确使用正则表达式。实例化一个matcher 对象是不够的,你还需要告诉它做一些事情:

if (m.find())
{
    id = m.group(1);
}

【讨论】:

    【解决方案3】:

    本网站可能会为您提供一些关于使用 Java 解析 XML 的信息 - http://www.java-samples.com/showtutorial.php?tutorialid=152

    【讨论】:

      【解决方案4】:

      这行得通

      String myline = "<status> <id>000000010</id> <created_at>2012/03/11</created_at> <text>@joerogan Played as Joe Savage Rogan in Undisputed3 Career mode, won Pride GP, got UFC title shot against Shields, lost 3 times, and retired</text> <retweet_count>0</retweet_count> <user> <name>Siggi Eggertsson</name> <location>Berlin, Germany</location> <description></description> <url>http://www.siggieggertsson.com</url> </user></status>";
      Pattern p = Pattern.compile(".*<id>(.+)</id>.*");
      Matcher m = p.matcher(myline);
      if (m.matches()) {
          String id = m.group(1);
          System.out.println(id);
      }
      

      [编辑:]这也有效,而且更好:

      String myline = "<status> <id>000000010</id> <created_at>2012/03/11</created_at> <text>@joerogan Played as Joe Savage Rogan in Undisputed3 Career mode, won Pride GP, got UFC title shot against Shields, lost 3 times, and retired</text> <retweet_count>0</retweet_count> <user> <name>Siggi Eggertsson</name> <location>Berlin, Germany</location> <description></description> <url>http://www.siggieggertsson.com</url> </user></status>";
      Pattern p = Pattern.compile("<id>(.+)</id>");
      Matcher m = p.matcher(myline);
      if (m.find()) {
          String id = m.group(1);
          System.out.println(id);
      }
      

      【讨论】:

      • 如果字符串中有多个&lt;id&gt;,如果&lt;id&gt;标签有任何属性,或者标签的内容包含换行符,这两种方法都会失败。
      • 当然,我完全同意您评论中的“您不应该使用正则表达式来解析 XML”部分
      猜你喜欢
      • 2013-10-20
      • 2013-03-18
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多