Now let’s look at a concrete example:

` <? The XML version = "1.0" encoding = "utf-8"? > <TsfunGroup> <TsfunItem Name="normalMode"> <Nodes> <Node> <NormalizedIntensity Value="0"/> <Opacity Value="0"/> <Emission ="0" R="0" B="0"/> </Node> <Node> <NormalizedIntensity Value="0.23597"/> <Opacity Value="0"/> <Emission ="0" R="0" B="0"/> </Node> <Node> <NormalizedIntensity Value="0.288538"/> <Opacity Value="0.0179028"/> <Emission ="0" R="0" B="0"/> </Node> <Node> <NormalizedIntensity Value="0.288578"/> <Opacity Value="0.563502"/> <Emission ="0" R="0" B = "0" / > < / Node > < Node > < NormalizedIntensity Value = "0.35417" / > < Opacity Value = "1" / > < = "0" Emission G R = "0" B = "0" / > < Node >  <Node> <NormalizedIntensity Value="1"/> <Opacity Value="1"/> <Emission G="0" R="0" B="0"/> </Node> </Nodes> <DensityScale Value="100"/> <ShadingType Value="2"/> <GradientFactor Value="3"/> </TsfunItem> <TsfunItem Name="maxMode">  <Nodes> <Node> <NormalizedIntensity Value="0"/> <Opacity Value="0"/> <Emission G="0" R="0" B="0"/> </Node> <Node> <NormalizedIntensity Value="0.13597"/> <Opacity Value="0"/> <Emission G="0" R="0" B="0"/> </Node> <Node> <NormalizedIntensity Value="0.188538"/> <Opacity Value="0.0279028"/> <Emission G="0" R="0" B="0"/> </Node> <Node> <NormalizedIntensity Value="0.288578"/> <Opacity Value="0.363502"/> <Emission G="0" R="0" B="0"/> </Node> <Node> <NormalizedIntensity Value="0.45417"/> <Opacity Value="1"/> <Emission ="0" R="0" B="0"/> </Node> <Node> <NormalizedIntensity Value="1"/> <Opacity Value="1"/> <Emission G="0" R="0" B="0"/> </Node> </Nodes> <DensityScale Value="100"/> <ShadingType Value="2"/> <GradientFactor Value="3"/> </TsfunItem> </TsfunGroup>`Copy the code

Consider that there are various XML files in our project, and the XML above is just one of them. How can we design a common API and then implement the functionality of the file above?

First, let’s define a public interface class. Note that this is a QT class. Let’s call it CommonXML.

Let’s start by creating a C++ QT class that inherits from QObject:

`class CommonXML : public QObject { Q_OBJECT public: private: public: CommonXML(QObject * parent = Q_NULLPTR); ~CommonXML(); private: }; `Copy the code

Let’s assume that we have a label (Name) for our class that identifies the contents of each class, let’s call it QString Name, and then we’re done defining the base class for now. We’ll define the inherited class later.

Let’s take a look at the example above:

The root node is called, in other words, this is a TSfunGroup group, and there are a lot of tsFunItems in it, and for simplicity we just set up two tsFunItems, one of which is normalMode and one of which is maxMode. Each TsfunItem contains four items:

`<Nodes>
</Nodes>
<DensityScale Value="100"/>
<ShadingType Value="2"/>
<GradientFactor Value="3"/>`
Copy the code

There is a set of Nodes and three sets of data.

We’ll leave that aside for now, but we need to notice the innermost data structure first:

`    <Node>
                <NormalizedIntensity Value="0"/>
                <Opacity Value="0"/>
                <Emission G="0" R="0" B="0"/>
    </Node>`
Copy the code

We need to design a data structure to store the Node. Now start from the innermost layer and work your way up to the outermost layer:

`Node.hpp #ifndef _NODETS_H__ #define _NODETS_H__ #include <QObject> #include <QColor> #include "commonxml.hpp" class NodeTS : public CommonXML { Q_OBJECT public: NodeTS(QObject * parent = Q_NULLPTR); ~NodeTS(); private: double NormalizedIntensity; double Opacity; QColor Emission; void readNode(QDomNodeList &node); }; #endif Let's just think about reading out what's in the node. #include "nodets.hpp" NodeTS::NodeTS(QObject * parent) : CommonXML(parent) { } NodeTS::~NodeTS() { } void NodeTS::readNode(QDomNodeList & nodelist) { for (int i = 0; i < nodelist.count(); i++) { QDomNode node = nodelist.at(i); if (node.toElement().tagName() == "NormalizedIntensity") { NormalizedIntensity = node.toElement().attribute("value").toDouble(); } else if(node.toElement().tagName() == "Opacity") { Opacity = node.toElement().attribute("value").toDouble(); } else if (node.toElement().tagName() == "Emission") { Emission.setRed(Opacity = node.toElement().attribute("R").toInt()); Emission.setGreen(Opacity = node.toElement().attribute("G").toInt()); Emission.setBlue(Opacity = node.toElement().attribute("B").toInt()); } //realDatanode.toElement().attribute("param1") + } }`Copy the code

The input is a QDomNodeList object, and each item inside is read out.

After all, there is more than one node in a Node, and we need to read these Nodes out.