Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Общие вопросы > Delphi&XML


Автор: max-@ 10.4.2005, 14:43
Как парсить XML в Delphi покажите простинкий пример....пожалуйста!
Добавлено @ 14:47
блин уже сам разобрался................а как записывать в XML средствами Delphi

Автор: SPrograMMer 10.4.2005, 17:28
Цитата(max @ 10.4.2005, 14:43)
Как парсить XML в Delphi покажите простинкий пример....пожалуйста!
Добавлено @ 14:47
блин уже сам разобрался................а как записывать в XML средствами Delphi

ХМ, ну и как же ты разобрался Как парсить XML в Delphi
Чего использовал?
Добавлено @ 17:29
TXMLDocument с вкладки Internet посмотри...

Автор: Medved 10.4.2005, 23:28
Вот маленький пример из моей программы:

Какие типы сохраняем:

Код

procedure TSettingValue.SetDefault;
begin
{General}
  IsLogFile := true;
  PathLogFile := ProgramPath+cLogFileName;
  ShowInTaskbar := true;
  ShowInSystemtray := true;
  MinimizeToSystemtray := true;

  Separator :=  '$';
  FileFilterDialogWindow := 'MS Excel files (*.xls)|*.XLS|MS Word files (*.doc)|*.DOC|Text files (*.txt)|*.TXT|All files (*.*)|*.*';
  DefaultFilterIndex := 4;

{Path} //*
  LangauageFolder := ProgramPath+cLangauageFolderName;
  DataFolder := ProgramPath+cDataFolderName;
  AttachFolder := ProgramPath+cAttachFolderName;

{Position} //*
  Bottom := 100;
  Left := 100;
  Right := 750;
  Top := 550;

end;


Создаем XML:

Код

procedure TStoreSetting.CreateXMLDoc(AComponent: TComponent; ACreate: boolean);
var
  appIXMLNode,tmpIXMLNodeChild,tmpIXMLNodeChild2 : IXMLNode;
begin
  SetDefault;

  if ACreate then
    FXMLDocument := TXMLDocument.Create(AComponent)
  else
    FXMLDocument.XML.Text := '';

  FXMLDocument.Active := false;

  try
    try
      FXMLDocument.NodeIndentStr := '  ';
      FXMLDocument.DOMVendor := GetDOMVendor('MSXML');
      FXMLDocument.Options := [doNodeAutoCreate,doNodeAutoIndent,doAttrNull,doAutoPrefix,doNamespaceDecl];
      FXMLDocument.Active := true;

      FXMLDocument.Version := UTF8Encode('1.0');
      FXMLDocument.Encoding := UTF8Encode('utf-8');

      appIXMLNode := FXMLDocument.AddChild(UTF8Encode(UpperCase('application')));
      with appIXMLNode do
      begin
        Attributes['name'] := UTF8Encode(cApplicationName);
        Attributes['version'] := UTF8Encode(Self.ProgramVersion);
      end;

      tmpIXMLNodeChild := appIXMLNode.AddChild(UTF8Encode(UpperCase('position')));
      with tmpIXMLNodeChild do
      begin
        Attributes['bottom']  := Bottom;
        Attributes['left']    := Left;
        Attributes['right']   := Right;
        Attributes['top']     := Top;
      end;

      tmpIXMLNodeChild := appIXMLNode.AddChild(UTF8Encode(UpperCase('path')));
      with tmpIXMLNodeChild do
      begin
        Attributes['LangauageFolder'] := UTF8Encode(LangauageFolder);
        Attributes['DataFolder']      := UTF8Encode(DataFolder);
        Attributes['AttachFolder']    := UTF8Encode(AttachFolder);
      end;

      tmpIXMLNodeChild := appIXMLNode.AddChild(UTF8Encode(UpperCase('general')));
      with tmpIXMLNodeChild do
      begin
        Attributes['IsLogFile']               := Ord(IsLogFile);
        Attributes['PathLogFile']             := UTF8Encode(PathLogFile);
        Attributes['ShowInTaskbar']           := Ord(ShowInTaskbar);
        Attributes['ShowInSystemtray']        := Ord(ShowInSystemtray);
        Attributes['MinimizeToSystemtray']    := Ord(MinimizeToSystemtray);
        Attributes['FileFilterDialogWindow']  := UTF8Encode(FileFilterDialogWindow);
        Attributes['DefaultFilterIndex']      := DefaultFilterIndex;
      end;


      FXMLDocument.SaveToFile(Self.ProgramPath+cServiceApplicationName+cDefExtXML);
      FXMLDocument.FileName := (Self.ProgramPath+cServiceApplicationName+cDefExtXML);
      if FXMLDocument.Active then FXMLDocument.Active := false;

    except

    end;
  finally
  end;
end;


Загружаю:

Код

function TStoreSetting.Load: boolean;
var
  appIXMLNode,tmpIXMLNodeChild,tmpIXMLNodeChild2 : IXMLNode;
begin
  result := false;
  try
    try
      FXMLDocument.Active := true;

      appIXMLNode := FXMLDocument.ChildNodes.FindNode(UTF8Encode(UpperCase('application')));
      if appIXMLNode = nil then exit;

      tmpIXMLNodeChild := appIXMLNode.ChildNodes.FindNode(UTF8Encode(UpperCase('position')));
      if tmpIXMLNodeChild = nil then exit;
      with tmpIXMLNodeChild do
      begin
        Bottom  := Attributes['bottom'];
        Left    := Attributes['left'];
        Right   := Attributes['right'];
        Top     := Attributes['top'];
      end;

      tmpIXMLNodeChild := appIXMLNode.ChildNodes.FindNode(UTF8Encode(UpperCase('path')));
      if tmpIXMLNodeChild = nil then exit;
      with tmpIXMLNodeChild do
      begin
        LangauageFolder := Attributes['LangauageFolder'];
        DataFolder      := Attributes['DataFolder'];
        AttachFolder    := Attributes['AttachFolder'];
      end;

      tmpIXMLNodeChild := appIXMLNode.ChildNodes.FindNode(UTF8Encode(UpperCase('general')));
      if tmpIXMLNodeChild = nil then exit;
      with tmpIXMLNodeChild do
      begin
        IsLogFile               := Attributes['IsLogFile'];
        PathLogFile             := Attributes['PathLogFile'];
        ShowInTaskbar           := Attributes['ShowInTaskbar'];
        ShowInSystemtray        := Attributes['ShowInSystemtray'];
        MinimizeToSystemtray    := Attributes['MinimizeToSystemtray'];
        FileFilterDialogWindow  := Attributes['FileFilterDialogWindow'];
        DefaultFilterIndex      := Attributes['DefaultFilterIndex'];
      end;

      result := true;
    except
    end;

  finally
    FXMLDocument.Active := false;
    if not Result then CreateXMLDoc(Application,false);
  end;
end;


Сохраняю:

Код

function TStoreSetting.Save: boolean;
var
  appIXMLNode,tmpIXMLNodeChild,tmpIXMLNodeChild2 : IXMLNode;
begin

  result := false;
  try
    try
      FXMLDocument.Active := true;
      appIXMLNode := FXMLDocument.ChildNodes.FindNode(UTF8Encode(UpperCase('application')));
      if appIXMLNode = nil then exit;

      tmpIXMLNodeChild := appIXMLNode.ChildNodes.FindNode(UTF8Encode(UpperCase('position')));
      with tmpIXMLNodeChild do
      begin
        Attributes['bottom']  := Bottom;
        Attributes['left']    := Left;
        Attributes['right']   := Right;
        Attributes['top']     := Top;
      end;

      tmpIXMLNodeChild := appIXMLNode.ChildNodes.FindNode(UTF8Encode(UpperCase('path')));
      with tmpIXMLNodeChild do
      begin
        Attributes['LangauageFolder'] := UTF8Encode(LangauageFolder);
        Attributes['DataFolder']      := UTF8Encode(DataFolder);
        Attributes['AttachFolder']    := UTF8Encode(AttachFolder);
      end;

      tmpIXMLNodeChild := appIXMLNode.ChildNodes.FindNode(UTF8Encode(UpperCase('general')));
      with tmpIXMLNodeChild do
      begin
        Attributes['IsLogFile']               := Ord(IsLogFile);
        Attributes['PathLogFile']             := UTF8Encode(PathLogFile);
        Attributes['ShowInTaskbar']           := Ord(ShowInTaskbar);
        Attributes['ShowInSystemtray']        := Ord(ShowInSystemtray);
        Attributes['MinimizeToSystemtray']    := Ord(MinimizeToSystemtray);
        Attributes['FileFilterDialogWindow']  := UTF8Encode(FileFilterDialogWindow);
        Attributes['DefaultFilterIndex']      := DefaultFilterIndex;
      end;

      result := true;
    except
    end;

  finally
    FXMLDocument.Active := false;
    if not result then CreateXMLDoc(Application,false);
  end;
end;

Автор: Петрович 11.4.2005, 10:01
Перенесено из раздела Дельфи

Автор: s-mike 11.4.2005, 23:08
Цитата(max @ 10.4.2005, 14:43)
Как парсить XML в Delphi покажите простинкий пример....пожалуйста!

http://forum.sources.ru/index.php?showtopic=81572&hl=xml

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)