2012-11-13 76 views
0

我使用QXmlStreamWriter生成一個xml文件。該文件是這樣的:使用QDomDocument讀取xml文件只需獲取第一行

<?xml version="1.0" encoding="UTF-8"?> 
<pedestrianinfo> 
    <pedestrian uuid="2112e2ed-fc9b-41e8-bbcb-b44ad78bde11"> 
     <module>11.1208</module> 
     <direction>4</direction> 
     <row>5</row> 
     <column>71</column> 
    </pedestrian> 
    <pedestrian uuid="1aabb9c1-4aa7-4f47-9542-36d2dfaa26e4"> 
     <module>1.48032</module> 
     <direction>4</direction> 
     <row>67</row> 
     <column>31</column> 
    </pedestrian> 

... 

</pedestrianinfo> 

然後我嘗試通過QDomDocument閱讀內容。我的代碼如下所示:

xmlReader *xp = new xmlReader(QString("D:\\0T.xml")); 
    if(xp->openFile()) { 
     if(xp->isGetRootIndex()) { 
      xp->parseRootIndexElement(); 
     } 
     else 
      cout<<"Unable to get root index."<<endl; 
    } 

這裏是isGetRootIndex():

bool xmlReader::isGetRootIndex() 
{ 
    doc.setContent(&file,false); 
    root = doc.documentElement(); 
    if(root.tagName() == getRootIndex()) //rootIndex=="pedestrianinfo" 
     return true; 
    return false; 
} 

這是parseRootIndexElement():

void xmlReader::parseRootIndexElement() 
{ 
    QDomNode child = root.firstChild(); 
    while(!child.isNull()) { 
     if(child.toElement().tagName() == getTagNameP()) //"childTagName=="pedestrian" 
      parseEntryElement(child.toElement()); 
     qDebug()<<"module="<<module<<" direction="<<direction<<" row="<<row<<" column="<<column; 
     child = child.nextSibling(); 
    } 
} 

parseEntryElement(常量QDomElement &元素)是一個功能獲取每個標籤中的信息並將其保存到模塊等變量中。 但是,每次運行我的代碼時,只有xml文件的第一個子代可能是qDebug * ed *。看起來在執行child.nextSibling()之後,child變爲null。爲什麼它沒有得到下一個行人信息?

回答

0

根據我在文檔中看到的內容,看起來是正確的。也許parseEntryElement意外推進迭代器?

+0

感謝您的回覆。今天我發現了一個可怕的錯誤。該程序讀取'舊'xml文件,而不是正確的。哎呀。該代碼現在適用於我。 – user957121