2012-03-23 13 views
1

次の行、Javaでmylineを解析しようとしていますが、null値が返され続けます。Pattern.compileを使用して行を解析します。

ここで私は '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

正規表現を使用してXML文書からデータを抽出することは悪い考えです。 XMLパーサーを調べます。 – pimaster

+0

@ user1289238あなたは答えを受け入れることができます、ありがとう。 – Adam

答えて

2

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

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

if (m.find()) 
{ 
    id = m.group(1); 
} 
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); 
} 
+0

''が文字列に複数ある場合、 ''タグに属性がある場合、またはタグの内容に改行が含まれている場合、これらの両方が失敗します。 –

+0

確かに、私は完全にあなたのコメントの一部をXMLを解析する正規表現を使用するべきではないと同意します –

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); 
+0

問題は私がXMLファイルを実際に扱っていないということです。だから私はXMLパーサーを使用してそれを動作するとは思わない? – user1289238

+1

それは、彼はちょうどあなたを示しています:) –

+0

ありがとう!それはちょうど魅力のように働く – user1289238

関連する問題