my_xml.cpp
上传用户:wangli
上传日期:2008-04-19
资源大小:3077k
文件大小:9k
- //---------------------------------------------------------------------------
- #include "my_xml.h"
- myXML::myXML():XmlDoc(NULL),Root(NULL),SavePause(0)
- {
- }
- //-----------------------------------------------------------------------
- myXML::myXML(AnsiString fpath) : XmlDoc(NULL),Root(NULL),SavePause(0)
- {
- init(fpath);
- }
- //-----------------------------------------------------------------------
- void myXML::init(AnsiString fpath)
- {
- _fpath = fpath;
- if( NULL != XmlDoc )delete XmlDoc; //我对这行代码没有信心,还请高人指点。
- XmlDoc = NULL;
- if( fpath.Length() == 0 )
- {
- }
- else
- {
- if(FileExists(fpath)) //如果文件存在
- {
- try{ XmlDoc = LoadXMLDocument(fpath);}
- catch(...){ XmlDoc = NULL;}
- }
- else //如果文件不存在,
- {
- try{ XmlDoc = NewXMLDocument(L"1.0"); } //创建, L""表示宽字符串。
- catch(...){ XmlDoc = NULL;}
- if( NULL == XmlDoc ){}
- else XmlDoc->FileName = fpath;
- }
- }
- if(XmlDoc == NULL){ Root = NULL; }
- else
- {
- XmlDoc->Encoding = AnsiString("UTF-16");
- //编码方式还可以选择:
- //"gb2312" 与一般中文文本兼容
- //"utf-8" 适于全部是英文的情况。
- //"big5" 台湾繁体中文
- Root = XmlDoc->DocumentElement;
- if( Root == NULL )
- {
- XmlDoc->AddChild("root"); //根节点,必须有一个。一般用"root"
- Root = XmlDoc->DocumentElement;
- }
- }
- SavePause = 0;
- }
- //---------------------------------------------------------------------
- bool myXML :: isNull()const{ return (Root == NULL); }
- //---------------------------------------------------------------------
- void myXML :: save()const //存盘
- {
- if( Root == NULL )return;
- if( SavePause > 0 )return;
- if( _fpath.Length() == 0 )return;
- if( XmlDoc->Modified )
- {
- try{ XmlDoc->SaveToFile( _fpath ); }catch(...){}
- }
- }
- //---------------------------------------------------------------------
- xmlNode myXML :: root()const{ return xmlNode(Root);}
- //---------------------------------------------------------------------
- void myXML :: disable_save(){ ++SavePause; }
- //---------------------------------------------------------------------
- void myXML :: enable_save(){ --SavePause; save(); }
- //---------------------------------------------------------------------
- void myXML::save_to( AnsiString fpath_ )const //保存到...
- {
- if( Root == NULL )return;
- try{ XmlDoc->SaveToFile( fpath_ ); }catch(...){}
- }
- //---------------------------------------------------------------------
- //===========================================================================
- //---------------------------------------------------------------------
- //本函数用于替换掉字符串中含有的非法字符,防止其写入xml文件中。
- //这里简单的用空格替换,不一定合适。有待大家指正。
- static AnsiString & Valid(AnsiString & str)
- {
- const int Len = str.Length();
- for(int i=1;i<=Len;++i)
- {
- char & c = str[i]; if(c > 0x00 && c < 0x20)c = ' ';
- }
- return str;
- }
- //-----------------------------------------------------------------------
- xmlNode :: xmlNode(){ _node = NULL; }
- //-----------------------------------------------------------------------
- xmlNode :: xmlNode(_di_IXMLNode node){ _node = node;}
- //-----------------------------------------------------------------------
- void xmlNode :: init(_di_IXMLNode node){ _node = node;}
- //-----------------------------------------------------------------------
- bool xmlNode :: isNull()const{ return (_node == NULL); }
- //-----------------------------------------------------------------------
- xmlNode & xmlNode::operator = ( const xmlNode & node_ )
- {
- _node = node_._node; return *this;
- }
- //-----------------------------------------------------------------------
- //返回一个名字是name的子节点,如果没有,创建一个。
- xmlNode xmlNode :: get_child_node(AnsiString name)const
- {
- if(_node == NULL)return xmlNode(NULL);
- _di_IXMLNode node = _node->ChildNodes->FindNode(name);
- if(node == NULL)
- {
- return xmlNode(_node->AddChild(name));
- }
- else return xmlNode(node);
- }
- //-----------------------------------------------------------------------
- //返回名字是name的子节点。
- xmlNode xmlNode :: find_child_node(AnsiString name)const
- {
- if(_node == NULL)return xmlNode(NULL);
- return xmlNode(_node->ChildNodes->FindNode(name));
- }
- //-----------------------------------------------------------------------
- AnsiString xmlNode :: name()const
- {
- if(_node == NULL)return AnsiString();
- return _node->NodeName;
- }
- //-----------------------------------------------------------------------
- AnsiString xmlNode :: value()const //获得该节点的字符串值。
- {
- if(_node == NULL)return AnsiString();
- AnsiString S;
- try
- {
- OleVariant v = _node->Text;
- if(v.IsNull())S = AnsiString();
- else S = AnsiString(v);
- }
- catch(...){ S = AnsiString();}
- return S;
- }
- //-----------------------------------------------------------------------
- AnsiString xmlNode :: child_value(AnsiString name)const //返回子节点的值。
- {
- if(_node == NULL)return AnsiString();
- AnsiString S;
- try
- {
- OleVariant v = _node->ChildValues[name];
- if(v.IsNull())S = AnsiString();
- else S = AnsiString(v);
- }
- catch(...){ S = AnsiString();}
- return S;
- }
- //-----------------------------------------------------------------------
- void xmlNode::set_value( AnsiString value_ )const
- {
- if(_node == NULL)return; Valid( value_ );
- try{ _node->Text = value_; }catch(...){}
- }
- //-----------------------------------------------------------------------
- void xmlNode :: set_child_value(AnsiString name,AnsiString value)const
- {
- if(_node == NULL)return ;
- Valid(value);
- const _di_IXMLNode node = _node->ChildNodes->FindNode(name);
- if(node == NULL)
- {
- if(value.Length() == 0);
- else
- {
- try{ _node->AddChild(name)->Text = value;}catch(...){}
- }
- }
- else
- {
- if(value.Length() == 0)
- {
- try{ _node->ChildNodes->Remove(node);}catch(...){}
- }
- else
- {
- try{ _node->ChildValues[name] = value;}catch(...){}
- }
- }
- }
- //-----------------------------------------------------------------------
- AnsiString xmlNode :: attribute(AnsiString name)const
- {
- if(_node == NULL)return AnsiString();
- AnsiString S;
- try{
- OleVariant v = _node->Attributes[name];
- if(v.IsNull())S = AnsiString();
- else S = AnsiString(v);
- }
- catch(...){ S = AnsiString();}
- return S;
- }
- //-----------------------------------------------------------------------
- void xmlNode :: set_attribute(AnsiString name,AnsiString value)const
- {
- if(_node == NULL)return ;
- Valid(value);
- if(value.Length() == 0)
- {
- try{ _node->Attributes[name] = Null();}catch(...){}
- }
- else
- {
- try{ _node->Attributes[name] = value;}catch(...){}
- }
- }
- //-----------------------------------------------------------------------
- void xmlNode :: find_child_nodes(vector<xmlNode> & nodes)const
- {
- if(_node == NULL)return ;
- _di_IXMLNodeList List = _node->ChildNodes;
- for(int i=0;i<List->Count;i++)
- {
- if( List->Nodes[i]->NodeType == ntElement )
- {
- nodes.push_back(List->Nodes[i]);
- }
- }
- }
- //-----------------------------------------------------------------------
- //一次获得本节点所有的属性。
- void xmlNode :: get_attrs(map<AnsiString,AnsiString> & attrs)const
- {
- if(_node == NULL)return ;
- _di_IXMLNodeList List = _node->AttributeNodes ;
- for(int i=0;i<List->Count;i++)
- {
- AnsiString attr_name = List->Nodes[i]->NodeName;
- AnsiString attr_value;
- try{ attr_value = List->Nodes[i]->NodeValue; }
- catch(...){ attr_value = AnsiString(); }
- attrs.insert(make_pair(attr_name,attr_value));
- }
- }
- //-----------------------------------------------------------------------
- //先将旧的属性全部删除,新属性作为一个整体增加。
- void xmlNode :: set_attrs(const map<AnsiString,AnsiString> & attrs)const
- {
- if(_node == NULL)return ;
- _node->AttributeNodes->Clear();
- map<AnsiString,AnsiString>::const_iterator p;
- for( p = attrs.begin(); p != attrs.end(); ++p )
- {
- set_attribute(p->first,p->second);
- }
- }
- //-----------------------------------------------------------------------
- void xmlNode :: add_child_node(AnsiString child_name)const
- {
- if(_node == NULL)return ;
- _node->AddChild(child_name);
- }
- //-----------------------------------------------------------------------
- void xmlNode :: del_child_nodes()const
- {
- if(_node == NULL)return ;
- if(_node->HasChildNodes)
- {
- _node->ChildNodes->Clear();
- }
- }
- //-----------------------------------------------------------------------
- void xmlNode :: del_child_node(AnsiString child_name)const
- {
- if(_node == NULL)return ;
- if(_node->HasChildNodes)
- {
- _node->ChildNodes->Delete( child_name );
- }
- }
- //====
-