3

別のプログラムによって生成されたファイルから非xmlタグを削除する必要があります。

ファイルは次のようなものです。

Executing Command - Blah.exe ...
-----Command Output-----
HTTP/1.1 200 OK
Connection: close
Content-Type: text/xml

<?xml version="1.0"?>
<testResults>
  <finalCounts>
    <right>7</right>
    <wrong>4</wrong>
    <ignores>0</ignores>
    <exceptions>0</exceptions>
  </finalCounts>
</testResults>

Exit-Code: 15

Javaで非xmlテキストを簡単に削除するにはどうすればよいですか?

4

2 に答える 2

8
// getContent() returns the complete text to strip.
//
String s = getContent();

// Find the start of the XML content using the <?xml prefix.
//
int xmlIndex = s.indexOf( "<?xml" );

// Strip the non-XML header.
//
s = s.substring( xmlIndex );

// Find the last closing angle-bracket; should indicate end of the XML.
//
xmlIndex = s.lastIndexOf( ">" );

// Strip everything after the closing angle-bracket.
//
s = s.substring( 0, xmlIndex );
于 2010-04-20T20:26:43.210 に答える
4

これは直接HTTP出力のように見えます...したがって、最初の2つの連続する改行をスキャンするだけで(おそらくそれらの前にキャリッジリターンがあります)、フィルターで除外するプレフィックスの終わりがわかります。

于 2010-04-20T20:29:19.310 に答える