구성 데이터를 XML 파일에 쓰려고합니다. 그러나 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>
말할 수 여기에 어떤 식 으로든 이 내 델파이 코드의 조각이라는 것이다 수 있습니다. 모든 유형의 함수/프로 시저가 있지만 여기에 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;
xml의 주된 목적은 사람이 읽을 수 없도록하는 것입니다. 그것은 기계 - 기계 통신을위한 것입니다. 귀하의 출력물은 "올바른"형식화 된 XML 출력이어야합니다. 왜 다른 모습이 필요한거야? XmlWith 이외의 다른 것을 "보기"원한다면, 자신의 XmlWriter를 작성해야만합니다. 그렇지 않으면 다른 형식으로 변경해야합니다. – Alex
대신 [TIniFile] (http://delphi.about.com/od/objectpascalide/a/tinifile.htm)을 사용해보십시오. '.ini' 파일 구조에서 설정을 편집하는 사람이 잘못된 것을 얻는 반면 XML은 * syntax-heavy * 일 가능성이 훨씬 적습니다. –
대신 YAML을 사용하여 더 멋지게 보이게하십시오 –