最近要和其它厂商的系统互相交换数据。提到要通过XML实现。花了点时间学习了一下,把一些要点记下来,免得浪费今天的劳动成果~有些函数还是没搞明白,但实现目前的需求应该是没问题了。在此感谢令狐虫和猛禽提供滴无私帮助。
1、IXMLDocument包含哪个头文件?
#include<XMLDoc.hpp>
2、如何创建一个XML对象?
//生成一个XMLDocument对象。
//对象生成后不用自己释放。
                                                                        AnsiString  rootpath = "c:\\Temp";
X->Active = true;
//添加一个NodeName叫"dir"
//Value为"1001"                           
ix->Attributes["name"] = "file1.txt";
ix->Attributes["path"] = "c:\\temp";
3、如何创建树状节结?
//在上面代码的基础上添加。
ix->Attributes["id"] =  "id";
ix->Attributes["name"] = name;
ix->Attributes["path"] = Path;
ix->Attributes["type"] = "type";
4、如何读取XML?
// 同样不用管释放
                                                                                                                   
//这个root是啥我也不知道。
                                                                                                //我理解为root是整个XML的根
                                                                                    
for(int i = 0; i < nodelist->Count; i ++) 
{
 AnsiString str;
 WideString wfile = node->GetAttribute(WideString("name"));
 str = wpath + "\\" +  wfile;
 Memo1->Lines->Add(str);
 //只是演试一下如何得到一下层
 for(int j = 0; j <childlist->Count; j++)
 {
  AnsiString str;
  _di_IXMLNode node = childlist->Nodes[j];
  WideString wpath = node->GetAttribute(WideString("path"));
  WideString wfile = node->GetAttribute(WideString("name"));
  str = "   " + wpath + "\\" +  wfile;
  Memo1->Lines->Add(str);
 }
}
5、其它有用的方法
 _di_IXMLNode有一个NodeTpye表明这个Node的类型。但不知道在何时使用它。
 _di_IXMLNode有一个NodeValue,我猜是用来表明这个NodeValue的类型。但不知道在何时使用它。也不知道怎么用,总是报错。
附一个例子:
TForm1 *Form1;
void AddPath(AnsiString Path, _di_IXMLNode node)
{
 AnsiString FilePath=Path+"\\*.*";
 TSearchRec sr;
 sr.Name=FilePath;
 int done;
 done = FindFirst(FilePath,faAnyFile,sr);
 AnsiString FileName;
 while (!done)      {
  FileName=Path+"\\"+sr.Name;
  FileSetAttr(FileName,0);
  if(sr.Attr & faDirectory&&sr.Name[1]!='.') {
   _di_IXMLNode ix = node->AddChild("dir");
  }
  else {
  }
  done = FindNext(sr);
 }
 FindClose(sr);
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
 _di_IXMLDocument X = NewXMLDocument();
 AnsiString  rootpath = "c:\\Temp";
 X->Active = true;
 _di_IXMLNode ix = X->AddChild("dir");
 ix->Attributes["id"] =  "";
 ix->Attributes["name"] = ExtractFileName(rootpath);
 ix->Attributes["path"] = ExtractFilePath(rootpath);
 AddPath(rootpath, ix);
 X->SaveToFile("c:\\temp\\files.xml");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 _di_IXMLDocument xml =LoadXMLDocument("c:\\temp\\files.xml");
 _di_IXMLNode root = xml->GetDocumentElement();
 _di_IXMLNodeList nodelist = root->ChildNodes;
 for(int i = 0; i <nodelist->>Count; i++)
 {
  AnsiString str;
  _di_IXMLNode node = nodelist->Nodes[i];
  WideString wpath = node->GetAttribute(WideString("path"));
  WideString wfile = node->GetAttribute(WideString("name"));
  str = wpath + "\\" +  wfile;
  Memo1->Lines->Add(str);
  _di_IXMLNodeList childlist = node->GetChildNodes();
  for(int j = 0;
  {
   AnsiString str;
   _di_IXMLNode node = childlist->Nodes[j];
   WideString wpath = node->GetAttribute(WideString("path"));
   WideString wfile = node->GetAttribute(WideString("name"));
   str = "   " + wpath + "\\" +  wfile;
   Memo1->Lines->Add(str);
  }
 }
}
//---------------------------------------------------------------------------
1 条评论:
看了好多看不懂的东东,还没有看到你推荐的歌,真是太赔了:(
发表评论