我使用XMLDocument(TXMLDocument组件),用创建了一个xml文件,用IE等带有解析功能的工具查看该文件换行与缩进都有,可是用记事本等工具打开就没有任何的换行与缩进,显得很乱,哪位高手能帮着解决下,不胜感激!
小弟分不多,还望见谅!

解决方案 »

  1.   

    按一楼说的 用editplus打开,然后粘贴到记事本中呗
      

  2.   

    XML格式化使用msxml引擎,Delphi代码如下:unit uXMLFormat;interfaceuses
      SysUtils, ActiveX, msxml;
            
    function PrettyFormat(const AXML: String): String;implementationconst             
      SMSDOMNotInstalled = 'Microsoft MSXML 4.0 or upper is not installed.';
      // msxml parser 4.0
      ProgID_FreeThreadedDOMDocument40 = 'Msxml2.FreeThreadedDOMDocument.4.0';
      // msxml parser 6.0
      ProgID_FreeThreadedDOMDocument60 = 'Msxml2.FreeThreadedDOMDocument.6.0';type
      EMSXMLDomException = class(Exception);function TryObjectCreateFromProgID(const ProgIDList: array of PWideChar): IUnknown; overload;
    var
      I: Integer;
      FClsID: TGUID;
      Status: HResult;
    begin
      for I := Low(ProgIDList) to High(ProgIDList) do
        if Succeeded(CLSIDFromProgID(ProgIDList[I], FClsID)) then
        begin
          Status := CoCreateInstance(FClsID, nil, CLSCTX_INPROC_SERVER or
            CLSCTX_LOCAL_SERVER, IUnknown, Result);
          if Status = S_OK then Exit;
        end;
    end;function CreateDOMDocument: IXMLDOMDocument;
    begin
      Result := TryObjectCreateFromProgID([ProgID_FreeThreadedDOMDocument60, ProgID_FreeThreadedDOMDocument40]) as IXMLDOMDocument;
      if not Assigned(Result) then
        raise EMSXMLDomException.Create(SMSDOMNotInstalled);
    end;function PrettyFormat(const AXML: String): String;  
    var
      FXMLDoc: 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
      Result := AXML;
      FXMLDoc := CreateDOMDocument;
      if Assigned(FXMLDoc) and FXMLDoc.loadXML(AXML) then
      begin
        TraverseNode(FXMLDoc.documentElement, '');
        Result := FXMLDoc.xml;
      end;
    end;end.
    使用实例:
    Memo2.Text := PrettyFormat('<root><budded/><who/></root>');
      

  3.   

    格式化后结果:
    <root>
    <budded/>
    <who/>
    </root>
      

  4.   

    无需那么复杂,设置XMLDocument的Options属性就可以了,doNodeAutoIndent 才是关键的。举个例子:uses XMLIntf,XMLDoc;procedure TForm1.Button1Click(Sender: TObject);
    var
        xmlDoc: IXMLDocument;
        aNode: IXMLNode;
    begin
        xmlDoc := TXMLDocument.Create(nil);
        try
            // 加入版本信息
            xmlDoc.Active := True;
            xmlDoc.Version := '1.0';
            xmlDoc.Encoding :='GB2312';        xmlDoc.Options := xmlDoc.Options + [doNodeAutoIndent];        // 加入根结点
            aNode := xmlDoc.AddChild('RootNode');
            // 加入子结点
            aNode := aNode.AddChild('ChildNode');
            // 设置子结点属性
            aNode.SetAttribute('Name', '名称');
            aNode.SetAttribute('Len', '长度');
            // 设置子结点内容
            aNode.Text := '文本内容';        xmlDoc.SaveToFile('C:\ccrun\123.xml');
        finally
            xmlDoc := nil ;
        end;
    end;
      

  5.   

    放到记事本里是了为了解析吧.这个没有关系吧
    想查看,直接把txt后缀修改了XML
      

  6.   

    如果用IDOMDocument呢  XMLDoc: TXMLDocument;
      XMLDom: IDOMDocument;
      iXml,iHITZD: IDOMNode;
      //导出XML文件
      XMLDoc.Active:= false;
      XMLDoc.XML.Text:='';
      XMLDoc.Active:=true;  XMLDom:=XMLDoc.DOMDocument;
      iXml:=XMLDom.appendChild(XMLDom.createElement('xml'));
      iHITZD:= iXml.appendChild(XMLDom.createElement('HITZD'));
    ……怎么弄