Recently, on CSDN, I've often seen people ask how to use XML in C + + Builer, and the beginning authors think it's baffling, first of all, XML technology seems to be associated with the network, or Soap/web Service, followed by C + + Builer provides txmldocument components that should be easy to use. These days the author studied Tomcat, found that its configuration file is written in XML file, it seems that this represents the advanced software: So I also decided to use C + + Builer to operate XML files to increase the professional sense of their software. At this point I deeply feel the confusion of those questioned, txmldocument components are really very difficult to use, if there has been asp+xml construction site experience, really have to be stumped by this question: "Less nonsense, turn to the topic."
Processing XML files requires XML Parser, and XML parse includes sax,dom two types, sax is relatively simple, Dom models are difficult to use, and C + + Builer uses the DOM model to process XML files. So here's how to deal with DOM models.
The Txmldocument component uses the MS XML DOM model to process XML, Msxmldom is based on COM, it is easy to use this COM in tools such as VB, and in C + + Builer It is not easy, I think it is because of our preconceived thinking , VCL's class design is very easy to understand and use, many methods see name, even help do not need to check, and the author in the use of txmldocument have to check help, many methods can not accurately know its meaning, and even traps.
Here's a simple example of how you can use Txmldocument to process XML, and the author needs a small program with a timed reminder schedule, assuming that all the other features are designed, and the question now is how to save our schedule, here to use the XML , so of course the XML file format is used to store it. For a scheduler, you definitely need to have the ability to add deletes, which basically already contains all the processing XML.
This is how we design the XML file (tip.xml):
<?xml version="1.0" encoding="GB2312"?> //注意这里的编码格式为GB2312,否则有乱码
<VTip>
<Tip>
<Mode>0</Mode>
<Time>2003/4/25?08:01:46</Time>
<Content>洗衣服</Content>
</Tip>
<Tip>
<Mode>1</Mode>
<Time>2003/4/25?PM 17:30:00</Time>
<Content>陪女朋友吃饭</Content>
</Tip>
</VTip>
The XML file above is a more disciplined format and does not include things like attributes and doctype, but with the following knowledge, you should be able to handle complex formats easily.
The first is to open the XML, and the present code fragment shows the process:
Xmldoc->loadfromfile (Widestring (pathstring (AppPath) + "Tip.xml"));
XmlDoc is Txmldocument's name.
xmldoc->active=true;
Filllist (); Add XML content to ListView
And then there's how to read the XML content (filllist):
lstMain->Items->BeginUpdate();
lstMain->Items->Clear();
_di_IXMLNode node=XMLDoc->DocumentElement;
//DocumentElement属性用来得到根节点
_di_IXMLNodeList nodes=node->ChildNodes;
//得到子节点List
for(int n=0;n<nodes->Count;n++)
{
TListItem* li=lstMain->Items->Add();
node=nodes->Nodes[n]->ChildNodes->Nodes[0];
//得到子节点的子节点,也也就是 VTip//Tip//Mode
int m=String(node->GetText()).ToInt();
//得到对应节点的Text内容并转换到Int
li->Caption="";
li->ImageIndex=m+5;
li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[1]->GetText());
li->SubItems->Add(nodes->Nodes[n]->ChildNodes->Nodes[2]->GetText());
//代码同上
}
lstMain->Items->EndUpdate();
After the code above, we see a result similar to the following figure:
To add a node element:
bool __fastcall Tfrmmain::AddTask(int m,
TDateTime& date,
TDateTime& time,
String con)
{
//TODO: Add your source code here
_di_IXMLNode node=XMLDoc->DocumentElement;
node=node->AddChild(WideString("Tip"));
_di_IXMLNode subnode=node->AddChild(WideString("Mode"));
subnode->SetText(WideString(m));
subnode=node->AddChild(WideString("Time"));
subnode->SetText(WideString(date.DateString()+"?"+time.TimeString()));
subnode=node->AddChild(WideString("Content"));
subnode->SetText(WideString(con));
return true;
} //以上代码很容易理解,读的时候可以对照XML文件的格式
Finally, delete:
int ind=lstMain->Selected->Index;
//ind为要删除的节点号
_di_IXMLNode node=XMLDoc->DocumentElement;
int re=node->ChildNodes->Delete(ind);
if (re!=-1)
lstMain->Items->Delete(ind);
The above code shows how to work with an XML file, and readers can look at it for themselves and know how to handle more complex XML file formats.