2010-04-20 4 views
3

다른 프로그램에서 생성 된 파일에서 non-xml 태그를 제거해야합니다.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 

어떻게 자바에서 비 XML 텍스트를 쉽게 제거하는 방법 :

파일은 다음과 같은 몇 가지입니까?

답변

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

'xmlIndex'에서 1을 더하거나 빼야 할 수도 있습니다. –

+0

PHP에서 이것을 찾고 있었지만 여전히 도움이되었습니다. 잘 했어. – IamFace

4

이것은 마치 직접적인 HTTP 출력처럼 보입니다 ... 그래서 첫 번째 두 개의 연속 된 줄 바꿈 (아마도 앞에있는 캐리지 리턴이 있음)을 검색하면 필터링 할 접두사의 끝을 알 수 있습니다.

+0

더 많은 힌트를 제공하는 'Content-Length' 헤더가 없습니다. – McDowell

관련 문제