[fpc-pascal] TDOMElement and multiple attributes

Reimar Grabowski reimgrab at web.de
Mon Mar 21 15:34:28 CET 2011


On Fri, 18 Mar 2011 20:16:08 +0100
Mattias Gaertner <nc-gaertnma at netcologne.de> wrote:

> Maybe Reimar can give an example when it does not work.
I am to lazy to attach the code, so here it is (you should know how to get it into Lazarus without too much hassle :) ):

program xmlattributes;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, CustApp,
  DOM, XMLWrite;
type

  { TXMLAttributes }

  TXMLAttributes = class(TCustomApplication)
  protected
    procedure DoRun; override;
  public
  end;

{ TXMLAttributes }

procedure TXMLAttributes.DoRun;
var
  ErrorMsg: String;
  Document: TXMLDocument;
  RootNode, Node: TDOMNode;
begin
  try
    Document:=TXMLDocument.Create;
    RootNode:=Document.CreateElement('ROOT');
    TDOMElement(RootNode).SetAttribute('someAttrib', 'someValue');
    Document.AppendChild(RootNode);

    RootNode:=Document.DocumentElement;
    Node:=Document.CreateElement('SomeNode');
    TDOMElement(Node).SetAttribute('aaaAttribute', '1');
    TDOMElement(Node).SetAttribute('aAttribute', '2');
    TDOMElement(Node).SetAttribute('ccAttribute', '4');
    TDOMElement(Node).SetAttribute('bAttribute', '3');
    RootNode.Appendchild(Node);

    WriteXMLFile(Document, 'test.xml');
  finally
    Document.Free;
  end;

  // stop program loop
  Terminate;
end;

var
  Application: TXMLAttributes;

{$R *.res}

begin
  Application:=TXMLAttributes.Create(nil);
  Application.Title:='XMLAttributes';
  Application.Run;
  Application.Free;
end.

someone at somewhere:~/pascal/Projects/xmldemo$ cat test.xml 
<?xml version="1.0"?>
<ROOT someAttrib="someValue">
  <SomeNode aAttribute="2" bAttribute="3" ccAttribute="4" aaaAttribute="1"/>
</ROOT>

Looks like the order is governed by the length of the attribute name first and then alphabetically.

R.
-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?



More information about the fpc-pascal mailing list