1

次の行、myline を Java で解析しようとしていますが、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);

何かアドバイス?

4

4 に答える 4

3

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);
于 2012-03-23T22:44:44.843 に答える
2

そもそも、正規表現を使用して XML を解析するべきではありません。

しかし、それとは別に、正規表現を正しく使用していません。オブジェクトをインスタンス化するだけでは十分ではmatcherありません。何かを行うように指示する必要もあります。

if (m.find())
{
    id = m.group(1);
}
于 2012-03-23T22:18:30.980 に答える
0

これは動作します

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);
}
于 2012-03-23T22:26:08.583 に答える
0

この Web サイトは、Java を使用した XML の解析に関する情報を提供する場合があります - http://www.java-samples.com/showtutorial.php?tutorialid=152

于 2012-03-23T22:19:06.080 に答える