 Hey everybody, this is Brian and welcome to the 94th cute tutorial with C++ and GUI programming. Wow 94 tutorials We're really cruising along here. All right today. We are going to learn how to write XML So let's just make a new project here put it in the usual place next next finish finish Now first thing we want to do here is actually go into our project file and we want to add an include for XML Once you got that done you're ready to go So let's jump into our main dot CPP and let's add a few includes here I'm going to add Qt core even though we have Qt core application Which means we can get rid of that We'll add all of Qt XML And then of course we're going to add the infamous Qt debug Now our application right now doesn't really do a whole lot. Let's give it a good build make sure we got everything linked correctly So it doesn't really do anything. We've just got our basic generic console application, which we've seen a million times And we are going to get this to write some XML. So we're just going to Put in a comment here XML can get a little bit tricky when you're coding it Especially when you're coding it by hand because you tend to lose track of where you are. So let's say Q document object model Document that's what the Q DOM stands for is Q document object model and we're making a document Now we have our document, but we haven't done anything with it yet. So what we need to do are create elements So we'll say Q DOM element Whoops don't need an element. We just need an element We're going to call this root because this is our root element if you remember from our XML primary a well-formed document We'll have a single root node We'll say document And we'll say create We want create element Let's just go ahead and hide our window here. There we go That way we get more space here Now that is our root element whoops helps if we put our little equal sign in here Just because we have a root element doesn't mean it actually exists anywhere So what we need to do is actually append it Say document a pen child and we're just going to add the root element in there and Then we'll also append any child elements If you remember from our primer we can add a node within a node within a node Or I should say an element within an element with an element so we can add multiple children to this route. So let's just say make the root element and Then we're just going to document here Add it to the document And don't worry. We're not going to document every single line here Just want it to be Perfectly clear what we're doing here Now let's write out some code really quickly to write this to a file And we're just gonna say q file. Let's give this a file name here And let's just call this my xml.xml If you're wondering what this code does on you should really watch the tutorial we've done on file.io Basically, we're just opening a file and we're saying the qio device and We want to get the write-only access to that file And we're going to do it of course in text mode That's not working. Oh, yeah Sorry, I've had a long couple days. There's some some wood siding on my house. My house is half wood half vinyl and Me and the better half or management as I like to call her Decided to go out there and replace some of that and what started as a very simple job as I'm sure some of you Homeowners are out there snickering What started is a very simple two-hour project ended up taking three days I'll fail to open one file for writing Basically, we're just doing a very simple test here to see if in fact We could open the file and write to it and if we couldn't we're just going to return an error code we'll just say negative one and If we could let's just say else Say q text stream Now I had a reader or reader jeez where's my head I had a viewer. Sorry. I've actually been doing some writing on the side I had a viewer Write in and say hey, why don't you have some of this stuff already pre-written so we don't have to sit here and watch type Well, that's a very very good question I've asked myself that many many times and the only answer I can come up with is if it's pre-written I tend to skip right over it and not explain it. So if somebody had not watched this tutorial or any of the previous tutorials on how to You know stream something to a document they'd be going. What are you doing? And it would just you know, it wouldn't make any sense. It wouldn't have any real congruent So just for the sake of argument bear with me as I whip this code out real quick. I Just I can't believe I just said whip this out real quick. Anyways, you guys know what I mean quit snickering, right? So let's just say Q to bug and we're just going to say finished So really let's do a quick review here All we're doing is we're creating a Q document object model document or Q DOM document We're creating a Q document object element or Q DOM element and we're doing that through the document dot create element It does that because well documents can have specific schemas and Things that we really won't touch into because we're not going to get too deep into how XML is structured But what that does is the Q DOM document is it actually makes sure it's well structured versus the document It gets all the formatting in there And now that we have our element we will say Document a pen child and we're going to append that element Now we're going to put extra elements in here and they all get appended and then we're simply going to write Out to a file now notice these forward slashes that is the cross platform way of doing it because if you do it that way That's an escape character and that's a no-no So let's save and run and oh Else without previous if what did we goof up here guys? Let me pause this real quick and find the error. Hmm. Yeah, that went through me off for a minute It's cuz I got this right there. All right Let's run this real quick see what happens And it says that I finished let me pull that up real quick All right now there's not a lot to our XML file as you can see here Basically, we've just got our declaration followed by an empty element here So we're gonna populate this out a little bit more Let's bring up cute again. Sorry about that Now if you remember from our discussion earlier what we're talking about is we have this root element and that is what we've appended here So what we need to do here. Oh, oh, no, I made eye contact with my cat here. She comes. Hey kitty daddy's recording Go bye. Bye. Love you I don't know what it is with this cat, but I am like invisible to her until I start recording something Then she's got to have my attention We're gonna add some elements here and we're just gonna say kitty go bye. Bye daddy's recording See I hate to have the cat whining in the background, but this far into it I just hate stopping and starting over just because the cat's whining So I hope you guys like cats and I hope none of you have dogs that absolutely hate cats Cuz she's gonna sit here and whine for my tension for a few minutes Yes, kitty, I can't type and pet you at the same time. Go bye. Bye You see the problem is I made eye contact with her and because of that. I'm just I'm sunk All right, so we're going to make an element here and we're gonna call it node And we're going to say this is document Create element kitty why don't you go take a nice nap or go watch the chipmunk cuts outside? And we're just gonna call this book because I mean we're just gonna make an XML file that has a bunch of books in it now We have our node what we need to do here is we're going to Set attribute remember our discussion of what an attribute is we're gonna give it a name attribute and we're gonna say the name of this is My book and Let's give it a number here Q string Number if you wonder what I'm doing. That's how you convert a Q string into a number Or I should say a number in a Q string. Sorry about that. So we're just gonna cover I our current index into a number So it's the name attributes quite literally gonna be my book and space and then a number and then we are going to say ID Set attribute and we'll just say ID you know say it was like an ISBN number or something But we're just gonna we're gonna take the cheap and easy way out. We're just gonna give it the the index here. All right Now what we want to do here is say root dot a pen child And we want to add the node in there and actually no, let's not call this node. Let's call this book Save our work run this So it's finished let me pull the file and sure enough here's our new XML file you see we've got the same Declaration we have our books element and we can collapse and expand that back out and in here We have a bunch of book elements Now what we're going to do is we're going to take these individual book elements and add chapters to each book So we've got 10 books. Let's just go ahead and add like 10 chapters per book Jump back into the code here and we can pretty much just copy and paste this code here Pretty simple pretty easy and we're just gonna say chapter And we're just going to set the name to my chapter and the ID is going to be the same here Let's set these actually to age instead of I Because we're in our inner loop here It's a nestled for loop case. You're fairly new to see plus plus Save our work run this Says finish no error so far Give it a quick refresh now you can see we've got our our root element and inside our root element We have individual books Or I should say individual elements and each element has other elements and you can keep going and going and going Going that's the beautiful thing about XML is that you can structure it the way you want it So no two XML files are identical That's actually held pretty true in my experience is that XML is so flexible that well It sometimes can be a nightmare to deal with which we will discuss in our next video reading XML This is Brian. I wanted to thank you for watching this tutorial and If you get a chance If you haven't been there before Some shameless advertising here head out to my website. I've got all these tutorials out there You just go under cute. I've got other languages and I've got them all in order. I am working on the playlist for YouTube I know you guys love those and let's say you go into a Tutorial here you can watch the tutorial and you can download the associated source code with it So you don't have to retype all this garbage, but anyways, this is Brian. Thank you for watching I appreciate your feedback. I know I'm like really really sorry. I've been horrible about getting back to you guys Life has just been crazy Like I said, I have my VMware test my cat which oddly has disappeared So let's talk very quietly so she doesn't hear us Anyways, my cat's been bugging me and working on my house and work and the kids. So I will get to your messages I promise you let me repeat that. Oh Here comes a cat See I spoke up. I will get to your messages. I'm sorry He's just gonna take me a little bit longer. So if you're sitting there going Brian, why haven't you returned my message? I'm getting to it. Anyways, thanks for watching. See you next time