We’ll start with Nodets at the bottom level and work our way up.

Define a function inside a class:

QDomElement writeNodets(QDomDocument &doc);

Note that the argument is QDomDocument, because that’s how you’re going to create the node element.

Enclose the function below:

`QDomElement NodeTS::writeNodets(QDomDocument &doc) { QDomElement NormalizedIntensityNode = doc.documentElement(); NormalizedIntensityNode = doc.createElement("NormalizedIntensity"); NormalizedIntensityNode.setAttribute("Value", NormalizedIntensity); QDomElement OpacityNode = doc.documentElement(); OpacityNode = doc.createElement("Opacity"); OpacityNode.setAttribute("Value",Opacity); QDomElement EmissionNode = doc.documentElement(); EmissionNode = doc.createElement("Emission"); EmissionNode.setAttribute("G", Emission.green()); EmissionNode.setAttribute("R", Emission.red()); EmissionNode.setAttribute("B", Emission.blue()); QDomElement ccNode = doc.documentElement(); ccNode = doc.createElement("Node"); ccNode.appendChild(NormalizedIntensityNode); ccNode.appendChild(OpacityNode); ccNode.appendChild(EmissionNode); return ccNode; } `Copy the code

It’s no different than reading and writing files before. We create each element, add attributes and attribute values to it, group it into a node, and return it as a return value. If you’ve read the first few chapters of the blog, you’ll be familiar with this. There’s nothing new to learn in these recent sections, except to get more familiar with the structure of XML files and the way we design our own classes to read and write them. The difficulty of design lies mainly in the progressive nature.

We go to the next level, which uses tsfunItem to get the contents of Nodets. Let’s define a function:

`QDomElement writeTsfunItem(QDomDocument &doc); QDomElement TsfunItem::writeTsfunItem(QDomDocument &doc) { QDomElement DensityScaleElement = doc.documentElement(); DensityScaleElement = doc.createElement("DensityScale"); DensityScaleElement.setAttribute("Value", DensityScale); QDomElement ShadingTypeElement = doc.documentElement(); ShadingTypeElement = doc.createElement("ShadingType"); ShadingTypeElement.setAttribute("Value", ShadingType); QDomElement GradientFactorElement = doc.documentElement(); GradientFactorElement = doc.createElement("GradientFactor"); GradientFactorElement.setAttribute("Value", GradientFactor); QDomElement TsNodesElement = doc.documentElement(); TsNodesElement = doc.createElement("Nodes"); //DebugWidget::getDebugWidget()->addContents(QString::number(TsNodes.count())); for (int i = 0; i < TsNodes.count(); i++) { TsNodesElement.appendChild(TsNodes[i].writeNodets(doc)); } QDomElement itemNodes = doc.documentElement(); itemNodes = doc.createElement("TsfunItem"); itemNodes.setAttribute("Name", NameID); itemNodes.appendChild(TsNodesElement); itemNodes.appendChild(DensityScaleElement); itemNodes.appendChild(ShadingTypeElement); itemNodes.appendChild(GradientFactorElement); return itemNodes; } `Copy the code

TsNodes[I]. WriteNodets (doc); Functions, there’s a lot of repetition here, but the structure seems to be getting more and more complicated. After I finish the whole read and write explanation, as well as before the implementation of a node to add and delete procedures, I will explain the overall design idea to you again, to ensure that you can better understand our design idea. In the next section we finish writing the entire file program.