www.pudn.com > xml_ptr.zip > XmlHandler.cpp
#include "XmlHandler.h" #include#include #include //#include using namespace std; Node::Node() { } Node::Node(const string& name) { _name = name; _key = _name; } Node::Node(const string& name, const string& val) { _name = name; _key = _name; _val = val; } Node::Node(const string& name, const string& val, const string& key) { _name = name; _val = val; _key = key; } Node::~Node() { //cout << "destroy:" << _name << endl; for_each(_nodeMap.begin(), _nodeMap.end(), DeleteObject()); _nodeMap.clear(); } void Node::setKey(const string& key) { _key = key; } void Node::setVal(const string& val) { _val = val; } void Node::setNode(const string& key, Node* val) { _nodeMap[key] = val; } XmlTree::XmlTree() { } XmlTree::~XmlTree() { if (root != NULL) { delete root; } } bool XmlTree::insertNode(Node* pNode, Node* cNode) { if (pNode == NULL) { root = cNode; return true; } pNode->_nodeMap[cNode->_key] = cNode; return true; } Node* XmlTree::findNode(Node* pNode, const string& key) { if(pNode == NULL) { pNode = root; } if(pNode->_key == key) { return pNode; } deque queue; queue.push_back(pNode); while(!queue.empty()) { //cout << queue.size() << endl; pNode = queue.front(); queue.pop_front(); NodeMap::iterator it = pNode->_nodeMap.find(key); if(it == pNode->_nodeMap.end()) { for(NodeMap::iterator it= (pNode->_nodeMap).begin(); it != (pNode->_nodeMap).end(); it++) { queue.push_back(it->second); } } else { return it->second; } } return NULL; } /* Node* XmlTree::findNode(Node* pNode, const string& key) { if(pNode == NULL) { pNode = root; } if (pNode->_key == key) { return pNode; } if(pNode->_nodeMap.empty()) { return NULL; } for(NodeMap::iterator it= (pNode->_nodeMap).begin(); it != (pNode->_nodeMap).end(); it++) { Node *p; if((p = findNode(it->second, key)) != NULL) { return p; } } return NULL; } */ bool XmlTree::readXmlFile(const string& fileName, string& content) { ifstream in; in.open(fileName.c_str()); if (!in) { cout << "Input file cannot be opened.\n"; return false; } string str; while(!in.eof()) { in >> str; content += str; } in.close(); return true; } XmlTree* XmlTree::createXmlTree(string& content) { //std::cout << content << std::endl; /* boost::regex re(" "); boost::sregex_token_iterator i(content.begin(), content.end(), re, -1); boost::sregex_token_iterator j; while(i != j) { std::cout << *i++ << std::endl; } */ } std::string XmlTree::printInfo(Node* pNode) { std::string info; if (pNode == NULL) { return info; } std::string head = "<" + pNode->_name + ">"; std::string tail = "" + pNode->_name + ">"; if (pNode->_nodeMap.empty()) { std::string body = pNode->_val; return (head + body + tail); } else { std::string body; for(NodeMap::iterator it = pNode->_nodeMap.begin(); it != pNode->_nodeMap.end(); ++it) { body += printInfo(it->second); } return (head + body + tail); } } void XmlTree::printInfo(ostream& out) { out << printInfo(root); } ostream& operator <<(ostream& out, XmlTree& tree) { tree.printInfo(out); return out; }