用Delphi创建和添加XML的过程,但所得的XML用记事本或UltraEdit打开没有换行和缩进,直接在IE打开则有缩进和换行,虽不影响实际的调用读取,但若要直接用记事本打开修改,则很不方便,哪位知道怎样处理可以实现换行和缩进,或有示例代码,望不吝赐教,谢谢先!
(注:我用的是IXMLDOMDocument 而不是TXMLDocument,所以不要告诉我设置doNodeAutoIndent为True方式)
procedure PrettyFormat( const AXMLDoc: IXMLDOMDocument);
procedure TraverseNode(Node: IXMLDOMNode; Indent: string);
const
IndentLevel = # 9 ;
var
LineBreak: IXMLDOMNode;
ChildNode, NextNode: IXMLDOMNode;
AnyChildNode: Boolean;
begin
if Node = nil then
Exit;
AnyChildNode:= False;
ChildNode:= Node.Get_firstChild;
while ChildNode <> nil do
begin
NextNode:= ChildNode.nextSibling;
if ChildNode.nodeType = NODE_ELEMENT then
begin
AnyChildNode:= True;
// Insert LineBreak before each child Node
LineBreak:= FXMLDoc.createTextNode(sLineBreak +
Indent + IndentLevel);
Node.insertBefore(LineBreak, ChildNode);
TraverseNode(ChildNode, Indent + IndentLevel);
end ;
ChildNode:= NextNode;
end ;
if (Node.nodeType = NODE_ELEMENT) and AnyChildNode then
begin
// Add LineBreak after Node
ChildNode:= NextNode;
LineBreak:= FXMLDoc.createTextNode(sLineBreak + Indent);
Node.appendChild(LineBreak);
end ;
end ;
begin
TraverseNode(AXMLDoc.documentElement, '' );
end ;