bool CreateXML(const char *xml_file)
{
// 定義一個TiXmlDocument類指針
TiXmlDocument *document = new TiXmlDocument;
if (NULL == document)
{
return false;
}
// 定義一個xml文件頭部聲明
TiXmlDeclaration *declaration = new TiXmlDeclaration(("1.0"),(""),(""));
if (NULL == declaration)
{
return false;
}
document->LinkEndChild(declaration);
// 生成一個根節點:MyApp
TiXmlElement *first_element = new TiXmlElement(("MyApp"));
if (NULL == first_element)
{
return false;
}
document->LinkEndChild(first_element);
// 生成子節點:Messages
TiXmlElement *child_element_messages = new TiXmlElement(("Messages"));
if (NULL == child_element_messages)
{
return false;
}
first_element->LinkEndChild(child_element_messages);
// 生成子節點:Welcome
TiXmlElement *child_element_welcome = new TiXmlElement(("Welcome"));
if (NULL == child_element_welcome)
{
return false;
}
child_element_messages->LinkEndChild(child_element_welcome);
// 設置Welcome節點的值
const char* str_value = ("Welcome to MyApp");
TiXmlText *welcome_value = new TiXmlText(str_value);
child_element_welcome->LinkEndChild(welcome_value);
// 生成子節點:Farewell
TiXmlElement *child_element_farewell = new TiXmlElement(("Farewell"));
if (NULL == child_element_farewell)
{
return false;
}
child_element_messages->LinkEndChild(child_element_farewell);
// 設置Farewell節點的值
str_value = ("Thank you for using MyApp");
TiXmlText *farewell_value = new TiXmlText(str_value);
child_element_farewell->LinkEndChild(farewell_value);
// 生成子節點:Windows
TiXmlElement *child_element_windows = new TiXmlElement(("Windows"));
if (NULL == child_element_windows)
{
return false;
}
first_element->LinkEndChild(child_element_windows);
// 生成子節點:Window
TiXmlElement *child_element_window = new TiXmlElement(("Window"));
if (NULL == child_element_window)
{
return false;
}
child_element_windows->LinkEndChild(child_element_window);
// 設置節點Window的值
child_element_window->SetAttribute(("name"),("MainFrame"));
child_element_window->SetAttribute(("x"),("5"));
child_element_window->SetAttribute(("y"),("15"));
child_element_window->SetAttribute(("w"),("400"));
child_element_window->SetAttribute(("h"),("250"));
// 生成子節點:Connection
TiXmlElement *child_element_connection = new TiXmlElement(("Connection"));
if (NULL == child_element_connection)
{
return false;
}
first_element->LinkEndChild(child_element_connection);
// 設置節點Connection的值
child_element_connection->SetAttribute(("ip"),("192.168.0.1"));
child_element_connection->SetAttribute(("timeout"),("123.456000"));
// 保存至文件
document->SaveFile(xml_file);
// 輸出到控制臺
cout << "Creat XML:" << endl;
document->Print(stdout);
return true;
}
</td>
</tr>
</tbody>
</table>
編譯后執行可以看到控制臺有如下輸出:
# ./xml_test
Creat XML:
<?xml version="1.0" ?>
<MyApp>
<Messages>
<Welcome>Welcome to MyApp</Welcome>
<Farewell>Thank you for using MyApp</Farewell>
</Messages>
<Windows>
<Window name="MainFrame" x="5" y="15" w="400" h="250" />
</Windows>
<Connection ip="192.168.0.1" timeout="123.456000" />
</MyApp></pre>
并且在目錄下生成一個XML文件如下:
<?xml version="1.0" ?>
<MyApp>
<Messages>
<Welcome>Welcome to MyApp</Welcome>
<Farewell>Thank you for using MyApp</Farewell>
</Messages>
<Windows>
<Window name="MainFrame" x="5" y="15" w="400" h="250" />
</Windows>
<Connection ip="192.168.0.1" timeout="123.456000" />
</MyApp></pre>
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
|