Menu

Error with SetValue()

fly2c
2007-08-27
2013-05-20
  • fly2c

    fly2c - 2007-08-27

    Hi

    I try to use SetValue() to set a new text for an element but get an error.

    my empty tag at the beginning is:

    <ph_res></ph_res>

    Now I try to set a value for "ph_res", e.g. "55"

    So my code looks like this:

    //got a CString with value "55"
    CString temp = getCString();
    USES Conversion;
    const char* value;
    value = T2A(temp);

    TiXmlText* text = docHandle.ChildElement("ph_res",0).FirstChild().Text();
    text->SetValue(value);

    When I starts my Console Application, it crashes at "SetValue"
    The Debugger says the error shows to "tinystr.h" at:

    //Return the length of a TiXmlString
    size_type length () const {return rep_->size; }

    "Unhandled exception: 0xC00000005: Access Violation"

    Same error occurs when I try:
    text->SetValue("55");

    Please can anybody help?

     
    • fly2c

      fly2c - 2007-08-27

      When I use STL the error is gone but nothing is working well. Can it be a STL error or anything like this?

       
    • Lee Thomason

      Lee Thomason - 2007-08-27

      fly2c --

      The FirstChild() isn't a TiXmlText - it is a TiXmlElement. The Text() function therefore returns null, and 'text' is null, and calling the method of a a null pointer is a Bad Thing. :)

      Also, Text() is deprecated in favor of ToText().

      lee

       
    • fly2c

      fly2c - 2007-08-28

      Thanks for your answer

      I changed the code to:

      TiXmlText* text = docHandle.ChildElement("ph_res",0).FirstChild().ToText();
      text->SetValue("55");

      ... but the problem still exists.

      I tried another xml file and there occurs the same error with SetValue().
      I must compile with the UNICODE compiler option. Can this be an error reason?

       
    • Lee Thomason

      Lee Thomason - 2007-08-28

      There is no TiXmlText node in your XML at all. You will never be able to get a pointer to something that doesn't exist. The FirstChild() is a TiXmlElement, not text.

      If you want to add a text node, InsertEndChild( text ) on the Element.

      typing from memory (untested):
      TiXmlElement* element = docHandle.ChildElement("ph_res",0).ToElement();
      TiXmlText text( "55" );
      element->InsertEndChild( text );

      lee

       
    • fly2c

      fly2c - 2007-08-29

      I tried it out, but

      element->InsertEndChild() wants a TiXmlNode, not a TiXmlText. So I get an error for: element->InsertEndChild(text);

      I can't really understand. In the "help" there exactly the same examples, but it doesn't work.

       
    • fly2c

      fly2c - 2007-08-29

      I found a solution.

      I go through the xml document and set "node" on the element that I will change. I take for this:

      TiXmlDocument doc(pathname);

      //after loading

      TiXmlNode* node = 0;
      node = doc.RootElement(); // node is root
      node = node->FirstChild();

      while(strcmp(node->Value(), "ph_res") != 0) //look for ph_res node
      {
      node = node->NextSibling();
      }

      When I reached the element:

      TiXmlElement* element = node->ToElement();
      TiXmlText text ( "55");
      element->InsertEndChild(text);

      ... and it works.

      Thanks for your help!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.