0

I'm quite new to TinyXml and I'm having some problems.I'm trying to learn how to create and read files.
I can read data from this example

<?xml version="1.0" standalone=no>
<!-- Our to do list data -->
<ToDo>
    <Item priority="1"> Go to the <bold>Toy store!</bold></Item>
    <Item priority="2"> Do bills</Item>
</ToDo>

provided on main page but when I recreate this file it fails to load

Here is my code to recreate that file:

TiXmlDocument document;
TiXmlElement * root = 0;
TiXmlDeclaration* declar = 0;
TiXmlComment * comment = 0;

// Create declaration
declar = new TiXmlDeclaration( "1.0", "", "no" );
// Link it to doc
document.LinkEndChild( declar );

// Create Comment
comment = new TiXmlComment();
comment->SetValue( "Our to do list data" );
// Link it to doc
document.LinkEndChild( comment );

// Create root and Link it
root = new TiXmlElement( "ToDo" );
document.LinkEndChild( root );

// Create item 1 element
TiXmlElement* item1 = new TiXmlElement( "item" );

// Set Its attribute priority 1
item1->SetAttribute( "priority", "1" );
// Link text element
TiXmlText* item1Text = new TiXmlText( "Go To The" );
item1->LinkEndChild( item1Text );

// Create item1 Bold element
TiXmlElement* item1Bold = new TiXmlElement( "Bold" );
// Link Text element to bold element
TiXmlText* boldText = new TiXmlText( "Toy Store" );
item1Bold->LinkEndChild( boldText );

// Link bold Element to Item element
item1->LinkEndChild( item1Bold );

// Link item element to root node
root->LinkEndChild( item1 );


// Create item 2 element
TiXmlElement* item2 = new TiXmlElement( "item" );

// Set its attribute priority 2
item2->SetAttribute( "priority", "2" );
// And Link Text Item
TiXmlText* item2Text = new TiXmlText( "Do Bills" );
item2->LinkEndChild( item2Text );

// Link another item element
root->LinkEndChild( item2 );

// Save
document.SaveFile("TestFile.xml");

Can you tell me please what I'm missing or doing wrong ?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
Rafal Rebisz
  • 189
  • 2
  • 7
  • Why aren't you storing the pointers to things you `new`? It makes your code absurdly complicated to read, and is probably the cause of your bugs. Also, I don't think you need to use `new` at all... – Mooing Duck Feb 24 '15 at 01:00
  • 1
    You say the file fails to load, but the code you have shown is the code to create the file, not load it. – Jonathan Potter Feb 24 '15 at 01:02
  • No `new`: http://coliru.stacked-crooked.com/a/6b0736462e8e14bd – Mooing Duck Feb 24 '15 at 01:06
  • the thing is that it fails on document.LoadFile(); thats what I mean I can create file but It fails on LoadFile Method here is the file created with this code Go To The Toy Store Do Bills – Rafal Rebisz Feb 24 '15 at 01:09
  • Mooing Duck When I run your code I get debug assertion failed Expression: _BLOCK_TYPE_IS_VALID(phead->nblockUse) after the code goes out of scope and now I get what what you mean by "Why aren't you storing the pointers" your code is much more readable – Rafal Rebisz Feb 24 '15 at 01:25
  • Ok so with pointers there is no assertion fail error, That's what I find in tutorials they using pointers to read and load data, the thing is that file is being saved but it doesn't wanna load as I said before it fails on LoadFile() method when I try to open demo file it works the file is loading but my saved file won't any idea why? there is small difference between files in number of lines and in how the text is formatted maybe that's the reason why it fails ? are there any settings that I can change to format my output file in different way ? – Rafal Rebisz Feb 24 '15 at 02:34

1 Answers1

0

Ok so here is solution to the problem I've linked root node to document before linking other elements to root therefore moving that at the end before save method call solved the problem thank you !!

TiXmlDocument document;
TiXmlElement * root = 0;
TiXmlDeclaration* declar = 0;
TiXmlComment * comment = 0;

// Create declaration
declar = new TiXmlDeclaration( "1.0", "", "no" );
// Link it to doc
document.LinkEndChild( declar );

// Create Comment
comment = new TiXmlComment();
comment->SetValue( "Our to do list data" );
// Link it to doc
document.LinkEndChild( comment );

// Create root and Link it
root = new TiXmlElement( "ToDo" );

document.LinkEndChild( root );  

//here is the problem I've linked root node to document before linking other elements to root node when moved that part of code at the end before I save the file it works Yeah

// Create item 1 element
TiXmlElement* item1 = new TiXmlElement( "item" );

// Set Its attribute priority 1
item1->SetAttribute( "priority", "1" );
// Link text element
TiXmlText* item1Text = new TiXmlText( "Go To The" );
item1->LinkEndChild( item1Text );

// Create item1 Bold element
TiXmlElement* item1Bold = new TiXmlElement( "Bold" );
// Link Text element to bold element
TiXmlText* boldText = new TiXmlText( "Toy Store" );
item1Bold->LinkEndChild( boldText );

// Link bold Element to Item element
item1->LinkEndChild( item1Bold );

// Link item element to root node
root->LinkEndChild( item1 );


// Create item 2 element
TiXmlElement* item2 = new TiXmlElement( "item" );

// Set its attribute priority 2
item2->SetAttribute( "priority", "2" );
// And Link Text Item
TiXmlText* item2Text = new TiXmlText( "Do Bills" );
item2->LinkEndChild( item2Text );

// Link another item element
root->LinkEndChild( item2 );


// Save
document.SaveFile("TestFile.xml");`
Rafal Rebisz
  • 189
  • 2
  • 7