2011-12-28 3 views
0

어떻게 두 태그 사이에 CString을 추출 할 수 있습니까?태그 사이에 CString 추출

<tag1>My Text</tag1> 

시작점과 끝점을 계산하고 싶지 않고 Mid을 사용하고 싶습니다. STL을 사용하는 다른 쉬운 방법이 있을까요?

+0

왜 신의 초록 지구에서 MFC와 CString을 사용하여 HTML/XML을 파싱하고 있습니까? –

+0

XML 파서 라이브러리를 사용하지 않는 이유가 있습니까? –

답변

0

면책 조항 : 다음 아이디어는 badshould not be used in production code입니다. 테스트 해킹을 원한다고 가정하고 있습니다.

태그를 일치 시키려면 정규식을 사용하십시오. Microsoft provides this in CAtlRegExp. Visual Studio 2008 이상을 사용하는 경우 download ATL here. 그런 다음, 아래 사항을 코드에 myString를 제공

#include "atlrx.h" 
CAtlRegExp<> regex; 
VERIFY(REPARSE_ERROR_OK == regex.Parse("<tag1>(.*)</tag1>")); 

CAtlREMatchContext<> mc; 
if (!regex.Match(myString, &mc)) { 
    // no match found 
} else { 
    // match(es) found 
    for (UINT nGroupIndex = 0; nGroupIndex < mc.m_uNumGroups; ++nGroupIndex) { 
     const CAtlREMatchContext<>::RECHAR* szStart = 0; 
     const CAtlREMatchContext<>::RECHAR* szEnd = 0; 
     mc.GetMatch(nGroupIndex, &szStart, &szEnd); 
     ptrdiff_t nLength = szEnd - szStart; 
     CString text(szStart, nLength); 
     // now do something with text 
    } 
} 

면책 조항 2 : 당신은 정말 an XML parser library instead를 사용해야합니다.

+0

전체 html 파일이 아닌 사용자 정의 요소를 추출해야합니다. –