QT QDomDocument读取xml格式

xml格式如下

<Skin level_count="1">
	<LOD level="0" MeshDataFile="xxxxxx.sknm" ske="xxxxxxx.skem">
		<Submeshes>
			<Submesh id="0">
				<Material shader="xxxxxxxx">
					<RenderStates/>
					<Uniforms>
						<tDiff type="Texture" TextureType="2D" sRGB="0">xxxxxxxxxxxx</tDiff>
					</Uniforms>
				</Material>
			</Submesh>
		</Submeshes>
	</LOD>
</Skin>

读取代码如下:

QFile fileReader(strUrl);
     if (!fileReader.open(QIODevice::ReadOnly))
         return;

     QDomDocument docXML;
     docXML.setContent(&fileReader);
     fileReader.close();

     QDomElement rootElement = docXML.documentElement();
     QDomElement childElement = rootElement.firstChildElement();
     if (!childElement.isNull())
     {
         qDebug("childElement node name =%s",childElement.nodeName().toStdString().c_str());

         QString MeshDataFile = childElement.attribute("MeshDataFile");
         QString ske = childElement.attribute("ske");
         qDebug("ske  =%s",ske.toStdString().c_str());
         qDebug("MeshDataFile  =%s",MeshDataFile.toStdString().c_str());

         QDomElement threeElement= childElement.firstChildElement();
         if (!threeElement.isNull())
         {
             qDebug("threeElement node name =%s",threeElement.nodeName().toStdString().c_str());

             QDomElement FourElement= threeElement.firstChildElement();
             if (!FourElement.isNull())
             {
                 qDebug("FourElement node name =%s",FourElement.nodeName().toStdString().c_str());

                 QDomElement FiveElement= FourElement.firstChildElement();
                 if (!FiveElement.isNull())
                 {
                     qDebug("FiveElement node name =%s",FiveElement.nodeName().toStdString().c_str());
                     if(FiveElement.nodeName()=="Material")
                     {
                         QString shader = FiveElement.attribute("shader");

                         QDomNamedNodeMap attributeMap = FiveElement.attributes();
                         int nSize = attributeMap.count();
                         for(int n=0;n<nSize;n++)
                         {
                             QDomNode attribute = attributeMap.item(n);
                             qDebug("attribute node name =%s",attribute.nodeName().toStdString().c_str());
                         }
                         //再往下一级就是纹理
                         QDomNodeList listChild = FiveElement.childNodes();
                         int iChildSize = listChild.size();
                         for(int k = 0; k < iChildSize; k++)
                         {
                              QDomNode itemChild = listChild.at(k);
                              qDebug("itemChild node name =%s",itemChild.nodeName().toStdString().c_str());
                              if(itemChild.nodeName()=="Uniforms")
                              {
                                  QDomElement itemUniforms= itemChild.toElement();
                                  QDomElement itemUniformsChild= itemUniforms.firstChildElement();
                                  QString type = itemUniformsChild.attribute("type");
                                  QString TextureType = itemUniformsChild.attribute("TextureType");
                                  QString typeget = itemUniformsChild.attribute("type");
                                  QString sRGB = itemUniformsChild.attribute("sRGB");
                                  QString valueUrl = itemUniformsChild.text();
                                  qDebug("valueUrl  =%s",valueUrl.toStdString().c_str());

                                  break;
                              }
                         }

                     }
                 }
             }
         }

     }

猜你喜欢

转载自blog.csdn.net/qq_30377315/article/details/128370311