Menu

Problem with TiXmlDocument

Developer
sandeep
2007-08-24
2013-05-20
  • sandeep

    sandeep - 2007-08-24

    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?

     
    • Ellers

      Ellers - 2007-08-26

      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

       

Log in to post a comment.