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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
When I use STL the error is gone but nothing is working well. Can it be a STL error or anything like this?
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
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?
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
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.
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!