Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Help using TXMLDocument 1

Status
Not open for further replies.

xzanti

Programmer
May 11, 2010
3
IE
I am trying to create a simple XML file but I am stuck on line 3 "customer id". I cannot include spaces in the xml element but I am not sure how to write this line in Delphi.

The xml file should read as follows:
<?xml version="1.0" encoding="utf-8"?>
<data>
<customer id="123456" name="Customer X Ltd"/>
<consignment id="LX1007401911070109">
<piece>NX1007401911070109</piece>
<piece>NX1007401911070110</piece>
</consignment>
</data>

My code is as follows (exception on line 7)
myDoc := TXMLDocument.Create(Application);
myDoc.Active := true;
myDoc.Options := [doNodeAutoCreate, doNodeAutoIndent];
myDoc.Version := '1.0';
myDoc.Encoding := 'utf-8';
rootNode := MyDoc.AddChild('data', '' );
SubNode := rootnode.AddChild('consignment id', '');
 
I think I sorted it using the following. The id and name are attritubes of the element.

SubNode.SetAttributeNS('id', '', '123456');
SubNode.SetAttributeNS('name', '', Customer X Ltd);
 
Your life will be musch easier if you use the XML databinding wizard. All you need to do is to create a template XML file and run the wizard, it will create the code for you...

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
here's an example:

template XML file:

Code:
<?xml version="1.0" encoding="utf-8"?>
<customers>
 <customer id="id1" name="Customer X Ltd">
 <consignments>
   <consignment id="id1">
    <pieces>
     <piece>piece1</piece>
     <piece>piece1</piece>  
    </pieces> 
   </consignment>
   <consignment id="id2">
    <pieces>
     <piece>piece1</piece>
     <piece>piece2</piece>  
    </pieces> 
   </consignment>
  </consignments>
 </customer> 
 <customer id="id2" name="Customer X Ltd">
 <consignments>
   <consignment id="id1">
    <pieces>
     <piece>piece1</piece>
     <piece>piece1</piece>  
    </pieces> 
   </consignment>
   <consignment id="id2">
    <pieces>
     <piece>piece1</piece>
     <piece>piece2</piece>  
    </pieces> 
   </consignment>
  </consignments>
 </customer> 
</customers>

resulting unit:
Code:
unit template;

interface

uses xmldom, XMLDoc, XMLIntf;

type

{ Forward Decls }

  IXMLCustomersType = interface;
  IXMLCustomerType = interface;
  IXMLConsignmentsType = interface;
  IXMLConsignmentType = interface;
  IXMLPiecesType = interface;

{ IXMLCustomersType }

  IXMLCustomersType = interface(IXMLNodeCollection)
    ['{F1399E4C-5473-4573-AB0C-A9A4E12172CD}']
    { Property Accessors }
    function Get_Customer(Index: Integer): IXMLCustomerType;
    { Methods & Properties }
    function Add: IXMLCustomerType;
    function Insert(const Index: Integer): IXMLCustomerType;
    property Customer[Index: Integer]: IXMLCustomerType read Get_Customer; default;
  end;

{ IXMLCustomerType }

  IXMLCustomerType = interface(IXMLNode)
    ['{4AF93DBF-5CFD-4E7F-8407-E408F66C4EB1}']
    { Property Accessors }
    function Get_Id: WideString;
    function Get_Name: WideString;
    function Get_Consignments: IXMLConsignmentsType;
    procedure Set_Id(Value: WideString);
    procedure Set_Name(Value: WideString);
    { Methods & Properties }
    property Id: WideString read Get_Id write Set_Id;
    property Name: WideString read Get_Name write Set_Name;
    property Consignments: IXMLConsignmentsType read Get_Consignments;
  end;

{ IXMLConsignmentsType }

  IXMLConsignmentsType = interface(IXMLNodeCollection)
    ['{2608B7B2-D5C1-4E25-BFDA-5F414D6876FB}']
    { Property Accessors }
    function Get_Consignment(Index: Integer): IXMLConsignmentType;
    { Methods & Properties }
    function Add: IXMLConsignmentType;
    function Insert(const Index: Integer): IXMLConsignmentType;
    property Consignment[Index: Integer]: IXMLConsignmentType read Get_Consignment; default;
  end;

{ IXMLConsignmentType }

  IXMLConsignmentType = interface(IXMLNode)
    ['{38A478D2-7D16-42AA-9A36-A416774FF44A}']
    { Property Accessors }
    function Get_Id: WideString;
    function Get_Pieces: IXMLPiecesType;
    procedure Set_Id(Value: WideString);
    { Methods & Properties }
    property Id: WideString read Get_Id write Set_Id;
    property Pieces: IXMLPiecesType read Get_Pieces;
  end;

{ IXMLPiecesType }

  IXMLPiecesType = interface(IXMLNodeCollection)
    ['{691D0D4F-7F7B-4951-83AF-4C60C10127BA}']
    { Property Accessors }
    function Get_Piece(Index: Integer): WideString;
    { Methods & Properties }
    function Add(const Piece: WideString): IXMLNode;
    function Insert(const Index: Integer; const Piece: WideString): IXMLNode;
    property Piece[Index: Integer]: WideString read Get_Piece; default;
  end;

{ Forward Decls }

  TXMLCustomersType = class;
  TXMLCustomerType = class;
  TXMLConsignmentsType = class;
  TXMLConsignmentType = class;
  TXMLPiecesType = class;

{ TXMLCustomersType }

  TXMLCustomersType = class(TXMLNodeCollection, IXMLCustomersType)
  protected
    { IXMLCustomersType }
    function Get_Customer(Index: Integer): IXMLCustomerType;
    function Add: IXMLCustomerType;
    function Insert(const Index: Integer): IXMLCustomerType;
  public
    procedure AfterConstruction; override;
  end;

{ TXMLCustomerType }

  TXMLCustomerType = class(TXMLNode, IXMLCustomerType)
  protected
    { IXMLCustomerType }
    function Get_Id: WideString;
    function Get_Name: WideString;
    function Get_Consignments: IXMLConsignmentsType;
    procedure Set_Id(Value: WideString);
    procedure Set_Name(Value: WideString);
  public
    procedure AfterConstruction; override;
  end;

{ TXMLConsignmentsType }

  TXMLConsignmentsType = class(TXMLNodeCollection, IXMLConsignmentsType)
  protected
    { IXMLConsignmentsType }
    function Get_Consignment(Index: Integer): IXMLConsignmentType;
    function Add: IXMLConsignmentType;
    function Insert(const Index: Integer): IXMLConsignmentType;
  public
    procedure AfterConstruction; override;
  end;

{ TXMLConsignmentType }

  TXMLConsignmentType = class(TXMLNode, IXMLConsignmentType)
  protected
    { IXMLConsignmentType }
    function Get_Id: WideString;
    function Get_Pieces: IXMLPiecesType;
    procedure Set_Id(Value: WideString);
  public
    procedure AfterConstruction; override;
  end;

{ TXMLPiecesType }

  TXMLPiecesType = class(TXMLNodeCollection, IXMLPiecesType)
  protected
    { IXMLPiecesType }
    function Get_Piece(Index: Integer): WideString;
    function Add(const Piece: WideString): IXMLNode;
    function Insert(const Index: Integer; const Piece: WideString): IXMLNode;
  public
    procedure AfterConstruction; override;
  end;

{ Global Functions }

function Getcustomers(Doc: IXMLDocument): IXMLCustomersType;
function Loadcustomers(const FileName: WideString): IXMLCustomersType;
function Newcustomers: IXMLCustomersType;

const
  TargetNamespace = '';

implementation

{ Global Functions }

function Getcustomers(Doc: IXMLDocument): IXMLCustomersType;
begin
  Result := Doc.GetDocBinding('customers', TXMLCustomersType, TargetNamespace) as IXMLCustomersType;
end;

function Loadcustomers(const FileName: WideString): IXMLCustomersType;
begin
  Result := LoadXMLDocument(FileName).GetDocBinding('customers', TXMLCustomersType, TargetNamespace) as IXMLCustomersType;
end;

function Newcustomers: IXMLCustomersType;
begin
  Result := NewXMLDocument.GetDocBinding('customers', TXMLCustomersType, TargetNamespace) as IXMLCustomersType;
end;

{ TXMLCustomersType }

procedure TXMLCustomersType.AfterConstruction;
begin
  RegisterChildNode('customer', TXMLCustomerType);
  ItemTag := 'customer';
  ItemInterface := IXMLCustomerType;
  inherited;
end;

function TXMLCustomersType.Get_Customer(Index: Integer): IXMLCustomerType;
begin
  Result := List[Index] as IXMLCustomerType;
end;

function TXMLCustomersType.Add: IXMLCustomerType;
begin
  Result := AddItem(-1) as IXMLCustomerType;
end;

function TXMLCustomersType.Insert(const Index: Integer): IXMLCustomerType;
begin
  Result := AddItem(Index) as IXMLCustomerType;
end;

{ TXMLCustomerType }

procedure TXMLCustomerType.AfterConstruction;
begin
  RegisterChildNode('consignments', TXMLConsignmentsType);
  inherited;
end;

function TXMLCustomerType.Get_Id: WideString;
begin
  Result := AttributeNodes['id'].Text;
end;

procedure TXMLCustomerType.Set_Id(Value: WideString);
begin
  SetAttribute('id', Value);
end;

function TXMLCustomerType.Get_Name: WideString;
begin
  Result := AttributeNodes['name'].Text;
end;

procedure TXMLCustomerType.Set_Name(Value: WideString);
begin
  SetAttribute('name', Value);
end;

function TXMLCustomerType.Get_Consignments: IXMLConsignmentsType;
begin
  Result := ChildNodes['consignments'] as IXMLConsignmentsType;
end;

{ TXMLConsignmentsType }

procedure TXMLConsignmentsType.AfterConstruction;
begin
  RegisterChildNode('consignment', TXMLConsignmentType);
  ItemTag := 'consignment';
  ItemInterface := IXMLConsignmentType;
  inherited;
end;

function TXMLConsignmentsType.Get_Consignment(Index: Integer): IXMLConsignmentType;
begin
  Result := List[Index] as IXMLConsignmentType;
end;

function TXMLConsignmentsType.Add: IXMLConsignmentType;
begin
  Result := AddItem(-1) as IXMLConsignmentType;
end;

function TXMLConsignmentsType.Insert(const Index: Integer): IXMLConsignmentType;
begin
  Result := AddItem(Index) as IXMLConsignmentType;
end;

{ TXMLConsignmentType }

procedure TXMLConsignmentType.AfterConstruction;
begin
  RegisterChildNode('pieces', TXMLPiecesType);
  inherited;
end;

function TXMLConsignmentType.Get_Id: WideString;
begin
  Result := AttributeNodes['id'].Text;
end;

procedure TXMLConsignmentType.Set_Id(Value: WideString);
begin
  SetAttribute('id', Value);
end;

function TXMLConsignmentType.Get_Pieces: IXMLPiecesType;
begin
  Result := ChildNodes['pieces'] as IXMLPiecesType;
end;

{ TXMLPiecesType }

procedure TXMLPiecesType.AfterConstruction;
begin
  ItemTag := 'piece';
  ItemInterface := IXMLNode;
  inherited;
end;

function TXMLPiecesType.Get_Piece(Index: Integer): WideString;
begin
  Result := List[Index].Text;
end;

function TXMLPiecesType.Add(const Piece: WideString): IXMLNode;
begin
  Result := AddItem(-1);
  Result.NodeValue := Piece;
end;

function TXMLPiecesType.Insert(const Index: Integer; const Piece: WideString): IXMLNode;
begin
  Result := AddItem(Index);
  Result.NodeValue := Piece;
end;

end.

how to use it:

Code:
uses template;
...
procedure createxmlfile;

var ICustomers: IXMLCustomersType;
    ICustomer : IXMLCustomerType;
  
begin
 ICustomers := NewCustomers;
 ICustomer := Result.Add;
 ICustomer.Id = 'customer id';
 ICustomer.Name = 'customer name';
 IConsigment := ICustomer.Consignments.Add;
 IConsigment.Id := 'consignment id';
 IConsigment.Pieces.Add('Piece1');
 IConsigment.Pieces.Add('Piece2');
 ICustomers.OwnerDocument.SaveToFile('yourfile.xml');
end;

10 minutes work...

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top