[問題] TinyXMl2適合使用讀取XML嗎

作者: bellman (Richard)   2018-10-29 20:03:34
開發平台(Platform): (Ex: Win10, Linux, ...)
Win8 Win10
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
GCC MinGW?
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
TinyXML2
問題(Question):
<Staff>
<Name> David Lee </Name>
<ID> 1234567 </ID>
<age> 38 </age>
<Department> CS </Department>
<Salary> 100000</Salary>
</Staff>
<Staff>
<Name> Jeff Wu </Name >
<ID> 1234568 </ID>
<age> 50 </age>
<Department> EE </Department>
<Salary> 200000</Salary>
</Staff>
以上兩個檔案要轉為
<Staff1>
Name: David Lee
ID: 1234567
Age: 38
Department: CS
Salary: 100000
<Staff2>
Name: Jeff Wu
ID: 1234568
age: 50
Department: EE
Salary: 200000
請問這樣適合嗎?
餵入的資料(Input):
<Staff>
<Name> David Lee </Name>
<ID> 1234567 </ID>
<age> 38 </age>
<Department> CS </Department>
<Salary> 100000</Salary>
</Staff>
預期的正確結果(Expected Output):
<Staff1>
Name: David Lee
ID: 1234567
Age: 38
Department: CS
Salary: 100000
錯誤結果(Wrong Output):
完全沒畫面
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
https://ideone.com/20IEkW
//以上跑不動 因為沒有引進tinyXML2
void TTT(){
tinyxml2::XMLDocument docXml;
XMLError errXml = docXml.LoadFile("../oldData.xml");
if (XML_SUCCESS == errXml){
XMLElement* elmtRoot = docXml.RootElement();
XMLElement *elmtStaff = elmtRoot->FirstChildElement("Staff");
XMLElement *elmtName = elmtStaff->FirstChildElement("Name");
if (elmtName)
{
const char* pContent = elmtName->GetText();
printf( "%s", pContent);
}
XMLElement *elmtID = elmtName->NextSiblingElement("ID");
if (elmtID)
{
const char* pContent= elmtID->GetText();
printf( "%s", pContent);
}
XMLElement *elmtDepartment = elmtID->NextSiblingElement("Department");
if (elmtDepartment)
{
const char* pContent= elmtDepartment->GetText();
printf( "%s", pContent);
}
XMLElement *elmtSalary = elmtDepartment->NextSiblingElement("Salary");
if (elmtSalary)
{
const char* pContent= elmtSalary->GetText();
printf( "%s", pContent);
}
XMLElement *elmtAge = elmtName->NextSiblingElement();
if (elmtAge)
{
const char* pContent= elmtAge->GetText();
printf( "%s", pContent);
}
}
}
補充說明(Supplement):
http://www.grinninglizard.com/tinyxml2/
作者: enonrick (EnonRick)   2018-10-30 09:20:00
tinyxml沒問題,還有更快的pugixml可以用,但簡單結構建議json就足夠
作者: chchwy (mat)   2018-10-30 11:17:00
tinyxml2適不適合讀取xml? 不然tinyxml2是拿來讀什麼?
作者: achicn3 (Sher)   2018-10-30 15:43:00
樓上XD
作者: MOONRAKER (㊣牛鶴鰻毛人)   2018-10-31 01:33:00
json不json顯然不是他可以選擇的。你接API的時候會跟他們說「天啊還用什麼XML肥皂(SOAP),換json啦」嗎?
作者: askacis (ASKA)   2018-10-31 13:04:00
用過tinyxml跟minixml,處理簡單xml還算方便,但是遇到大型專案,用gsoap直接從xsd長出來骨架code還是比這兩個方便快速很多

Links booklink

Contact Us: admin [ a t ] ucptt.com