Java 从XML文件中删除头的简单方法

Java 从XML文件中删除头的简单方法,java,xml,text-processing,Java,Xml,Text Processing,我需要从另一个程序生成的文件中删除非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>

我需要从另一个程序生成的文件中删除非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
正在执行命令-Blah.exe。。。
-----命令输出-----
HTTP/1.1200ok
连接:关闭
内容类型:text/xml
7.
4.
0
0
出境代码:15
如何在java中轻松删除非xml文本?

//getContent()返回要剥离的完整文本。
// 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 );
// 字符串s=getContent();
//使用查找XML内容的开头这看起来像是直接HTTP输出。。。因此,只需扫描前两个连续的换行符(可能前面有回车符)就可以得到要过滤掉的前缀的结尾。

您可能需要从
xmlIndex
中添加或减去1。遗憾的是,没有
内容长度
标题来提供更多提示。