2011-10-01 99 views
0


我目前正在使用C++編寫一個項目,我需要從xml文件讀取一些東西,我已經想出了tinyxml接縫是要走的路,但我仍然不知道如何去做。
另外我的xml文件有點棘手,因爲它需要使用它的每個用戶看起來有點不同。如何將xml粘貼到C++(Tinyxml)

的XML文件,我需要閱讀看起來像這樣

<?xml version="1.0" encoding="utf-8"?> 
<cloud_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xmlns:d="http://www.kuju.com/TnT/2003/Delta" d:version="1.0"> 
    <cCareerModel d:id="154964152"> 
     <ScenarioCareer> 
      <cScenarioCareer d:id="237116344"> 
       <IsCompleted d:type="cDeltaString">CompletedSuccessfully</IsCompleted> 
       <BestScore d:type="sInt32">0</BestScore> 
       <LastScore d:type="sInt32">0</LastScore> 
       <ID> 
        <cGUID> 
         <UUID> 
          <e d:type="sUInt64">5034713268864262327</e> 
          <e d:type="sUInt64">2399721711294842250</e> 
         </UUID> 
         <DevString d:type="cDeltaString">0099a0b7-e50b-45de-8a85-85a12e864d21</DevString> 
        </cGUID> 
       </ID> 
      </cScenarioCareer> 
     </ScenarioCareer> 
     <MD5 d:type="cDeltaString"></MD5> 
    </cCareerModel> 
</cloud_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx> 

現在,這個計劃的目標是能夠插入一些字符串,並serch主頁的相應的「cScenarioCarrer d(通過一個變量。): ID「並閱讀」IsComplete「和」BestScore「。

這些字符串後來需要在我的程序中使用,但我可以處理。

我的問題,這裏有

A.如何去通過搜索特定的「cScenarioCareer」 ID

B.如何粘貼「IsComplete」和「BestScore」到一些變量我程序。

注意:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx字符串對於每個用戶都是唯一的,因此請記住它可以是任何內容。

如果有人願意幫助我,我會非常優雅,謝謝。

PS。我想對我在這裏做的事情有一些瞭解,儘管「將這些代碼粘貼到你的程序中」的答案是可以接受的,但我認爲如果你能告訴我它是如何工作的,以及它爲什麼會起作用,情況會好得多。

+0

這聽起來像你真正想要的是[TinyXPath](http://sourceforge.net/projects/tinyxpath/?_test=beta)。 –

回答

1

既然你在C++中這樣做,我將使用ticpp接口來創建這個例子,以 TinyXml在ticpp.googlecode.com上可用。

假設:

  • 給定的XML文件將包含一個<cloud>標記和多個 <cCareerModel>標籤。
  • 每個<cCareerModel>包含單個<ScenarioCareer>標籤又包含一個<cScenarioCareer>標籤
  • 你在解析XML文件到一個名爲xmlDoc中
  • 你不需要檢查數據類型TiXmlDocument屬性
  • 你不介意使用異常

我還假設你有一個上下文變量某處包含一個指向 <cloud>標籤,就像這樣:

ticpp::Element* cloud = xmlDoc.FirstChildElement("cloud"); 

這裏有一個函數,它將爲cScenarioCareer找到給定ID爲 的ticpp :: Element。

ticpp::Element* findScenarioCareer(const std::string& careerId) 
{ 
    try 
    { 
    // Declare an iterator to access all of the cCareerModel tags and construct an 
    // end iterator to terminate the loop 
    ticpp::Iterator<ticpp::Element> careerModel; 
    const ticpp::Iterator<ticpp::Element> modelEnd = careerModel.end(); 

    // Loop over the careerModel tags 
    for (careerModel = cloud->FirstChildElement() ; careerModel != modelEnd ; 
     ++careerModel) 
    { 
     // Construct loop controls to access careers 
     ticpp::Iterator<ticpp::Element> career; 
     const ticpp::Iterator<ticpp::ELement> careerEnd = career.end(); 

     // Loop over careers 
     for (career = careerModel->FirstChildElement("ScenarioCareer").FirstChildElement() ; 
      career != careerEnd ; ++career) 
     { 
     // If the the d:id attribute value matches then we're done 
     if (career->GetAttributeOrDefault("d:id", "") == careerId) 
      return career; 
     } 
    } 
    } 
    catch (const ticpp::Exception&) 
    { 
    } 
    return 0; 
} 

然後讓你想你會做類似的信息:

std::string careerId = "237116344"; 
std::string completion; 
std::string score; 
ticpp::Element* career = findScenarioCareer(careerId); 
if (career) 
{ 
    try 
    { 
    completion = career->FirstChildElement("IsCompleted")->GetText(); 
    score = career->FirstChildElement("BestScore")->GetText(); 
    } 
    catch (const ticpp::Exception&) 
    { 
    // Handle missing element condition 
    } 
} 
else 
{ 
    // Not found 
} 

當然我還沒有編譯或測試任何這一點,但它應該給你的想法。