0

我正在尝试读取具有嵌套标签的 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 并保存在结构中,然后循环遍历正文等。在下面代码我不明白 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()。

PS 上面的代码永远不会用于“Body”和“tbaData”

4

1 回答 1

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();

                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

希望它对某人有帮助。

于 2017-12-28T09:34:10.483 回答