VC中tinyxml使用方法
来源:百度文库 编辑:神马文学网 时间:2024/04/28 15:50:50
VC中tinyxml使用方法
1.加载文件。
TiXmlDocument doc( "demo.xml" );
doc.LoadFile();
2.
void main(void)
{
TiXmlDocument doc("example1.xml");
bool loadOkay = doc.LoadFile();
if (loadOkay)
{
printf("\n%s:\n", pFilename);
dump_to_stdout( &doc ); // defined later in the tutorial
}
else
{
printf("Failed to load file \"%s\"\n", pFilename);
}
return;
}
example1.xml 的内容如果是:
World
输出为:
DOCUMENT
+ DECLARATION
+ ELEMENT Hello
+ TEXT[World]
3.建立文档的方法.
void build( )
{ TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
TiXmlElement * element = new TiXmlElement( "Hello" );
TiXmlText * text = new TiXmlText( "World" );
element->LinkEndChild( text );
doc.LinkEndChild( decl );
doc.LinkEndChild( element );
doc.SaveFile( "example1.xml" );
}
4.设定节点属性。
TiXmlElement window = new TiXmlElement( "Demo" );
window->SetAttribute("name", "Circle");
window->SetAttribute("x", 5);
window->SetAttribute("y", 15);
window->SetDoubleAttribute("radius", 3.14159);
5.获取元素的所有属性,并打印出属性名称和值
int printElement(TiXmlElement* pElement, unsigned int indent)
{
if ( !pElement ) return 0;
TiXmlAttribute* pAttrib=pElement->FirstAttribute();
int i=0;
int ival;
double dval;
const char* pIndent=getIndent(indent);
printf("\n");
while (pAttrib)
{
printf( "%s%s: value=[%s]", pIndent, pAttrib->Name(), pAttrib->Value());
if (pAttrib->QueryIntValue(&ival)==TIXML_SUCCESS) printf( " int=%d", ival);
if (pAttrib->QueryDoubleValue(&dval)==TIXML_SUCCESS) printf( " d=%1.1f", dval);
printf( "\n" );
i++;
pAttrib=pAttrib->Next();
}
return i;
}
6.写入文件,其实上面已经用到了。
doc.SaveFile( saveFilename );
7.建立一个其内容如下的文档:
Welcome to MyApp
Thank you for using MyApp
void main( )
{
TiXmlDocument doc;
TiXmlElement* msg;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); //文档声明
doc.LinkEndChild( decl );
TiXmlElement * root = new TiXmlElement( "MyApp" );
doc.LinkEndChild( root ); //根元素
TiXmlComment * comment = new TiXmlComment();//xml注释
comment->SetValue(" Settings for MyApp " );
root->LinkEndChild( comment ); //插入根元素之间
TiXmlElement * msgs = new TiXmlElement( "Messages" );
root->LinkEndChild( msgs ); //定义元素Messages,插入到root
msg = new TiXmlElement( "Welcome" ); //定义新元素,并插入到msgs
msg->LinkEndChild( new TiXmlText( "Welcome to MyApp" ));
msgs->LinkEndChild( msg );
msg = new TiXmlElement( "Farewell" ); //定义新元素,并插入到msgs
msg->LinkEndChild( new TiXmlText( "Thank you for using MyApp" ));
msgs->LinkEndChild( msg );
TiXmlElement * windows = new TiXmlElement( "Windows" );
root->LinkEndChild( windows ); //root中插入新元素windows
TiXmlElement * window;
window = new TiXmlElement( "Window" );
windows->LinkEndChild( window ); //定义新元素,并设定其属性。
window->SetAttribute("name", "MainFrame");
window->SetAttribute("x", 5);
window->SetAttribute("y", 15);
window->SetAttribute("w", 400);
window->SetAttribute("h", 250);
TiXmlElement * cxn = new TiXmlElement( "Connection" );
root->LinkEndChild( cxn );
cxn->SetAttribute("ip", "192.168.0.1");
cxn->SetDoubleAttribute("timeout", 123.456); // floating point attrib
dump_to_stdout( &doc );
doc.SaveFile( "appsettings.xml" ); //保存文件
}
8.对象到XML的转换。
///class
#include
#include
1.加载文件。
TiXmlDocument doc( "demo.xml" );
doc.LoadFile();
2.
void main(void)
{
TiXmlDocument doc("example1.xml");
bool loadOkay = doc.LoadFile();
if (loadOkay)
{
printf("\n%s:\n", pFilename);
dump_to_stdout( &doc ); // defined later in the tutorial
}
else
{
printf("Failed to load file \"%s\"\n", pFilename);
}
return;
}
example1.xml 的内容如果是:
输出为:
DOCUMENT
+ DECLARATION
+ ELEMENT Hello
+ TEXT[World]
3.建立文档的方法.
void build( )
{ TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
TiXmlElement * element = new TiXmlElement( "Hello" );
TiXmlText * text = new TiXmlText( "World" );
element->LinkEndChild( text );
doc.LinkEndChild( decl );
doc.LinkEndChild( element );
doc.SaveFile( "example1.xml" );
}
4.设定节点属性。
TiXmlElement window = new TiXmlElement( "Demo" );
window->SetAttribute("name", "Circle");
window->SetAttribute("x", 5);
window->SetAttribute("y", 15);
window->SetDoubleAttribute("radius", 3.14159);
5.获取元素的所有属性,并打印出属性名称和值
int printElement(TiXmlElement* pElement, unsigned int indent)
{
if ( !pElement ) return 0;
TiXmlAttribute* pAttrib=pElement->FirstAttribute();
int i=0;
int ival;
double dval;
const char* pIndent=getIndent(indent);
printf("\n");
while (pAttrib)
{
printf( "%s%s: value=[%s]", pIndent, pAttrib->Name(), pAttrib->Value());
if (pAttrib->QueryIntValue(&ival)==TIXML_SUCCESS) printf( " int=%d", ival);
if (pAttrib->QueryDoubleValue(&dval)==TIXML_SUCCESS) printf( " d=%1.1f", dval);
printf( "\n" );
i++;
pAttrib=pAttrib->Next();
}
return i;
}
6.写入文件,其实上面已经用到了。
doc.SaveFile( saveFilename );
7.建立一个其内容如下的文档:
void main( )
{
TiXmlDocument doc;
TiXmlElement* msg;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); //文档声明
doc.LinkEndChild( decl );
TiXmlElement * root = new TiXmlElement( "MyApp" );
doc.LinkEndChild( root ); //根元素
TiXmlComment * comment = new TiXmlComment();//xml注释
comment->SetValue(" Settings for MyApp " );
root->LinkEndChild( comment ); //插入根元素之间
TiXmlElement * msgs = new TiXmlElement( "Messages" );
root->LinkEndChild( msgs ); //定义元素Messages,插入到root
msg = new TiXmlElement( "Welcome" ); //定义新元素,并插入到msgs
msg->LinkEndChild( new TiXmlText( "Welcome to MyApp" ));
msgs->LinkEndChild( msg );
msg = new TiXmlElement( "Farewell" ); //定义新元素,并插入到msgs
msg->LinkEndChild( new TiXmlText( "Thank you for using MyApp" ));
msgs->LinkEndChild( msg );
TiXmlElement * windows = new TiXmlElement( "Windows" );
root->LinkEndChild( windows ); //root中插入新元素windows
TiXmlElement * window;
window = new TiXmlElement( "Window" );
windows->LinkEndChild( window ); //定义新元素,并设定其属性。
window->SetAttribute("name", "MainFrame");
window->SetAttribute("x", 5);
window->SetAttribute("y", 15);
window->SetAttribute("w", 400);
window->SetAttribute("h", 250);
TiXmlElement * cxn = new TiXmlElement( "Connection" );
root->LinkEndChild( cxn );
cxn->SetAttribute("ip", "192.168.0.1");
cxn->SetDoubleAttribute("timeout", 123.456); // floating point attrib
dump_to_stdout( &doc );
doc.SaveFile( "appsettings.xml" ); //保存文件
}
8.对象到XML的转换。
///class
#include
#include
VC中tinyxml使用方法
VC++ WM_TIMER 定时器使用方法
VC GetSystemMetrics函数的一些使用方法
VC中MessageBox用法
VC中字符串换行
VC中ListCtrl经验总结
VC++中(.def) 文件
VC中ListCtrl经验总结
VC中坐标系的建立 - VC - 转载
awk 中 printf 使用方法
awk?中?printf?使用方法
在VC中调用 WebService
VC中生成汇编文件
在VC中调用 WebService
vc中更改对话框大小
VC中进行Unicode编程
VC中ListCtrl经验总结【转载】
IE 控件一些高级使用方法 - IE编程 - vc++ c++ c 程序开发(vc学习园地
vc中如何调用webservice? VC/MFC / 进程/线程/DLL
VC中坐标系的建立 zz - VC - 转载
VC中坐标系的建立 zz - VC - 转载
TinyXML应用例子ggg
TinyXML应用例子
TinyXML中文文档