TinyXML解析器 入门

//初始化:建立XML结构文件 ,创建根节点

TiXmlDocument *myDocument = new TiXmlDocument("ePhoto.xml");

       TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "" );

      myDocument->LinkEndChild(decl);

         TiXmlElement *ephoto=new TiXmlElement("ephoto");

      myDocument->LinkEndChild(ephoto);

       myDocument->SaveFile("ePhoto.xml");//保存

//打开已经建立的XML结构文件,对其进行操作。

TiXmlDocument *myDocument = new TiXmlDocument("ePhoto.xml");

myDocument->LoadFile();//载入文件

//增加节点

TiXmlElement *type1 = new TiXmlElement(type);//“type1”是字符串变量

TiXmlElement *type2 = new TiXmlElement("photo");

 type2->SetAttribute("ID","1");

myDocument->FirstChildElement()->LinkEndChild(type1); type1->LinkEndChild(type2);

//删除节点

TiXmlNode *node=first1;

myDocument->FirstChildElement()->RemoveChild(node);

//更新节点

TiXmlText te1(s);

xmlp->ReplaceChild(xmlp->FirstChild(),te1);

xmlp=xmlp->NextSiblingElement();

///////////////////////////////////////////////////////////////////////

xml结构文件ePhoto.xml如下:

<?xml version="1.0" encoding="UTF-8" ?>

<ephoto>

    <Andy>

        <photo ID="1">

            <name>NULL</name>

            <directory>NULL</directory>

            <describe>NULL</describe>

        </photo>

    </Andy>

    <Tom>

        <photo ID="1">

            <name>NULL</name>

            <directory>NULL</directory>

            <describe>NULL</describe>

        </photo>

    </Tom>

    <Bill>

        <photo ID="1">

            <name>NULL</name>

            <directory>NULL</directory>

            <describe>NULL</describe>

        </photo>

    </Bill>

</ephoto>

posted @ 2013-01-05 10:10  星语海蓝  Views(275)  Comments(0)    收藏  举报