请帮忙:C++代码实现,把一个XML文件放入vector数组中,该如何处理
请帮忙:C++代码实现,把一个XML文件放入vector<CNode>数组中
请帮忙:把一个XML文件内容放入vector<CNode>数组中,C++代码如何实现?
CNode>是自己定义的类
class CNode
{
string m_strNodeName;//节点名称
vector<string> m_vectorAttributeName; //属性名称数组
vector<string> m_vectorAttributeValue;//属性值数组 以上2个数组顺序对应
string m_strText; //节点的值
vector<CNode> m_subvector; 该节点下的所有子节点组成的数组
}
我想把每个节点用CNode存储,如果有子节点,则存储在 m_subvector中,这样循环,感觉有点像树的存储结构。
但是我数据结构学的不好,不知道怎么用。请大家多多帮忙。我想过用递归,但是也不会写具体实现。感觉怎么做都有问题。
------解决方案--------------------
很久以前一个小工具中写的,可能是有bug的.楼主小心。
请帮忙:把一个XML文件内容放入vector<CNode>数组中,C++代码如何实现?
CNode>是自己定义的类
class CNode
{
string m_strNodeName;//节点名称
vector<string> m_vectorAttributeName; //属性名称数组
vector<string> m_vectorAttributeValue;//属性值数组 以上2个数组顺序对应
string m_strText; //节点的值
vector<CNode> m_subvector; 该节点下的所有子节点组成的数组
}
我想把每个节点用CNode存储,如果有子节点,则存储在 m_subvector中,这样循环,感觉有点像树的存储结构。
但是我数据结构学的不好,不知道怎么用。请大家多多帮忙。我想过用递归,但是也不会写具体实现。感觉怎么做都有问题。
------解决方案--------------------
很久以前一个小工具中写的,可能是有bug的.楼主小心。
- C/C++ code
bool CConfig::Parse( TiXmlElement* pobjElement,CConfigEntry* pobjEntry ) { bool bRet = true; pobjEntry->Name() = pobjElement->ValueStr(); if(pobjElement->GetText()) { pobjEntry->Text() = pobjElement->GetText(); } TiXmlElement* pobjCurrElement = pobjElement->FirstChildElement(); if (!pobjCurrElement) { return true; } CConfigEntry* pobjChildEntry = 0; do { pobjChildEntry = new CConfigEntry; try { if(!Parse(pobjCurrElement,pobjChildEntry)) { bRet = false; break; } pobjEntry->AddChild(pobjChildEntry); pobjCurrElement = pobjCurrElement->NextSiblingElement(); } catch(...) { bRet = false break; } } while (pobjCurrElement); if (pobjChildEntry && (!bRet)) { delete pobjChildEntry; } return bRet; }
------解决方案--------------------
- C/C++ code
void load_xml(LPCTSTR path) { xml_obj o(path); node n; o.get_first_child(&n); CNode* root = new Node(n);//构造函数由你的xml库定义的node,取得CNode里的各种属性 load_impl(o,root);//需要保存这个root } void load_impl(xmlobj& o,node* root) { node n; if(!o.get_first_child(root,&n)) return; do { root.m_subvector.push_back(new Node(n)); if(n.has_child()) load_impl(o,&n); }while(n = o.get_next_brother(n,&n));//node如果是com接口则不能这么用 }