?
Solved

Parsing this simple XML

Posted on 2008-06-23
1
Medium Priority
?
267 Views
Last Modified: 2010-04-05
Hi,

I have "TMemo" that has XML content like this (see in the code snippet).

How do I parse that XML and display:

- CaseID value
- CaseTitle value

And also display all the "ActivityDate" and "ActivityDetails" from all the <CaseActivity> nodes?

Thanks.


<?xml version="1.0" encoding="utf-8" ?>
<CaseDetailResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<CaseId>1234567890</CaseId>
<CaseTitle>Testing</CaseTitle>
<CaseStatus>Closed</CaseStatus>
<SerialNumber />
<Activities>
<CaseActivity>
<Activity>Create</Activity>
<ActivityDate>01-01-2008</ActivityDate>
<ActivityDetails>Created by John</ActivityDetails>
</CaseActivity>
<CaseActivity>
<Activity>Status Change</Activity>
<ActivityDate>01-02-2008</ActivityDate>
<ActivityDetails>Case Opened</ActivityDetails>
</CaseActivity>
<CaseActivity>
<Activity>Status Change</Activity>
<ActivityDate>01-03-2008</ActivityDate>
<ActivityDetails>Case Complete</ActivityDetails>
</CaseActivity>
</Activities>
</CaseDetailResult>

Open in new window

0
Comment
Question by:dudup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 1000 total points
ID: 21851939
here's a quick demo I just wrote up for you using your sample file
I saved your file as "file3.xml" for this demo
unit Unit3;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,
  MSXML_TLB, comobj, ActiveX;
 
type
  TForm3 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FDocument : IXMLDOMDocument;
    procedure ParseCaseXML;
  public
    { Public declarations }
  end;
 
var
  Form3: TForm3;
 
implementation
 
{$R *.DFM}
 
procedure TForm3.Button1Click(Sender: TObject);
var
  i: integer;
begin
  FDocument := nil;
  OleCheck(CoCreateInstance(Class_DOMDocument, nil, CLSCTX_ALL,IXMLDOMDocument, FDocument));
  if FDocument <> nil then
  begin
    try
      if not FDocument.load('file3.xml') then
        ShowMessage('Error loading DOMDocument');
      ParseCaseXML;
    finally
      FDocument := nil;
    end;
  end
  else
    ShowMessage('Failed to create XML object');
end;
 
procedure TForm3.ParseCaseXML;
var
 rootnode_, CaseDetailResultNode_, ActivitiesNode_  : IXMLDOMNode;
 tempnode_: IXMLDOMNode;
 lstNodes : IXMLDOMNodeList; // list of activities
 ActivityNode_: IXMLDOMElement;
 activityindex: integer;
begin
  rootnode_ := FDocument.firstChild;
  if rootnode_ = nil then exit;
  CaseDetailResultNode_ := rootnode_.selectSingleNode('//CaseDetailResult');
  if assigned(CaseDetailResultNode_) then
  begin
    tempnode_ := CaseDetailResultNode_.selectSingleNode('CaseId');
    memo1.Lines.add(Format('%s : %s', [tempnode_.nodeName, tempnode_.text] ));
    tempnode_ := CaseDetailResultNode_.selectSingleNode('CaseTitle');
    memo1.Lines.add(Format('%s : %s', [tempnode_.nodeName, tempnode_.text] ));
 
    activityindex := 0; // just a simple visual reference
    ActivitiesNode_ := CaseDetailResultNode_.selectSingleNode('Activities');
    if assigned(ActivitiesNode_) then
    begin
      lstNodes := ActivitiesNode_.selectNodes('CaseActivity');
      if assigned(lstNodes) then
      begin
        ActivityNode_ := lstNodes.nextNode as IXMLDOMElement;
        while assigned(ActivityNode_) do
        begin
          inc(activityindex);
          memo1.lines.add(Format('Activity #%d', [activityindex]));
 
          tempnode_ := ActivityNode_.selectSingleNode('Activity');
          memo1.Lines.add(Format('     %s : %s', [tempnode_.nodeName, tempnode_.text] ));
          tempnode_ := ActivityNode_.selectSingleNode('ActivityDate');
          memo1.Lines.add(Format('     %s : %s', [tempnode_.nodeName, tempnode_.text] ));
          tempnode_ := ActivityNode_.selectSingleNode('ActivityDetails');
          memo1.Lines.add(Format('     %s : %s', [tempnode_.nodeName, tempnode_.text] ));
 
          memo1.lines.add('----------');
          ActivityNode_ := lstNodes.nextNode as IXMLDOMElement;
        end;
      end;
    end;
  end;
end;
 
end.
 
 
**** Form follows ****
 
object Form3: TForm3
  Left = 249
  Top = 107
  Width = 503
  Height = 427
  Caption = 'Form3'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 24
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Memo1: TMemo
    Left = 24
    Top = 52
    Width = 329
    Height = 301
    TabOrder = 1
  end
end

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question