1,考虑到xml自带的节点 xmlNodePtr 太大,没有必要那么多功能,就自己写了一个Node多叉树
里面主要有4个成员,
// 父节点
Node* m_parent;
// 孩子节点
vector<Node*> m_children;
// key值,对应xmlNode里面的name成员
string m_key;
// 属性值 properties->children->content
string m_value;
h文件
#ifndef __MapEdit__Node__
#define __MapEdit__Node__
#include <iostream>
#include <vector>
using namespace std;
class Node
{
public:
Node(string key, string value = "");
~Node();
// 获得父节点
Node* getParent();
// 获得所有孩子节点
vector<Node*>* getChildren();
// 获得该节点的key
string getKey();
// 获得改节点的value
string getValue();
// 判断是否是节点
bool isNode();
// 判断是否是叶子
bool isLeaf();
// 增加孩子节点
void addChild(Node* child);
// 释放内存,包括所有的子孙节点
void releaseAllDatas();
void printf();
private:
void releaseNode(Node* node);
void printf(Node* node);
// 父节点
Node* m_parent;
// 孩子节点
vector<Node*> m_children;
// key值,对应xmlNode里面的name成员
string m_key;
// 属性值 properties->children->content
string m_value;
};
typedef vector<Node*>::iterator NodeItr;
#define NEW_NODE(key, value) new Node(key, value)
#endif /* defined(__MapEdit__Node__) */
// cpp文件
#include "Node.h"
#include "cocos2d.h"
Node::Node(string key, string value/* = ""*/)
: m_key(key)
, m_value(value)
{
m_parent = NULL;
}
Node::~Node()
{
// CCLOG("Node::~Node delete key = %s, value = %s\n", m_key.c_str(), m_value.c_str());
}
// 获得父节点
Node* Node::getParent()
{
return m_parent;
}
// 获得所有孩子节点
vector<Node*>* Node::getChildren()
{
return &m_children;
}
// 获得该节点的key
string Node::getKey()
{
return m_key;
}
// 获得改节点的value
string Node::getValue()
{
return m_value;
}
// 判断是否是节点
bool Node::isNode()
{
if (m_children.size() > 0)
{
return true;
}
return false;
}
// 判断是否是叶子
bool Node::isLeaf()
{
return !isNode();
}
// 增加孩子节点
void Node::addChild(Node* child)
{
child->m_parent = this;
m_children.push_back(child);
}
// 释放内存,包括所有的子孙节点
void Node::releaseAllDatas()
{
releaseNode(this);
}
void Node::releaseNode(Node* node)
{
// CCLOG("Node::releaseNode delete key = %s, value = %s\n", node->m_key.c_str(), node->m_value.c_str());
NodeItr it = node->m_children.begin();
for (; it != node->m_children.end(); ++it)
{
releaseNode(*it);
}
delete node;
node = NULL;
}
void Node::printf()
{
printf(this);
}
void Node::printf(Node* node)
{
CCLOG("key = %s, value = %s", node->getKey().c_str(), node->getValue().c_str());
NodeItr it = node->m_children.begin();
for (; it != node->m_children.end(); ++it)
{
printf(*it);
}
}
// xml的写入
void XMLWrite::dataWrite(Node* node, xmlNodePtr parent)
{
if (node->isNode())
{
xmlNodePtr xmlNode = xmlNewNode(NULL, BAD_CAST node->getKey().c_str());
xmlAddChild(parent, xmlNode);
NodeItr it = node->getChildren()->begin();
for (; it != node->getChildren()->end(); ++it)
{
dataWrite(*it, xmlNode);
}
}
else
{
xmlNewProp(parent, BAD_CAST node->getKey().c_str(), BAD_CAST node->getValue().c_str());
}
}
// 解析xml
void XMLParse::parseData(xmlNodePtr node, Node* data)
{
if (node)
{
string key = (char*)node->name;
Node* child = NEW_NODE(key, "");
data->addChild(child);
// CCLOG("node = %s", key.c_str());
struct _xmlAttr* attr = node->properties;
while (attr)
{
// CCLOG("attr = %s, value = %s", attr->name, attr->children->content);
data->addChild(NEW_NODE((char*)attr->name, (char*)attr->children->content));
attr = attr->next;
}
// CCLOG("--------------------");
node = node->children;
for (; node; node = node->next)
{
parseData(node, child);
}
}
}