once the document object goes outof scope it is no longer valid, but there is situation in my code resembmling the above sitation and i want the document object in the scope as above, i cant declare it in the same scope as xnode.
is there any solution for the such case?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
My 2c worth though (said without knowing the larger design scheme of your application) is that it is better to act on the data while you have it, rather than copy stuff.
TiXmlNode* xnode;
TiXmlDocument xdoc("C:\\books.xml");
xdoc.LoadFile();
xnode = xdoc.FirstChild();
while (xnode) { // or use a for loop
do_something_with_node(xnode);
xnode=xnode->NextSibling();
}
HTH
Ellers
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hi all,
i am facing the problem while writing the code like this:
TiXmlNode* xnode;
{
TiXmlDocument xdoc("C:\\books.xml");
xdoc.LoadFile();
xnode = xdoc.FirstChild();
}
TiXmlNode* node1;
node1 = xnode;
once the document object goes outof scope it is no longer valid, but there is situation in my code resembmling the above sitation and i want the document object in the scope as above, i cant declare it in the same scope as xnode.
is there any solution for the such case?
You can use Clone() to make a copy of the node.
http://www.grinninglizard.com/tinyxmldocs/classTiXmlNode.html#a72
So something like:
TiXmlNode* xnode;
{
TiXmlDocument xdoc("C:\\books.xml");
xdoc.LoadFile();
xnode = xdoc.FirstChild().Clone();
}
My 2c worth though (said without knowing the larger design scheme of your application) is that it is better to act on the data while you have it, rather than copy stuff.
e.g. something like:
TiXmlNode* xnode;
TiXmlDocument xdoc("C:\\books.xml");
xdoc.LoadFile();
xnode = xdoc.FirstChild();
do_something_with_node(xnode);
or to process many nodes
TiXmlNode* xnode;
TiXmlDocument xdoc("C:\\books.xml");
xdoc.LoadFile();
xnode = xdoc.FirstChild();
while (xnode) { // or use a for loop
do_something_with_node(xnode);
xnode=xnode->NextSibling();
}
HTH
Ellers