1

我正在将配置数据写入 XML 文件。但是XML看起来很不结构化或者怎么说

<Configuration>
  <Ftp Host="LOCALHOST" Port="21"/>
  <Pop3 Host="LOCALHOST" Port="110" Interval="30000"/>
  <Smtp Host="LOCALHOST" Port="25"/>
</Configuration>

我希望它看起来像

<Configuration>
  <Ftp 
        Host="LOCALHOST" 
        Port="21"
    />
  <Pop3 
        Host="LOCALHOST" 
        Port="110" 
        Interval="30000"
    />
  <Smtp 
        Host="LOCALHOST" 
        Port="25"
    />
</Configuration>

有没有可能这是我的 Delphi 代码片段。我有所有类型的函数/程序,但这里只显示 2

constructor TConnXml.Create(const FileName: string);
begin
  inherited Create;
  fConfigfile     := FileName;
  fXMLDoc         := TXMLDocument.Create(Application);
  fXMLDoc.Options := [doNodeAutoIndent];
  if FileExists(fConfigfile) then
    fXMLDoc.LoadFromFile(fConfigfile)
  else
    begin
      fXMLDoc.Active := True;
      fXMLDoc.AddChild('Configuration');
      fXMLDoc.SaveToFile(fConfigfile);
    end;
end;

constructor TConnXml.Create;
begin
  Create(SettingsFileBuild);
end;

function TConnXml.ReadString(const Section, Key, Default: string): string;
var
  Node: IXMLNode;
begin
  Node := fXMLDoc.DocumentElement.ChildNodes.FindNode(Section);
  if Assigned(Node) and Node.HasAttribute(Key) then
    Result := Node.Attributes[Key]
  else
    Result := Default;
end;

procedure TConnXml.WriteString(const Section, Key, Value: string);
var
  Node: IXMLNode;
begin
  if ReadString(Section, Key, '') = Value then
    Exit;
  Node := fXMLDoc.DocumentElement.ChildNodes.FindNode(Section);
  if not Assigned(Node) then
    Node := fXMLDoc.DocumentElement.AddChild(Section);
  Node.Attributes[Key] := Value;
  fModified := True;
  Save;
end;

procedure TConnXml.Save;
begin
  if not fModified then
    Exit;
  if fBackup then
    CopyFile(PChar(fConfigfile), PChar(fConfigfile + '.bak'), False);
  fXMLDoc.Active := True;
  fXMLDoc.SaveToFile(fConfigfile);
  fModified := False;
end;

function TConnXml.ReadBoolean(const Section, Key: string; Default: Boolean): Boolean;
begin
  Result := Boolean(ReadInteger(Section, Key, Integer(Default)));
end;

procedure TConnXml.WriteBoolean(const Section, Key: string; Value: Boolean);
begin
  WriteInteger(Section, Key, Integer(Value));
end;
4

1 回答 1

2

如果您生成此 XML 并且它是出于配置目的,那么使其更具可读性是有其目的的。我经常使用 XML 进行配置,并且仅在真正适用时才使用属性。

我会这样写:

<Configuration>
  <Ftp> 
    <Host>LOCALHOST</Host> 
    <Port>25</Port>
  </Ftp>
  <Pop3> 
    <Host>LOCALHOST/<Host> 
    <Port>110</Port>
    <Interval>30000</Interval>
  </Pop>
  <Smtp> 
    <Host>LOCALHOST</Host> 
    <Port>25</Port>
  </Smtp>
</Configuration>

使用其他格式,然后是 XML,也是一种解决方案。但是,如果您坚持使用 XML,那么我的回答是以人类可读的方式组织 XML 的一种方法。此外,如果您避免使用属性,则例如转换为 JSON 非常简单。

即使 XML 被标记臃肿,我发现如果结构良好,它也是可读的。虽然它是用于计算机数据交换的,但我发现它非常适合配置文件。YAML 看起来不错,但对我来说,它缺乏明确的结构 :)

更新:

由于对代码的请求,我用附加信息更新了答案。要获得像我下面这样的 XML,您所要做的就是更改一个程序。另一方面,这是基本的 XML 处理,所以我建议您学习它。

function TConnXml.ReadString(const Section, Key, Default: string): string;
var
  Node: IXMLNode;
  Child: IXMLNode;
begin
  Node := fXMLDoc.DocumentElement.ChildNodes.FindNode(Section);

  if not Assigned(Node) then 
  begin
    Result := Default;
    Exit;
  end;

  Child:= Node.FindNode(Key);

  if not Assigned(Child) then 
  begin
    Result := Default;
    Exit;
  end;

  Result := Child.Text;
end;

procedure TConnXml.WriteString(const Section, Key, Value: string);
var
  Node: IXMLNode;
  Child: IXMLNode;
begin
  if ReadString(Section, Key, '') = Value then
    Exit;

  Node := fXMLDoc.DocumentElement.ChildNodes.FindNode(Section);

  if not Assigned(Node) then
    Node := fXMLDoc.DocumentElement.AddChild(Section);

  Child:= Node.ChildNodes.FindNode(Key);

  if not Assigned(Child) then
    Child:= Node.AddChild(Key);

  Child.Text := Value;
  fModified := True;
  Save;
end;

我在没有测试的情况下编写了它,因此其中可能存在一些错误,但这是您应该使用的代码。

于 2013-08-24T06:24:07.860 回答