使用QXmlStreamReader qt

时间:2018-04-28 12:50:02

标签: qt xml-parsing qxmlstreamreader

我正在尝试解析QString,如下所示:

<unit>
 <unit_type>110A</unit_type>
 <manufacturer>ABC</manufacturer>
 <article_number>9900</article_number>
 <production_date>2008-05-29</production_date>
 <delivery_revision>03B</delivery_revision>
 <present_revision>03B</present_revision>
 <rev_date>2008-05-29</rev_date>
  <made_the_last_rev>B21</made_the_last_rev>
  <serial_number>530029</serial_number>
 <eth_addresses>
 <eth_address>00:06:30:03:0c:0b</eth_address>
 </eth_addresses>
 <boards>
 <board>
  <board_type>411C</board_type>
  <article_number>9910</article_number>
  <production_date>2008-05-29</production_date>
  <delivery_revision>01A</delivery_revision>
  <present_revision>01A</present_revision>
  <rev_date>2008-05-29</rev_date>
  <made_the_last_rev>B21</made_the_last_rev>
  <serial_number>0460044</serial_number>
  </board>
  <board>200Z4
  <board_type>731A</board_type>
  <article_number>3-474</article_number>
  <serial_number>200Z4</serial_number>
 </board>
 <board>
  <board_type>401B</board_type>
  <article_number>04418</article_number>
  <production_date>2008-05-28</production_date>
  <delivery_revision>01B</delivery_revision>
  <present_revision>01B</present_revision>
  <rev_date>2008-05-28</rev_date>
  <made_the_last_rev>B21</made_the_last_rev>
  <serial_number>1370017</serial_number>
  </board>
  </boards>
  </unit>
  <unit2 version="1" release="0" update="0" evolution="0" >
  <unit_type>4533</unit_type>
  <manufacturer>AH</manufacturer>
  <article_number>34030</article_number>
  <production_date>2017-07-27</production_date>
  <delivery_revision>01A</delivery_revision>
 <present_revision>P2</present_revision>
 <rev_date>2017-07-27</rev_date>
 <made_the_last_rev>CH</made_the_last_rev>
 <serial_number>300000042</serial_number>
<eth_addresses>
 <eth_address>C0:35:80:00:10:96</eth_address>
 <eth_address>C0:35:80:00:10:97</eth_address>
 </eth_addresses>
<boards>
 <board>
 <board_type>MAINBOARD</board_type>
 <article_number>0BEDR</article_number>
 <production_date>2017-07-19</production_date>
 <serial_number>2626</serial_number>
</board>
<board>
<board_type>TIO</board_type>
<article_number>00SMDTOP</article_number>
<production_date>2017-07-15</production_date>
<serial_number>0599</serial_number>
</board>
<board>
<board_type>TFT</board_type>
<article_number>MTI-HL</article_number>
<production_date>2017-07-10</production_date>
<serial_number>31307</serial_number>
</board>

  

我的功能如下:

void VrsRequesterWrapper::parseHardwareData(QString pData)
 {
qDebug() << " parseHardwareData ";
QXmlStreamReader reader(pData);
qDebug() << " first Item #1" << reader.name();
if(reader.readNextStartElement())
{
    qDebug() << " first Item #2" << reader.name();
    while(reader.name() == "unit2") {
        if(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE  #1 " << reader.name();
            reader.skipCurrentElement();
        }
        if(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE  #2" << reader.name();
            reader.skipCurrentElement();
        }
        if(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE  #3" << reader.name();
            reader.skipCurrentElement();
        }
        if(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE  #4" << reader.name();
            reader.skipCurrentElement();
        }
        if(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE  #5" << reader.name();
            reader.skipCurrentElement();
        }
        if(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE  #6" << reader.name();
            reader.skipCurrentElement();
        }
        if(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE  #7" << reader.name();
            reader.skipCurrentElement();
        }
        if(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE  #8" << reader.name();
            reader.skipCurrentElement();
        }
        if(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE  #9" << reader.name();
            reader.skipCurrentElement();
        }
        qDebug() << "DATA VALUE  #10" << reader.name();
        while(reader.readNextStartElement()){
            if(reader.name() == "eth_addresses"){
                while(reader.readNextStartElement()) {
                    qDebug() << "DATA VALUE Ethernet #1" << reader.name();
                    reader.skipCurrentElement();
                }
            }
        }
        qDebug() << "DATA VALUE Board while #1" << reader.name();
        while(reader.readNextStartElement()) {
            qDebug() << "DATA VALUE Board while #2" << reader.name();
            if(reader.name() == "boards"){

                qDebug() << "DATA VALUE Board while #3" << reader.name();
               while(reader.readNextStartElement()) {
                        if(reader.readNextStartElement()) {
                            qDebug() << "DATA VALUE Boards #1" << reader.name();
                            reader.skipCurrentElement();
                        }
                        if(reader.readNextStartElement()) {
                            qDebug() << "DATA VALUE Boards #2" << reader.name();
                            reader.skipCurrentElement();
                        }
                        if(reader.readNextStartElement()) {
                            qDebug() << "DATA VALUE Boards #3" << reader.name();
                            reader.skipCurrentElement();
                        }
                        if(reader.readNextStartElement()) {
                            qDebug() << "DATA VALUE Boards #4" << reader.name();
                            reader.skipCurrentElement();
                        }
                        if(reader.readNextStartElement()) {
                            qDebug() << "DATA VALUE Boards #5" << reader.name();
                            reader.skipCurrentElement();
                        }
                        if(reader.readNextStartElement()) {
                            qDebug() << "DATA VALUE Boards #6" << reader.name();
                            reader.skipCurrentElement();
                        }
                        if(reader.readNextStartElement()) {
                            qDebug() << "DATA VALUE Boards #7" << reader.name();
                            reader.skipCurrentElement();
                        }
                        if(reader.readNextStartElement()) {
                            qDebug() << "DATA VALUE Boards #8" << reader.name();
                            reader.skipCurrentElement();
                        }
                    }
            }
        }
    }
}

}

然而,我无法完全解析“board”标签..会很好如果我能得到一些提示让它不要。此外,电路板标签的数量可能会有所不同,有些用于eth_addresses,例如,上面示例中的电路板标签为2和3.

1 个答案:

答案 0 :(得分:0)

最后我设法解决了它,下面是代码片段。希望它对其他人有用:

Python