Use XMLDocment traversal 9cbs forum post Reply
On the day of the water garden, I saw someone eliminated in the water garden. It is to put a number of people participating in someone in the forum in the forum. Suddenly I feel the heart, why not, when necessary, I can please I want to intend to interested MM. , Huh, you can know more about someone else, why not :)
Carefully study the file format of the forum is XML, is Delphi6 not bring an XMLDocument control? It can be readily read on the XML file, studied several related articles, starting to do it, spending more than two hours, the only flaw is a little slow, traversing 2000 posts, probably 5 To a minute, then I have time to help the brothers, for example, what to make a multi-thread, I really are too lazy to improve, huh, huh, nonsense, existing code, mainly completed the response to a specific author Search, other by setting keywords, forum numbers, time sections can be improved.
Main code part:
There are four text boxes on the form, a MEMO control and two twists
By specifying the start number, end number and author name, click "Search" button, you can display the results into the MEMO box.
Unit XMLDocSearch;
Interface
Uses
Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, stdctrls, xmldom, xmlintf, msxmldom, xmldoc, comCtrl
Const
URL = 'http: //expert.9cbs.net/expert/topic/';
Type
TFORMTEST = Class (TFORM)
Memooutput: TMEMO;
Button1: tbutton;
Label1: TLABEL;
Label2: TLABEL;
Label3: TLABEL;
Label4: TLABEL;
EDIT1: TEDIT;
EDIT2: TEDIT;
EDIT3: TEDIT;
EDIT4: TEDIT;
Button2: tbutton;
Xmldoc: txmldocument;
Procedure Button1Click (Sender: TOBJECT);
Procedure Button2Click (Sender: TOBJECT);
Private
{Private Declarations}
STARTID, EndId: longint;
Keyword, Author: string;
public
{Public declarations}
END;
VAR
FormTest: TFORMTEST;
IMPLEMENTATION
Usess searchthread;
{$ R * .dfm}
Procedure TFormTest.Button1Click (Sender: TOBJECT);
Display format:
{# # # Search: 489000--493000 # # #
Post ID: 489353
Reply to: Silly Cat
Reply time: 2002-1-21 10:48:45
content:
Wait a minute. I sent a short message to ask her.
# # # # # # # # # # #
}
VAR
Root, curnode, childnode: ixmlnode; // root node, current node, child node
SSTR, SSTRRETURN: STRING; TopiciD, ReplyName, ReplynickName, ReplyTime, ReplyContent: String;
// Theme number, replying to the name, reply people, call, reply time, reply content
Cururl: pchar;
DTR, DTS, DTE: TDATETIME;
MIN: DOUBLE;
Curid: integer; // Current number
Begin
DTS: = now; // Start time
StartID: = start (edit3.text);
EndId: = strt (edit4.text);
Keyword: = trim (edit1.text);
Author: = trim (edit2.text);
Memooutput.clear;
SSTRRRRURN: = CHR (13) CHR (10);
SSTR: = '# # Search:' INTOSTR (STARTID) '-' INTOSTR (Endid) '# #' sstrreturn sstrreturn;
Memooutput.setselTextBuf (Pchar (SSTR));
For curid: = startid to endid do
Begin
TopiciD: = INTOSTR (CURID);
Cururl: = PCHAR (URL COPY (TopiciD, 1, Length (Topicid) -3) '/' Topicid '. XML');
XMLDoc.LoadFromFile (cururl);
XMLDoc.Active: = TRUE;
Root: = xmldoc.documentelement; // Take the root node of the XML file, ie Topic
Curnode: = root.childNodes ['reply']; // Remove the current node to the second child node replys
CURNODE: = CURNODE.CHILDNODES.First; // Make the current node to the first child node information of the Replys node
While (Curnode <> nil) DO // Loop reads multiple reply information
Begin
if Curnode.childNodes ['PostuserName']. Text = Author Then
Begin
ChildNode: = CURNODE.CHILDNODES.FIRST; // Make ChildNode points to the first child node information of this reply
While (ChildNode <> nil) do // Recycled each child node of reply
Begin
IF (childnode.nodeename = 'postusername') THEN
Replyname: = childNode.text
Else if (childnode.nodeename = 'postusernickname') THEN
Replynickname: = childNode.text
Else if (childnode.nodeename = 'postdatetime') THEN
Replytime: = childNode.text
Else IF (childnode.nodeename = 'content') THEN
ReplyContent: = trim (childnode.text); childnode: = childNode.nextSibling; // Sequentially remove a reply child node information
END;
SSTR: = 'Post Id:' Topicid SSTRRRRRRRRRN;
SSTR: = SSTR 'Replies:' replyName '(' replynickname ')' sstrreturn;
SSTR: = SSTR 'Reply Time:' ReplyTime SSTRRRRRRRRRRRRRRRRRRRRRRN;
SSTR: = SSTR 'content:' sstrreturn sstrreturn replycontent sstrreturn sstrreturn;
SSTR: = SSTR '# # # # # # # # # # # # # # # #';
SSTR: = SSTR SSTRRRRETURN;
Memooutput.setselTextBuf (Pchar (SSTR));
END;
Curnode: = CURNODE.NEXTSIBLING; // Sequential Remove a reply information
END;
// xmldoc.savetofile ('./ Down /' Topicid '. XML'); // Save File
END;
DTE: = now; // End time
DTR: = DTE-DTS;
min: = DTR * 24 * 60 * 60;
SHOWMESSAGE ('uses a total of' floattostr (min) 'seconds!');
END;
Procedure TFormTest.Button2Click (Sender: TOBJECT);
Begin
CLOSE;
END;
End.
The main code part is written above, what is the problem with the friend or a better suggestion, please contact me, my email: hnxy @hotmail.com.
Completion Date: 2003/5/28