Listview question.

Peter Kiers
Peter Kiers used Ask the Experts™
on
Dear Experts,

I have a Button, a ListView and a XMLDocument-component on my form.
I have put the Button1Click procedure in the code-section. When I press
on the button it uses the XMLDocument-component to parse the xml-file
that is specified in the procedure. It searches for the nodes that begins with 'BG'  
and displays from every node that begins with BG the values from Val, Dt and TM
into the Listview columns Value, Date and Time.

AND IT WORKS SUPER.

Now I want to add a fourt column called Day and every cell of the column Day
has to look in the cell Date and give the name of the day in the cell.
If you don't know what I mean. I have allready asked this question but then
I had a ClientDataset instead of a Listview.

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26274267.html

Is that possible? I hope someone knows the answer and is willing to help me.

Greetings,
Peter Kiers

procedure TForm1.Button1Click(Sender: TObject);
var
  LoopNodes : IDOMNodeList;
  i: Integer;
begin
  XMLDocument1.FileName := '.\MyDB.xml';
  XMLDocument1.Active := True;
  try
    LoopNodes:= XMLDocument1.DOMDocument.getElementsByTagName( 'BG' );

    ListView1.Items.BeginUpdate;
    try
      for i:= 0 to LoopNodes.length -1 do
        with ListView1.Items.add do begin
          Caption:= LoopNodes[i].attributes.getNamedItem('Dt').nodeValue;
          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Tm').nodeValue );
          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Val').nodeValue );
        end;
    finally
      ListView1.Items.EndUpdate;
    end;

  finally
    XMLDocument1.Active := False;
  end;
end;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
procedure TForm1.Button1Click(Sender: TObject);
const
  FormatSettings: TFormatSettings = ( DateSeparator: '-'; ShortDateFormat: 'yyyy-mm-dd'; LongDateFormat: 'yyyy-mm-dd');
  Days : array[1..7] of string
    = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var
  LoopNodes : IDOMNodeList;
  i: Integer;
  DateTime: TDateTime;
begin
  XMLDocument1.FileName := '.\XMLDocument1.xml';
  XMLDocument1.Active := True;
  try
    LoopNodes:= XMLDocument1.DOMDocument.getElementsByTagName( 'BG' );

    ListView1.Items.BeginUpdate;
    try
      for i:= 0 to LoopNodes.length -1 do
        with ListView1.Items.add do begin
          Caption:= LoopNodes[i].attributes.getNamedItem('Val').nodeValue;
          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Dt').nodeValue );
          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Tm').nodeValue );

          if TryStrToDate(LoopNodes[i].attributes.getNamedItem('Dt').nodeValue, DateTime, FormatSettings) then
            SubItems.Add( Days[ DayOfWeek(DateTime) ] )
          else
            SubItems.Add( 'Can''t convert date' );

        end;
    finally
      ListView1.Items.EndUpdate;
    end;

  finally
    XMLDocument1.Active := False;
  end;
end;
Peter KiersOperator

Author

Commented:
It works great,
Thanks. PK

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial