传说, xml流行,问问。plz,give a simple!

解决方案 »

  1.   

    用msxml可以解析xml的。xml本身没有什么神奇的。要结合具体应用。
      

  2.   

    BOOL CWeatherForecast::WriteWeatherIntoFile(CString strWeather,CString strFileName)
    {
    COleDateTime odtDate;
    CString strPath,strPathFile; 
    TCHAR * ptPath;
    CString strMonth,strDay;

    ptPath=strPath.GetBuffer(_MAX_PATH);
    ::GetModuleFileName(AfxGetInstanceHandle(),ptPath,_MAX_PATH);
    strPath.ReleaseBuffer();
    ptPath=NULL; strPathFile=strPath.Left(strPath.ReverseFind(_T('\\'))+1)+strFileName; if ( !PathFileExists (strPathFile) )
    return FALSE ; if( FALSE == ExtractDateAndMonth( strDay , strMonth , strWeather ))
    {
    return FALSE;
    }
    //The following codes are used to generate COleDateTime object
    if( strMonth != CString("1") || COleDateTime::GetCurrentTime().GetMonth() != 12 )
    {
    odtDate.SetDate( COleDateTime::GetCurrentTime().GetYear() , _ttoi(strMonth), atoi(strDay) );
    }
    else{
    odtDate.SetDate( COleDateTime::GetCurrentTime().GetYear() +1 , _ttoi(strMonth), atoi(strDay) );
    }
    TRACE0(odtDate.Format()); try
    {
    BOOL bUpdated = FALSE ;
    IXMLDOMDocumentPtr pXMLDom;
    HRESULT hr;

    hr= pXMLDom.CreateInstance(__uuidof(DOMDocument40));
    if (FAILED(hr)) 
    {
    return FALSE;
    }

    pXMLDom->async = VARIANT_FALSE; // default - true,
    pXMLDom->validateOnParse= VARIANT_FALSE;

    if(pXMLDom->load((_bstr_t)strPathFile)!=VARIANT_TRUE)
    {
    return FALSE;
    }

    IXMLDOMNodeListPtr pnl = pXMLDom->selectNodes("/weather/*");
    IXMLDOMNodePtr pNode=NULL;
    for (int i=0; i<pnl->length; i++)
    {
    pNode = pnl->item[i];
    COleDateTime odtXMLDate(pNode->attributes->getNamedItem("date")->nodeTypedValue);

    if(odtXMLDate==odtDate)
    {
    pNode->text=(_bstr_t)strWeather;
    bUpdated = TRUE;
    break;
    }
    } if ( ! bUpdated ){
    IXMLDOMElementPtr pe;
    pe = pXMLDom->createElement("weatherreport");

    if (pe != NULL)
    {
    // Add newline + tab for indentation.
    pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));

    pe->text = (_bstr_t)strWeather;
    IXMLDOMAttributePtr pa;
    pa = pXMLDom->createAttribute("date");
    if (pa != NULL) 
    {
    _variant_t varDate;
    varDate.ChangeType(VT_DATE);
    varDate.date=odtDate;
    //memcpy( &varDate,&odtDate,sizeof() );

    pa->value =varDate ;
    pe->setAttributeNode(pa);
    pa.Release();
    }

    pXMLDom->documentElement->appendChild(pe); // Add newline + tab for indentation.
    pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n"));
    pe.Release();
    }

    } if(FAILED(pXMLDom->save((_bstr_t)strPathFile)))
    {
    return  FALSE;
    }

    pXMLDom.Release();
    pnl.Release();

    }
    catch(_com_error &ex)
    {
    TRACE(ex.ErrorMessage());
    }

    return TRUE;
    }