TinyXML 2 in Terminal

来源:互联网 发布:最新韩国网络剧2017 编辑:程序博客网 时间:2024/06/06 01:53

  • 测试说明
  • 测试代码
  • 终端编译


测试说明

  • 在源文件目录下放置tinyxml2.cpp和tinyxml.h文件
  • 测试文件TinyXMLTest.cpp

测试代码

#include <iostream>#include <string>#include "tinyxml2.h"using namespace tinyxml2;void makeXML(const char *_fileName){    // use absulote path    std::string filePath = std::string("/Users/phenix/Desktop/") + _fileName;    XMLDocument *pDoc = new XMLDocument();    // xml declaration    XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");    pDoc->LinkEndChild(pDel);    // add 'plist' node    XMLElement *plistElement = pDoc->NewElement("plist");    plistElement->SetAttribute("version", "1.0");    pDoc->LinkEndChild(plistElement); // Add a child node as the last (right) child.    XMLComment *commentElement = pDoc->NewComment("this is xml comment");    plistElement->LinkEndChild(commentElement);    // add 'dic' node    XMLElement *dicElement = pDoc->NewElement("dic");    plistElement->LinkEndChild(dicElement);    // add 'key' node    XMLElement *keyElement = pDoc->NewElement("key");    keyElement->LinkEndChild(pDoc->NewText("Text"));    dicElement->LinkEndChild(keyElement);    XMLElement *arrayElement = pDoc->NewElement("array");    dicElement->LinkEndChild(arrayElement);    for (int i = 0; i<3; i++) {        XMLElement *elm = pDoc->NewElement("name");        elm->LinkEndChild(pDoc->NewText("Cocos2d-x"));        arrayElement->LinkEndChild(elm);    }    pDoc->SaveFile(filePath.c_str());    pDoc->Print();    delete pDoc;    // <?xml version="1.0" encoding="UTF-8"?>    // <plist version="1.0">    // <!--this is xml comment-->    // <dic>    // <key>Text</key>    // <array>    // <name>Cocos2d-x</name>    // <name>Cocos2d-x</name>    // <name>Cocos2d-x</name>    // </array>    // </dic>    // </plist>}void parseXML(const char *_fileName){    // std::string filePath = FileUtils::getInstance()->getWritablePath() + _fileName;    // use absolute path    std::string filePath = std::string("/Users/phenix/Desktop/") + _fileName;    XMLDocument *pDoc = new XMLDocument();    XMLError errorId = pDoc->LoadFile(filePath.c_str());    if (errorId != 0) {        // xml format error        return;    }    XMLElement *rootEle = pDoc->RootElement();    // get the first node's attribute    const XMLAttribute *attribute = rootEle->FirstAttribute();    // print the node's attribute and value    std::cout << "attribute_name = " << attribute->Name() << ",attribute_value = " << attribute->Value() << std::endl;    XMLElement *dicEle = rootEle->FirstChildElement("dic");    XMLElement *keyEle = dicEle->FirstChildElement("key");    if (keyEle) {        std::cout << "keyEle Text = " << keyEle->GetText() << std::endl;    }    XMLElement *arrayEle = keyEle->NextSiblingElement();    XMLElement *childEle = arrayEle->FirstChildElement();    while ( childEle ) {        std::cout << "childEle Text = " << childEle->GetText() << std::endl;        childEle = childEle->NextSiblingElement();    }    delete pDoc;}int main(int argc, char const *argv[]){    makeXML("Example.xml");    parseXML("Example.xml");    return 0;}

终端编译

g++ -std=c++11 TinyXMLTest.cpp tinyxml2.cpp 

CSDN 辣鸡 MD 编辑器,无序列表格式全丢