xml操作C++表述
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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 的内容如果是:
输出为:
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