我用的是delphi里的IXMLDOCUMENT控件,在写XML文件时程序自动把XML中的所有单引号更改为双引号,还有程序每输入一个子结点不能自动换行。请各位高手不吝赐教,这两个问题该如何解决啊。小弟先行感谢了!

解决方案 »

  1.   

    NativeXml 不错 delphi自带的 没用过
    http://www.delphibox.com/article.asp?articleid=2654
    http://www.delphifans.com/SoftView/SoftView_1295.html
      

  2.   

    我稍微封装了两个函数
    楼主参考使用,没问题//==============================================================================
    //  Function: 扩充TXMLDocument的操作便利性
    //==============================================================================
    unit UXMLDocument;interfaceuses
      XMLIntf, XMLDoc;type
      TMyXMLDocument = class(TXMLDocument)
      public
        function GetNodeValue(NodeName: array of WideString;
          const DefValue: OleVariant): OleVariant;
        procedure SetNodeValue(NodeName: array of WideString;
          const Value: OleVariant);
      end;implementation{ TMyXMLDocument }function TMyXMLDocument.GetNodeValue(NodeName: array of WideString;
      const DefValue: OleVariant): OleVariant;
    //==============================================================================
    //  Function: 获得节点值,如果节点不存在,则返回默认值DefValue
    //Parameters: NodeName表示多级节点名称,DefValue表示如果节点不存在的返回值
    //    Result: 节点内容
    //==============================================================================
    var
      I: Integer;
      Root: IXMLNode;
    begin
      Result := DefValue;
      if DocumentElement = nil then Exit;
      Root := DocumentElement;
      for I := 0 to Length(NodeName) -1 do
      begin
        if Root = nil then Break;
        Root := Root.ChildNodes.FindNode(NodeName[I]);
      end;
      if Root <> nil then
        Result := Root.NodeValue;
    end;procedure TMyXMLDocument.SetNodeValue(NodeName: array of WideString;
      const Value: OleVariant);
    //==============================================================================
    //  Function: 设置节点内容,如果节点不存在,则创建节点;如果根节点不存在,则不进
    //            行任何操作
    //Parameters: NodeName表示多级节点名称,Value表示节点值
    //==============================================================================
    var
      I: Integer;
      Root, Root2: IXMLNode;
    begin
      if DocumentElement = nil then Exit;
      Root := DocumentElement;
      for I := 0 to Length(NodeName) -1 do
      begin
        Root2 := Root.ChildNodes.FindNode(NodeName[I]);
        if Root2 = nil then
          Root2 := Root.AddChild(NodeName[I]);
        Root := Root2;
      end;
      Root.NodeValue := Value;
    end;end.测试单元
    --------------
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls, XMLIntf, ComCtrls,
      UXMLDocument;type
      TfrmMain = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      frmMain: TfrmMain;
    const
      SettingFileName = 'setting.xml';implementation
    {$R *.dfm}procedure TfrmMain.FormCreate(Sender: TObject);
    var
      XML: TMyXMLDocument;
    begin
      XML := TMyXMLDocument.Create(Application);
      XML.Options := [doNodeAutoCreate, doNodeAutoIndent, doAttrNull, doAutoPrefix, doNamespaceDecl];
      try
        if FileExists(SettingFileName) then
          XML.LoadFromFile(SettingFileName)
        else
        begin
          XML.Active := True;
          XML.Create(SettingFileName);
          XML.AddChild('Setting', '1.0');
          XML.SaveToFile(SettingFileName);
        end;
        Width := XML.GetNodeValue([Name, 'Width'], Width);
        Height := XML.GetNodeValue([Name, 'Height'], Height);
        Top := XML.GetNodeValue([Name, 'Top'], Top);
        Left := XML.GetNodeValue([Name, 'Left'], Left);
      finally
        XML.Free;
      end;
    end;procedure TfrmMain.FormDestroy(Sender: TObject);
    var
      XML: TMyXMLDocument;
    begin
      XML := TMyXMLDocument.Create(Application);
      XML.Options := [doNodeAutoCreate, doNodeAutoIndent, doAttrNull, doAutoPrefix, doNamespaceDecl];
      try
        if FileExists(SettingFileName) then
          XML.LoadFromFile(SettingFileName)
        else
        begin
          XML.Active := True;
          XML.Create(SettingFileName);
          XML.AddChild('Setting', '1.0');
        end;
        XML.SetNodeValue([Name, 'Width'], Width);
        XML.SetNodeValue([Name, 'Height'], Height);
        XML.SetNodeValue([Name, 'Top'], Top);
        XML.SetNodeValue([Name, 'Left'], Left);
      finally
        XML.SaveToFile(SettingFileName);
        XML.Free;
      end;
    end;end.
      

  3.   

    IXMLDOCUMENT中的OPTIONS中的
    donodeautoindent属性设置true,接点可以自动换行
      

  4.   

    对于XML文件,换行不换行,没有任何影响的
      

  5.   

    多谢 小虫 和 黄金左脚, 只是现在在xml文件里的引号全都是双引号,请问怎样让它变为单引号啊,多谢
      

  6.   

    别用XML处理,直接当文本文件处理,读取后全部替换双引号为单引号,然后再保存
      

  7.   

    function StringReplace(const S, OldPattern, NewPattern: string; Flags: 
    TReplaceFlags): string;DescriptionStringReplace replaces occurrences of the substring specified by OldPattern with the substring specified by NewPattern. StringReplace assumes that the source string may contain Multibyte characters.S is the source string, whose substrings are changed.OldPattern is the substring to locate and replace with NewPattern.NewPattern is the substring to substitute for occurrences of OldPattern.Flags is a set of flags that govern how StringReplace locates and replaces occurrences of OldPattern. If Flags does not include rfReplaceAll, StringReplace only replaces the first occurrence of OldPattern in S. Otherwise, StringReplace replaces all instances of OldPattern with NewPattern. If the Flags parameter includes rfIgnoreCase, The comparison operation is case insensitive.