Qt读取xml文件

<?xml version="1.0" encoding="utf-8"?>

<Transaction version="1.0"> 
  <BlockchainId type="string"/>  
  <FrezzeAmount type="long"/>  
  <RequestType type="enum"> 
    <CCR>1</CCR>  
    <CCA>2</CCA> 
  </RequestType> 
  <Amount type="long"/> 
</Transaction>
bool parseTransactionXml(QString xmlPath) {

    QFile xmlFile(xmlPath);
    if(!xmlFile.open(QFile::ReadOnly)) {
        qDebug() << "transaction xml file open failed";
        return false;
    }
    QXmlStreamReader xml(&xmlFile);

    if(xml.readNextStartElement()) {

        QString rootName = xml.name().toString();
        QString xmlVersion = xml.attributes().value("version").toString();

        if (rootName != TRANSACTION_XML_ROOT) {
            qDebug("cannot find root element");
            return false;
        }
        if (xmlVersion != TRANSACTION_XML_VERSION) {
            qDebug() << "transaction xml version is not compatible";
            return false;
        }
        //循环获取所有元素
        while (xml.readNextStartElement()) {
            QString strName = xml.name().toString();
            QXmlStreamAttributes attributes = xml.attributes();
            QString type = attributes.value("type").toString();
            qDebug() << strName << ":" << type;
            if(type == "enum")
            {
                while (xml.readNextStartElement()) {
                    QString strName = xml.name().toString();
                    qDebug() << strName << ":" << xml.readElementText();
                    xml.readNext();
                }
            }
            xml.readNext();
        }

    }
    else if(xml.hasError()) {
        qDebug() << "xml file error:" << xml.errorString();
        return false;
    }
    return true;
}

例子二:

xml文件:

<?xml version="1.0" encoding="utf-8"?>

<pnml:pnml xmlns:pnml="http://www.pnml.org/version-2009/grammar/pnml" xmlns="http://www.daimi.au.dk/CPnets/pnml/hlpn" xmlns:cpn="http://www.daimi.au.dk/CPNTools/hlpn">
  <pnml:net id="N65545" type="http://www.daimi.au.dk/CPnets/pnml/hlpn">
    <pnml:declarations>
      <pnml:toolspecific tool="CPN Tools" version="1.0.0">
        <cpn:startblock name="Standard priorities"/>
      </pnml:toolspecific>
      <pnml:toolspecific tool="CPN Tools" version="1.0.0">
        <cpn:endblock/>
      </pnml:toolspecific>
      <pnml:toolspecific tool="CPN Tools" version="1.0.0">
        <cpn:startblock name="Standard declarations"/>
      </pnml:toolspecific>
      <pnml:declaration>
        <pnml:type>
          <pnml:name>UNIT</pnml:name>
          <pnml:type/>
        </pnml:type>
      </pnml:declaration>
      <pnml:declaration>
        <pnml:type>
          <pnml:name>BOOL</pnml:name>
          <pnml:type>
            <pnml:bool/>
          </pnml:type>
        </pnml:type>
      </pnml:declaration>
      <pnml:declaration>
        <pnml:type>
          <pnml:name>INT</pnml:name>
          <pnml:type>
            <pnml:int/>
          </pnml:type>
        </pnml:type>
      </pnml:declaration>
      <pnml:declaration>
        <pnml:type>
          <pnml:name>INTINF</pnml:name>
          <pnml:type/>
        </pnml:type>
      </pnml:declaration>
      <pnml:declaration>
        <pnml:type>
          <pnml:name>TIME</pnml:name>
          <pnml:type/>
        </pnml:type>
      </pnml:declaration>
      <pnml:declaration>
        <pnml:type>
          <pnml:name>REAL</pnml:name>
          <pnml:type/>
        </pnml:type>
      </pnml:declaration>
      <pnml:declaration>
        <pnml:type>
          <pnml:name>STRING</pnml:name>
          <pnml:type>
            <pnml:string/>
          </pnml:type>
        </pnml:type>
      </pnml:declaration>
      <pnml:toolspecific tool="CPN Tools" version="1.0.0">
        <cpn:endblock/>
      </pnml:toolspecific>
    </pnml:declarations>
    <pnml:page id="ID6"> 
      <pnml:name>
        <pnml:text>New Page</pnml:text>
      </pnml:name>  
      <pnml:place id="ID1412323325">
        <pnml:name>
          <pnml:text>P0</pnml:text>
          <pnml:graphics>
            <pnml:offset x="0" y="0"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:name>
        <pnml:initialMarking>
          <pnml:text>0</pnml:text>
          <pnml:graphics>
            <pnml:offset x="57" y="24"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:initialMarking>
        <pnml:type>
          <pnml:text>UNIT</pnml:text>
          <pnml:graphics>
            <pnml:offset x="40" y="-23"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:type>
        <pnml:graphics>
          <pnml:position x="31" y="168"/>
          <pnml:fill color="#ffffff"/>
          <pnml:line color="#000000" width="2" style="solid"/>
          <pnml:dimension x="60.000000" y="40.000000"/>
          <pnml:toolspecific tool="CPN Tools" version="1.0.0">
            <cpn:ellipse/>
          </pnml:toolspecific>
        </pnml:graphics>
      </pnml:place>  
      <pnml:place id="ID1412323496">
        <pnml:name>
          <pnml:text>P1</pnml:text>
          <pnml:graphics>
            <pnml:offset x="0" y="0"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:name>
        <pnml:initialMarking>
          <pnml:text>0</pnml:text>
          <pnml:graphics>
            <pnml:offset x="56" y="23"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:initialMarking>
        <pnml:type>
          <pnml:text>UNIT</pnml:text>
          <pnml:graphics>
            <pnml:offset x="39" y="-24"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:type>
        <pnml:graphics>
          <pnml:position x="241" y="52"/>
          <pnml:fill color="#ffffff"/>
          <pnml:line color="#000000" width="2" style="solid"/>
          <pnml:dimension x="60.000000" y="40.000000"/>
          <pnml:toolspecific tool="CPN Tools" version="1.0.0">
            <cpn:ellipse/>
          </pnml:toolspecific>
        </pnml:graphics>
      </pnml:place>  
      <pnml:place id="ID1412323568">
        <pnml:name>
          <pnml:text>P2</pnml:text>
          <pnml:graphics>
            <pnml:offset x="0" y="0"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:name>
        <pnml:initialMarking>
          <pnml:text>0</pnml:text>
          <pnml:graphics>
            <pnml:offset x="56" y="24"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:initialMarking>
        <pnml:type>
          <pnml:text>UNIT</pnml:text>
          <pnml:graphics>
            <pnml:offset x="39" y="-23"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:type>
        <pnml:graphics>
          <pnml:position x="241" y="245"/>
          <pnml:fill color="#ffffff"/>
          <pnml:line color="#000000" width="2" style="solid"/>
          <pnml:dimension x="60.000000" y="40.000000"/>
          <pnml:toolspecific tool="CPN Tools" version="1.0.0">
            <cpn:ellipse/>
          </pnml:toolspecific>
        </pnml:graphics>
      </pnml:place>  
      <pnml:place id="ID1412323751">
        <pnml:name>
          <pnml:text>P3</pnml:text>
          <pnml:graphics>
            <pnml:offset x="0" y="0"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:name>
        <pnml:initialMarking>
          <pnml:text>0</pnml:text>
          <pnml:graphics>
            <pnml:offset x="56" y="23"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:initialMarking>
        <pnml:type>
          <pnml:text>UNIT</pnml:text>
          <pnml:graphics>
            <pnml:offset x="39" y="-23"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:type>
        <pnml:graphics>
          <pnml:position x="475" y="163"/>
          <pnml:fill color="#ffffff"/>
          <pnml:line color="#000000" width="2" style="solid"/>
          <pnml:dimension x="60.000000" y="40.000000"/>
          <pnml:toolspecific tool="CPN Tools" version="1.0.0">
            <cpn:ellipse/>
          </pnml:toolspecific>
        </pnml:graphics>
      </pnml:place>  
      <pnml:transition id="ID1412323343">
        <pnml:name>
          <pnml:text>T0</pnml:text>
          <pnml:graphics>
            <pnml:offset x="0" y="0"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:name>
        <pnml:guard>
          <pnml:text/>
          <pnml:graphics>
            <pnml:offset x="-39" y="31"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:guard>
        <pnml:toolspecific tool="CPN Tools" version="1.0.0">
          <cpn:time>
            <cpn:text/>
            <cpn:graphics>
              <cpn:line color=""/>
              <cpn:font family="arial" size="12" style="normal" align="left" weight="bold"/>
              <cpn:offset x="44.5" y="31"/>
            </cpn:graphics>
          </cpn:time>
          <cpn:code>
            <cpn:text/>
            <cpn:graphics>
              <cpn:line color=""/>
              <cpn:font family="arial" size="12" style="normal" align="left" weight="bold"/>
              <cpn:offset x="64.5" y="-52"/>
            </cpn:graphics>
          </cpn:code>
        </pnml:toolspecific>
        <pnml:graphics>
          <pnml:position x="115" y="52"/>
          <pnml:fill color="#ffffff"/>
          <pnml:line color="#000000" width="2" style="solid"/>
          <pnml:dimension x="60.000000" y="40.000000"/>
          <pnml:toolspecific tool="CPN Tools" version="1.0.0">
            <cpn:rectangle/>
          </pnml:toolspecific>
        </pnml:graphics>
      </pnml:transition>  
      <pnml:transition id="ID1412323363">
        <pnml:name>
          <pnml:text>T1</pnml:text>
          <pnml:graphics>
            <pnml:offset x="0" y="0"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:name>
        <pnml:guard>
          <pnml:text/>
          <pnml:graphics>
            <pnml:offset x="-39" y="31"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:guard>
        <pnml:toolspecific tool="CPN Tools" version="1.0.0">
          <cpn:time>
            <cpn:text/>
            <cpn:graphics>
              <cpn:line color=""/>
              <cpn:font family="arial" size="12" style="normal" align="left" weight="bold"/>
              <cpn:offset x="44.5" y="31"/>
            </cpn:graphics>
          </cpn:time>
          <cpn:code>
            <cpn:text/>
            <cpn:graphics>
              <cpn:line color=""/>
              <cpn:font family="arial" size="12" style="normal" align="left" weight="bold"/>
              <cpn:offset x="64.5" y="-52"/>
            </cpn:graphics>
          </cpn:code>
        </pnml:toolspecific>
        <pnml:graphics>
          <pnml:position x="115" y="245"/>
          <pnml:fill color="#ffffff"/>
          <pnml:line color="#000000" width="2" style="solid"/>
          <pnml:dimension x="60.000000" y="40.000000"/>
          <pnml:toolspecific tool="CPN Tools" version="1.0.0">
            <cpn:rectangle/>
          </pnml:toolspecific>
        </pnml:graphics>
      </pnml:transition>  
      <pnml:transition id="ID1412323652">
        <pnml:name>
          <pnml:text>T2</pnml:text>
          <pnml:graphics>
            <pnml:offset x="0" y="0"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:name>
        <pnml:guard>
          <pnml:text/>
          <pnml:graphics>
            <pnml:offset x="-39" y="31"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:guard>
        <pnml:toolspecific tool="CPN Tools" version="1.0.0">
          <cpn:time>
            <cpn:text/>
            <cpn:graphics>
              <cpn:line color=""/>
              <cpn:font family="arial" size="12" style="normal" align="left" weight="bold"/>
              <cpn:offset x="44.5" y="31"/>
            </cpn:graphics>
          </cpn:time>
          <cpn:code>
            <cpn:text/>
            <cpn:graphics>
              <cpn:line color=""/>
              <cpn:font family="arial" size="12" style="normal" align="left" weight="bold"/>
              <cpn:offset x="64.5" y="-52"/>
            </cpn:graphics>
          </cpn:code>
        </pnml:toolspecific>
        <pnml:graphics>
          <pnml:position x="356" y="163"/>
          <pnml:fill color="#ffffff"/>
          <pnml:line color="#000000" width="2" style="solid"/>
          <pnml:dimension x="60.000000" y="40.000000"/>
          <pnml:toolspecific tool="CPN Tools" version="1.0.0">
            <cpn:rectangle/>
          </pnml:toolspecific>
        </pnml:graphics>
      </pnml:transition>  
      <pnml:arc source="ID1412323325" target="ID1412323343" id="ID1412323868pt">
        <pnml:inscription>
          <pnml:text>1</pnml:text>
          <pnml:graphics>
            <pnml:offset x="-153" y="28"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:inscription>
        <pnml:graphics>
          <pnml:line color="#000000" width="2" style="solid"/>
        </pnml:graphics>
        <pnml:arctype>
          <pnml:text>normal</pnml:text>
        </pnml:arctype>
      </pnml:arc>  
      <pnml:arc source="ID1412323325" target="ID1412323363" id="ID1412323936pt">
        <pnml:inscription>
          <pnml:text>1</pnml:text>
          <pnml:graphics>
            <pnml:offset x="-158" y="-41"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:inscription>
        <pnml:graphics>
          <pnml:line color="#000000" width="2" style="solid"/>
        </pnml:graphics>
        <pnml:arctype>
          <pnml:text>normal</pnml:text>
        </pnml:arctype>
      </pnml:arc>  
      <pnml:arc source="ID1412323363" target="ID1412323568" id="ID1412324007tp">
        <pnml:inscription>
          <pnml:text>1</pnml:text>
          <pnml:graphics>
            <pnml:offset x="-63" y="-84"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:inscription>
        <pnml:graphics>
          <pnml:line color="#000000" width="2" style="solid"/>
        </pnml:graphics>
        <pnml:arctype>
          <pnml:text>normal</pnml:text>
        </pnml:arctype>
      </pnml:arc>  
      <pnml:arc source="ID1412323343" target="ID1412323496" id="ID1412324081tp">
        <pnml:inscription>
          <pnml:text>1</pnml:text>
          <pnml:graphics>
            <pnml:offset x="-63" y="109"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:inscription>
        <pnml:graphics>
          <pnml:line color="#000000" width="2" style="solid"/>
        </pnml:graphics>
        <pnml:arctype>
          <pnml:text>normal</pnml:text>
        </pnml:arctype>
      </pnml:arc>  
      <pnml:arc source="ID1412323496" target="ID1412323652" id="ID1412324158pt">
        <pnml:inscription>
          <pnml:text>1</pnml:text>
          <pnml:graphics>
            <pnml:offset x="69" y="58"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:inscription>
        <pnml:graphics>
          <pnml:line color="#000000" width="2" style="solid"/>
        </pnml:graphics>
        <pnml:arctype>
          <pnml:text>normal</pnml:text>
        </pnml:arctype>
      </pnml:arc>  
      <pnml:arc source="ID1412323568" target="ID1412323652" id="ID1412324238pt">
        <pnml:inscription>
          <pnml:text>1</pnml:text>
          <pnml:graphics>
            <pnml:offset x="43" y="-42"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:inscription>
        <pnml:graphics>
          <pnml:line color="#000000" width="2" style="solid"/>
        </pnml:graphics>
        <pnml:arctype>
          <pnml:text>normal</pnml:text>
        </pnml:arctype>
      </pnml:arc>  
      <pnml:arc source="ID1412323652" target="ID1412323751" id="ID1412324321tp">
        <pnml:inscription>
          <pnml:text>1</pnml:text>
          <pnml:graphics>
            <pnml:offset x="174" y="-2"/>
            <pnml:line color="#000000"/>
            <pnml:font family="arial" size="12" style="normal" align="left" weight="normal"/>
          </pnml:graphics>
        </pnml:inscription>
        <pnml:graphics>
          <pnml:line color="#000000" width="2" style="solid"/>
        </pnml:graphics>
        <pnml:arctype>
          <pnml:text>normal</pnml:text>
        </pnml:arctype>
      </pnml:arc> 
    </pnml:page>
  </pnml:net>
</pnml:pnml>
bool pnmlparse::parse(QString xmlPath, QVector<pnmlpoint> &pnmlV)
{
	QFile xmlFile(xmlPath);
	if (!xmlFile.open(QFile::ReadOnly)) {
		qDebug() << "transaction xml file open failed";
		return false;
	}
	QXmlStreamReader reader(&xmlFile);
	QString s = reader.name().toString();
	bool b = false;
	int line = 0;
	while (!reader.atEnd())
	{
		pnmlpoint pp;
		s = reader.name().toString();

		if (s == "place") {
			pp.type = pointtype::pt_place;
            pp.id = reader.attributes().value("id").toString();
			s = reader.name().toString();
			while (!reader.isEndElement())
			{
				b = reader.readNextStartElement();
				line = reader.lineNumber();
				s = reader.name().toString();
				if (reader.isEndElement() && s == "place") {
					break;
				}
				if (s == "name") {
					while (!reader.isEndElement())
					{
						b = reader.readNextStartElement();
						line = reader.lineNumber();
						s = reader.name().toString();
						if (reader.isEndElement() && s == "name") {
							break;
						}
						if (!b)
						{
							reader.readNext();
							continue;
						}
						if (s == "text") {
							line = reader.lineNumber();
							pp.text = reader.readElementText();
							b = reader.readNextStartElement();
						}
					}
				}
				if(s == "initialMarking")
					jumpelement(reader, s);
				if (s == "type")
					jumpelement(reader, s);
				if (s == "graphics") {
					while (!reader.isEndElement())
					{
						bool b = reader.readNextStartElement();
						line = reader.lineNumber();
						s = reader.name().toString();
						if (reader.isEndElement() && s == "graphics") {
							break;
						}
						if (!b)
						{
							reader.readNext();
							continue;
						}
						if (s == "position") {
							int x = reader.attributes().value("x").toInt();
							int y = reader.attributes().value("y").toInt();
							pp.p = QPoint(x, y);
						}
					}
				}
				if (!b)
					reader.readNext();
			}
		}

		if (s == "transition") {
			pp.type = pointtype::pt_transition;
            pp.id = reader.attributes().value("id").toString();
			s = reader.name().toString();
			while (!reader.isEndElement())
			{
				b = reader.readNextStartElement();
				line = reader.lineNumber();
				s = reader.name().toString();
				if (reader.isEndElement() && s == "place") {
					break;
				}
				if (s == "name") {
					while (!reader.isEndElement())
					{
						b = reader.readNextStartElement();
						line = reader.lineNumber();
						s = reader.name().toString();
						if (reader.isEndElement() && s == "name") {
							break;
						}
						if (!b)
						{
							reader.readNext();
							continue;
						}
						if (s == "text") {
							line = reader.lineNumber();
							pp.text = reader.readElementText();
							b = reader.readNextStartElement();
						}
					}
				}
				if (s == "guard")
					jumpelement(reader, s);
				if (s == "toolspecific")
					jumpelement(reader, s);
				if (s == "graphics") {
					while (!reader.isEndElement())
					{
						bool b = reader.readNextStartElement();
						line = reader.lineNumber();
						s = reader.name().toString();
						if (reader.isEndElement() && s == "graphics") {
							break;
						}
						if (!b)
						{
							reader.readNext();
							continue;
						}
						if (s == "position") {
							int x = reader.attributes().value("x").toInt();
							int y = reader.attributes().value("y").toInt();
							pp.p = QPoint(x, y);
						}
					}
				}
				if (!b)
					reader.readNext();
			}
		}

		if (s == "arc" && reader.isStartElement()) {
            QString srcid = reader.attributes().value("source").toString();
            QString dstid = reader.attributes().value("target").toString();
            pp.hasArrow = true;
            pp.type = pt_arc;
            for (auto it : pnmlV) {
                if(it.id == srcid) {
                    pp.src = it.p;
                }
                if(it.id == dstid) {
                    pp.dst = it.p;
                }
            }
		}

		if (!pp.text.isEmpty() || pp.hasArrow)
			pnmlV.append(pp);

		reader.readNext();
	}

	return true;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值