void StringSplit(CString source, CStringArray& dest, CString division)
{
if(source.IsEmpty())
{
}
else
{
int pos = source.Find(division);
if(pos == -1)
{
dest.Add(source);
}
else
{
CString tmp = source.Left(pos);
dest.Add(tmp);
source = source.Mid(pos+1);
StringSplit(source,dest,division);
}
}
}
使用:本示例用于分割多个xml
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
CString src = "<?xml version='1.0' encoding='utf-8' ?><UpsAlarm><EncoderID>3004</EncoderID><AlarmID>6091</AlarmID><AlarmType>9512</AlarmType><UpsSn>866104025728984</UpsSn><AlarmTime>2017/2/28 15:10:50</AlarmTime></UpsAlarm><?xml version='1.0' encoding='utf-8' ?><UpsAlarmConfirmNotify><UpsSn>866104025728984</UpsSn><AlarmList><AlarmID>6091</AlarmID><AlarmID>6077</AlarmID></AlarmList><NotifyTime>2017/2/28 15:10:52</NotifyTime></UpsAlarmConfirmNotify><?xml version='1.0' encoding='utf-8' ?><UpsAlarm><EncoderID>3004</EncoderID><AlarmID>6091</AlarmID><AlarmType>9512</AlarmType><UpsSn>866104025728984</UpsSn><AlarmTime>2017/2/28 15:10:50</AlarmTime></UpsAlarm><?xml version='1.0' encoding='utf-8' ?><UpsAlarmConfirmNotify><UpsSn>866104025728984</UpsSn><AlarmList><AlarmID>6091</AlarmID><AlarmID>6077</AlarmID></AlarmList><NotifyTime>2017/2/28 15:10:52</NotifyTime></UpsAlarmConfirmNotify><?xml version='1.0' encoding='utf-8' ?><UpsAlarmConfirmNotify><UpsSn>866104025728984</UpsSn><AlarmList><AlarmID>6091</AlarmID><AlarmID>6077</AlarmID></AlarmList><NotifyTime>2017/2/28 15:10:52</NotifyTime></UpsAlarmConfirmNotify>";
CStringArray strarry;
StringSplit(src,strarry,"<?xml");
int nSize = strarry.GetSize();
CString tmp;
for (int i=1;i<nSize;i++)
{
CString subUrl="<"+strarry.GetAt(i);
TRACE("cnt:%d:%s\n",i,subUrl) ;
}
return 0;
}