Question : Parse xml-file

Hi,

I have a Button, a ListView and a XMLDocument-component on my form.
When I press on the button it uses the XMLDocument-component to parse
the xml-file hat 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.

This is a few lines from the xml-file:

<BG Val="7.9" Dt="2010-07-04" Tm="15:50" D="1"/>
<BG Val="18.8" Dt="2010-07-04" Tm="14:34" D="1"/>
<BG Val="9.7" Dt="2010-07-04" Tm="12:16" D="1"/>
<BG Val="4.1" Dt="2010-07-03" Tm="23:41" D="1"/>

I have put the procedure in the code-section.

But I need to parse this data from the xml-file too and display the data
between the (" ...  ")  it in a memo:

<ACSPIX Type="2106" SN="UI00452344" Ver="3.01.03"/>
<DEVICE  Name="Aviva" SN="01839366" Dt="2010-07-04" Tm="19:26" BGUnit="mmol/L"/>
<RECENTREC Dt="2010-07-04" Tm="17:14"/>

Who knows the answer and is willing to help me?

Peter Kiers


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
procedure TMainForm.btnGlucDataClick(Sender: TObject);
const
  FormatFrom: TFormatSettings = (DateSeparator: '-'; ShortDateFormat: 'yyyy-mm-dd');
  FormatTo: TFormatSettings = (DateSeparator: '-'; ShortDateFormat: 'dd-mm-yyyy');
  DoubleFormat: TFormatSettings = (DecimalSeparator: '.');
  Days : array[1..7] of string
    = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var
  LoopNodes : IDOMNodeList;
  i: Integer;
  DateTime: TDateTime;
  Val: Extended;
  strVal, strValText: string;
begin
  XMLDoc.FileName := '.\G1839366.XML';
  XMLDoc.Active := True;
  try
    LoopNodes:= XMLDoc.DOMDocument.getElementsByTagName( 'BG' );
    lstvGlucose.Items.BeginUpdate;
    try
      lstvGlucose.Items.Clear;
      for i:= 0 to LoopNodes.length -1 do
        with lstvGlucose.Items.add do
        begin
          if TryStrToDate(LoopNodes[i].attributes.getNamedItem('Dt').nodeValue, DateTime, FormatFrom) then
          begin
            Caption:= Days[ DayOfWeek(DateTime) ];
            SubItems.Add( DateToStr( DateTime, FormatTo ) );
          end
          else
          begin
            Caption:= 'can''t convert date';
            SubItems.Add( LoopNodes[i].attributes.getNamedItem('Dt').nodeValue );
          end;
          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Tm').nodeValue );
          SubItems.Add( LoopNodes[i].attributes.getNamedItem('Val').nodeValue );
          strVal:= StringReplace(LoopNodes[i].attributes.getNamedItem('Val').NodeValue, ',', '.', []);
          strValText:= '';
          if TryStrToFloat( strVal, Val, DoubleFormat ) then
            if Val < 4 then
              strValText:= 'HYPO'
            else if Val > 10 then
              strValText:= 'HYPER';
          SubItems.Add( strValText );
        end;
    finally
      lstvGlucose.Items.EndUpdate;
    end;
  finally
    XMLDoc.Active := False;
  end;
end;

Answer : Parse xml-file

Use selectSingleNode and XPATH to select the single nodes for their data.

Here's an example:

ACSPIXNode := XMLDoc.DOMDocument.selectSingleNode('//ACSPIX');
ACSPIXType := ACSPIXNode.attributes.getNamedItem('Type').nodeValue;

and so on...




Random Solutions  
 
programming4us programming4us