读取XML文件使用QxmlStreamReader

嵌套的标签下面

我试图读取一个标签嵌套的xml文件,是我的xml文件读取XML文件使用QxmlStreamReader

<?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="[email protected]">

<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="[email protected]">

<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个标记循环,然后开始下一个,例如,我想读取标记标题并保存在结构中,然后通过body等循环在下面的代码时,头开始,当身体等

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上面的代码永远不会在“身体”和“tbaData”

回答:

最后我设法通过标签的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();

}

}

}

}

}

}

}

}

}

}

希望它会对某人有帮助。

以上是 读取XML文件使用QxmlStreamReader 的全部内容, 来源链接: utcz.com/qa/265606.html

回到顶部