C# 请参阅:C# 用 System.Xml 读 Freeplane.mm文件,生成测试用例.csv文件
Freeplane 是一款基于 Java 的开源软件,继承 Freemind 的思维导图工具软件,它扩展了知识管理功能,在 Freemind 上增加了一些额外的功能,比如数学公式、节点属性面板等。
打开 lazarus-IDE,创建 project: xml2csv , 编辑GUI界面,编写 unit1.pas 如下
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
StdCtrls, Crt, laz2_DOM, laz2_XMLRead; // ShellApi,
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
fn1: String;
fn2: String;
xmlDoc: TXMLDocument;
sList: TStringList;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
// Local function that outputs a node attributes as a string
function GetNodeAttribute(pNode: TDOMNode; attr: string):string;
var i: integer;
begin
Result:='';
if pNode.HasAttributes then
for i := 0 to pNode.Attributes.Length -1 do
with pNode.Attributes[i] do
if NodeName = attr then
Result := format('%s', [NodeValue]);
// Remove leading and trailing spaces
Result:=Trim(Result);
end;
// Recursive function to process a node and all its child nodes
procedure ParseXML(Node:TDOMNode);
var str: string;
begin
// Exit procedure if no more nodes to process
if Node = nil then Exit;
// Add a node 'TEXT' attributes to StringList
str := GetNodeAttribute(Node, 'TEXT');
if str <>'' then sList.Add(str);
// Process all child nodes
Node := Node.FirstChild;
while Node <> Nil do
begin
ParseXML(Node);
Node := Node.NextSibling;
end;
end;
begin
if OpenDialog1.Execute then
begin
fn1 := OpenDialog1.FileName;
Edit1.Clear;
Edit1.Text := fn1;
end;
if FileExists(fn1) then
begin
//Memo1.Lines.LoadFromFile(fn1);
if ExtractFileExt(fn1) ='.mm' then
begin
try
sList := TStringList.Create;
ReadXMLFile(xmlDoc, fn1);
Memo1.Lines.Clear;
ParseXML(xmlDoc.DocumentElement);
Memo1.Lines.Add(sList.Text);
finally
xmlDoc.Free;
sList.Free;
end;
end
else
Memo1.Lines.LoadFromFile(fn1);
end
else
ShowMessage(AnsiToUTF8(fn1+' 文件没找到'));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
fn1 := Edit1.Text;
if FileExists(fn1) then
begin
// Simple one-liner (ignoring error returns) :
//if ShellExecute(0,nil, PChar('mm_Xml_csv.exe'),PChar(fn1),nil,1) =0 then;
SysUtils.ExecuteProcess('mm_Xml_csv.exe', Utf8ToAnsi(fn1), []);
delay(100);
end
else
ShowMessage(AnsiToUTF8(fn1+' 文件没找到'));
fn2 := fn1 +'.csv';
if FileExists(fn2) then
begin
Memo1.Lines.LoadFromFile(fn2);
end;
end;
end.
copy mm_Xml_csv.exe to projects\xml2csv\ 点击运行。
注意有中文文件名,需用 SysUtils.ExecuteProcess('mm_Xml_csv.exe', Utf8ToAnsi(fn1), []);
参阅:Executing External Programs