How to reset the "TiXmlText" of a "TiXmlEleme

Developer
Lazy Fox
2009-03-17
2013-05-20
  • Lazy Fox
    Lazy Fox
    2009-03-17

    I worte following code:

        TiXmlElement* pElm = pElm->FirstChildElement("name");
        cout << pElm2->GetText() << endl;  // It's OK, output is "Tom"

        // And now, I want to change "Tom" to "Jack"
        TiXmlText *new_txt = new TiXmlText("Jack");
        pElm2->LinkEndChild(new_txt);
        cout << pElm2->GetText() << endl;  // Output is "TomJack"

    How to use "Jack" replace "Tom"?

    Thanks!

     

  • Anonymous
    2010-08-21

    void SetNodeValue( TiXmlNode* node , CString value ) {
    TiXmlNode* child ;
    TiXmlText* text ;
    for ( child = node->FirstChild() ; child ; node = child->NextSibling() ) {
    text = child->ToText() ;
    if ( text ) {
    if ( text->Value() != 0 ) {
    DeleteNode( text ) ;
    break ;
    }
    }
    }
    if ( !value.IsEmpty() ) {
    child = node->FirstChild() ;
    text = new TiXmlText( value ) ;
    ( child ) ? node->InsertBeforeChild( child , *text ) : node->LinkEndChild( text ) ;
    }
    Document->SaveFile( File ) ;
    }