使用QxmlStreamReader读取带有嵌套标记的xml文件

时间:2017-12-27 14:14:43

标签: xml qt qxmlstreamreader

我正在尝试读取具有嵌套标记的xml文件,下面是我的xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<root> 
<Header>
<Hvers>1.0.0.0</Hvers>
<SvVers>3.1.0.0</SvVers>
<CID>d3911</CstID>
<CrID>d3911</CarID>
<DevID>cdp1</DevID>
<SrvNm>VrsService</SrvNm>
<Date>170516</Date>
<Time>134838</Time>
<PrjInf>none</PrjInf>
   </Header>
<Body>
<TBA>
  <tbaData uri="hardware.i2@mcg1.d3911.d3911">
    <unit2 version="1" release="0" update="0" evolution="0" >
      <unit_type>DCY 1550A</unit_type>
      <article_number>3EST000225-5711</article_number>
      <production_date>2013-11-27</production_date>
      <delivery_revision>06A</delivery_revision>
      <present_revision>06A</present_revision>
      <rev_date>2013-11-27</rev_date>
      <made_the_last_rev>B21</made_the_last_rev>
      <serial_number>B21-13464250019</serial_number>
      <eth_addresses>
        <eth_address>00:06:30:03:2D:A2</eth_address>
        <eth_address>00:06:30:03:EE:02</eth_address>
      </eth_addresses>
      <boards>
        <board>
          <board_type>DTBB 411F</board_type>
          <article_number>3EST000218-1223</article_number>
          <production_date>2013-11-27</production_date>
          <delivery_revision>03A</delivery_revision>
          <present_revision>03A</present_revision>
          <rev_date>2013-11-27</rev_date>
          <made_the_last_rev>B21</made_the_last_rev>
          <serial_number>B21-13445430010</serial_number>
        </board>
        <board>
          <board_type>DTPC 401B</board_type>
          <article_number>3EST000212-2418</article_number>
          <production_date>2013-11-20</production_date>
          <delivery_revision>05A</delivery_revision>
          <present_revision>05A</present_revision>
          <rev_date>2013-11-20</rev_date>
          <made_the_last_rev>B21</made_the_last_rev>
          <serial_number>B21-13459400179</serial_number>
          <eth_addresses>
            <eth_address>00:06:30:03:2D:A2</eth_address>
          </eth_addresses>
        </board>
        <board>
          <board_type>DTMB 403A</board_type>
          <article_number>3EST000218-1237</article_number>
          <production_date>2013-11-09</production_date>
          <delivery_revision>02A</delivery_revision>
          <present_revision>02A</present_revision>
          <rev_date>2013-11-09</rev_date>
          <made_the_last_rev>B21</made_the_last_rev>
          <serial_number>B21-13447030057</serial_number>
          <eth_addresses>
            <eth_address>00:06:30:03:EE:02</eth_address>
          </eth_addresses>
        </board>
      </boards>
    </unit2>
  </tbaData>
  <tbaData uri="software.i1@AMP1.d3911.d3911">
    <system><component><type>Application</type><name>SCINumErr</name> <version>0.0.0.0</version><created>0</created></component></system>
  </tbaData>
 </TBA>

</Body>
</root>

我想要阅读标签,但缺少结构化的方式来阅读和填充我的结构。

我正在读取xml文件,如下所示,它返回所有标签而不是我想循环通过1个标签,然后开始下一个,例如,我想读取标签标题并保存在结构中,然后循环遍历正文等。在下面的代码中,我不明白Header何时开始以及何时出现身体等。

void test::readXml()
{

    QFile file("C:/VRC_Collector/VRS_Collector/deploy/all/conf/dpa/VRS_Collector/vrsdata.xml");
    if(!file.open(QIODevice::ReadOnly | QFile::Text)){
        qDebug()<<"File Openning Error"<< file.errorString();
    }
    QXmlStreamReader reader(&file);
    reader.readNext();
    while(!reader.atEnd()){
        // reader.readNextStartElement();
        qDebug()<< "root1" << reader.name();
        if(reader.readNextStartElement()){
            if(reader.name() == "root")
            {
                qDebug()<< "root";
                reader.readNext();
            }else if(reader.name() == "Header") {
                qDebug()<< "header";
                reader.readNext();
            }
            else if(reader.name() == "Body") {
                qDebug()<< "Body";
                reader.readNext();
            }else if(reader.name() == "tbaData") {
                qDebug()<< "tbaData";
                reader.readNext();
            } else {
                qDebug()<< "else";
                reader.readNext();
            }
        }
    }
}

我希望我的问题清楚,我现在还没有把结构填入代码中,只使用了qDebug()。

P.S上面的代码永远不会出现在“Body”和“tbaData”

1 个答案:

答案 0 :(得分:2)

最后我设法通过标签解析xml标签,下面是代码片段:

QXmlStreamReader reader;
QFile file("C:/data.xml");
if(!file.open(QIODevice::ReadOnly | QFile::Text)){
    qDebug()<<"File Openning Error"<< file.errorString();
}
reader.setDevice(&file);
if(reader.readNextStartElement()) {
    if(reader.name() == "root"){
        qDebug()<<"Root";
        while(reader.readNextStartElement()){
            if(reader.name() == "Header"){
                qDebug()<< "Header" << reader.name().toString();
                while(reader.readNextStartElement()){
                    if(reader.name() == "DevID") {
                        qDebug()<< "DevID" << reader.readElementText();
                    } else {
                        reader.skipCurrentElement();
                    }
                    //qDebug()<< "Header Elements" << reader.name();
                }
            }
            while(reader.readNextStartElement()) {
                if(reader.name() == "Body") {
                    while(reader.readNextStartElement()) {
                        if(reader.name() == "TBA") {
                            while(reader.readNextStartElement()) {
                                if(reader.name() == "tbaData") {
                                    qDebug()<< "unit_type" << reader.name();
                                    while(reader.readNextStartElement()){
                                        qDebug()<< "unit_type #1" << reader.name();
                                        if(reader.name() == "unit2"){
                                            while(reader.readNextStartElement()) {
                                                if(reader.name() == "unit_type"){
                                                    qDebug()<< "unit_type" << reader.readElementText();
                                                } else {
                                                    reader.skipCurrentElement();
                                                }
                                            }
                                        }
                                        qDebug()<< "Check 1" << reader.name();
                                        reader.skipCurrentElement();
                                        while(reader.readNextStartElement()) {
                                            if(reader.name() == "tbaData") {
                                                while(reader.readNextStartElement()) {
                                                    if(reader.name() == "system") {
                                                        while(reader.readNextStartElement()) {
                                                           reader.skipCurrentElement();
                                                        }
                                                    } else {
                                                        reader.skipCurrentElement();
                                                    }
                                                }
                                                while(reader.readNextStartElement()) {
                                                    if(reader.name() == "system2") {
                                                        while(reader.readNextStartElement()) {
                                                            if(reader.name() == "name") {
                                                            qDebug()<< "system2 " << reader.name();
                                                            } else {
                                                                reader.skipCurrentElement();
                                                            }
                                                            while(reader.readNextStartElement()) {
                                                                if(reader.name() == "edPackage") {
                                                                    while(reader.readNextStartElement()) {
                                                                        if(reader.name() == "name") {
                                                                            qDebug()<< "reader  ss" << reader.name();
                                                                        } else {
                                                                            reader.skipCurrentElement();
                                                                        }
                                                                    }
                                                                } else {
                                                                    if(reader.name() == "sci") {
                                                                        while(reader.readNextStartElement()) {
                                                                            if(reader.name() == "name") {
                                                                                while(reader.readNextStartElement()) {
                                                                                qDebug()<< "sci name" << reader.name();
                                                                                }
                                                                            } else {
                                                                                if(reader.name() == "component") {
                                                                                    while(reader.readNextStartElement()) {
                                                                                        qDebug()<< "sci name 1" << reader.name();
                                                                                    }
                                                                                }
                                                                                 reader.skipCurrentElement();
                                                                            }
                                                                        }
                                                                    }
                                                                    qDebug()<< "reader  ss1" << reader.name();
                                                                    reader.skipCurrentElement();
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                reader.skipCurrentElement();
                                                // qDebug()<< "Check 2" << reader.name();
                                            }
                                        }
                                        reader.readNextStartElement();

                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

希望对某人有所帮助。