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

2024-03-03

我需要从另一个程序生成的文件中删除非 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文本?


// 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 );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 XML 文件中删除标头的简单方法 的相关文章

随机推荐