BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Parsing XML deep imbeded nodes

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi XML
View previous topic :: View next topic  
Author Message
Bryan
Guest





PostPosted: Thu Aug 17, 2006 10:39 pm    Post subject: Parsing XML deep imbeded nodes Reply with quote



Hello,
I've tried for hours to parse an XML file and pull out the data I need without success...

Here is a sample strucutre of the XML...(I've left the XML header stuff off....
How to I pull out all Latitude and Longitude Values...?

<main>
<Level1>
<Level1Name>A Name</Level1Name>
<Level1Size>A Size</Level1Size>
</Level1>
<Data>
<Part1>
<SubPart1>
<Important_Info>
<Important_Location1>
<WGS84_Position>
<Latitude>43.5N</Latitude>
<Longitude>75.3W</Longitude>
</WGS84_Position>
</Important_Location1>
<Important_Location2>
<WGS84_Position>
<Latitude>43.5N</Latitude>
<Longitude>75.3W</Longitude>
</WGS84_Position>
</Important_Location2>
<Important_Location3>
<WGS84_Position>
<Latitude>43.5N</Latitude>
<Longitude>75.3W</Longitude>
</WGS84_Position>
</Important_Location3>
</Important_Info>
</SubPart1>
</Part1>
</Data>
</main>

Thanks for any help...
Bryan
Back to top
Bryan
Guest





PostPosted: Thu Aug 17, 2006 10:41 pm    Post subject: Re: Parsing XML deep imbeded nodes Reply with quote



I forgot to mention...

BDS 2006 using TXMLDocument....
Bryan
Back to top
Don Siders
Guest





PostPosted: Fri Aug 18, 2006 12:42 am    Post subject: Re: Parsing XML deep imbeded nodes Reply with quote



Quote:
Hello,
I've tried for hours to parse an XML file and pull out the data I need
without success...

Here is a sample strucutre of the XML...(I've left the XML header stuff
off....
How to I pull out all Latitude and Longitude Values...?

You need to navigate to the values using the ChildNodes for each xml node in
the hierarchy.

The alternative is to use the DOM model. In that case, I usually use the
native XML parser and XPath to avoid the abstraction layer(s) in
TXMLDocument.

hth...

Don
---

The Form:
----------
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 571
ClientWidth = 483
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
DesignSize = (
483
571)
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 400
Top = 540
Width = 75
Height = 25
Anchors = [akRight, akBottom]
Caption = 'Parse XML'
TabOrder = 0
OnClick = Button1Click
ExplicitLeft = 709
end
object ListView1: TListView
AlignWithMargins = True
Left = 3
Top = 3
Width = 477
Height = 529
Align = alTop
Anchors = [akLeft, akTop, akRight, akBottom]
Columns = <
item
Caption = 'Element'
Width = 150
end
item
Caption = 'Latitude'
Width = 150
end
item
Caption = 'Longitude'
Width = 150
end>
TabOrder = 1
ViewStyle = vsReport
ExplicitWidth = 786
end
object XMLDocument1: TXMLDocument
Options = [doNodeAutoCreate, doNodeAutoIndent, doAttrNull]
XML.Strings = (
'<main>'
'<Level1>'
'<Level1Name>A Name</Level1Name>'
'<Level1Size>A Size</Level1Size>'
'</Level1> '
'<Data>'
'<Part1>'
'<SubPart1>'
'<Important_Info>'
'<Important_Location1>'
'<WGS84_Position>'
'<Latitude>43.5N</Latitude>'
'<Longitude>75.3W</Longitude>'
'</WGS84_Position>'
'</Important_Location1>'
'<Important_Location2>'
'<WGS84_Position>'
'<Latitude>43.5N</Latitude>'
'<Longitude>75.3W</Longitude>'
'</WGS84_Position>'
'</Important_Location2>'
'<Important_Location3>'
'<WGS84_Position>'
'<Latitude>43.5N</Latitude>'
'<Longitude>75.3W</Longitude>'
'</WGS84_Position>'
'</Important_Location3>'
'</Important_Info>'
'</SubPart1>'
'</Part1> '
'</Data> '
'</main>')
Left = 64
Top = 432
DOMVendorDesc = 'MSXML'
end
object XPManifest1: TXPManifest
Left = 32
Top = 432
end
end
----------

The Code:
----------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms, Dialogs,
xmldom, XMLIntf, msxmldom, XMLDoc, ComCtrls, StdCtrls, XPMan;

type
TForm1 = class(TForm)
XMLDocument1: TXMLDocument;
XPManifest1: TXPManifest;
Button1: TButton;
ListView1: TListView;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
ANode: IXMLNode;
iIndex: Integer;
sContainer: string;
sLat: string;
sLong: string;
begin
ListView1.Items.BeginUpdate;
ListView1.Items.Clear;

if XMLDocument1.Active then XMLDocument1.Active := False;
XMLDocument1.Active := True;

for iIndex := 0 to
XMLDocument1.DocumentElement.ChildNodes['Data'].ChildNodes['Part1'].ChildNodes['SubPart1'].ChildNodes['Important_Info'].ChildNodes.Count-1
do
begin
ANode :=
XMLDocument1.DocumentElement.ChildNodes['Data'].ChildNodes['Part1'].ChildNodes['SubPart1'].ChildNodes['Important_Info'].ChildNodes[iIndex];

sContainer := ANode.LocalName;
sLat := ANode.ChildNodes['WGS84_Position'].ChildNodes['Latitude'].Text;
sLong :=
ANode.ChildNodes['WGS84_Position'].ChildNodes['Longitude'].Text;

with ListView1.Items.Add do
begin
Caption := sContainer;
SubItems.Add(sLat);
SubItems.Add(sLong);
end;
end;
ListView1.Items.EndUpdate;
end;

end.
----------
Back to top
Bryan
Guest





PostPosted: Fri Aug 18, 2006 1:18 am    Post subject: Re: Parsing XML deep imbeded nodes Reply with quote

Don,

Thank you so much.... your code works great....

I personally thought there might be an easier way than to drill down through each ChildNode after ChildNode to pull out the data... but if it works, it works... can't argue with that...

Thanks again.
Bryan
Back to top
Don Siders
Guest





PostPosted: Fri Aug 18, 2006 1:52 am    Post subject: Re: Parsing XML deep imbeded nodes Reply with quote

Quote:
I personally thought there might be an easier way than to drill down
through each ChildNode after ChildNode to pull out the data...

There is, of course. Use the DOM Object model and XPath expressions. For
me, it's always been easier to use MSXML directly when using XPath - instead
of TXMLDocument.

YMMV...
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri Aug 18, 2006 3:41 am    Post subject: Re: Parsing XML deep imbeded nodes Reply with quote

"Don Siders" <dsiders (AT) charter (DOT) net> wrote in message
news:44e4c6b1$1 (AT) newsgroups (DOT) borland.com...

Quote:
for iIndex := 0 to

XMLDocument1.DocumentElement.ChildNodes['Data'].ChildNodes['Part1'].ChildNod

es['SubPart1'].ChildNodes['Important_Info'].ChildNodes.Count-1
Quote:
do
begin
ANode :=

XMLDocument1.DocumentElement.ChildNodes['Data'].ChildNodes['Part1'].ChildNod

es['SubPart1'].ChildNodes['Important_Info'].ChildNodes[iIndex];

That is very inefficient. You should store the Important_Info node into a
variable:

var
ImportantInfo: IXMLNode;
begin
//...
ImportantInfo :=
XMLDocument1.DocumentElement.ChildNodes['Data'].ChildNodes['Part1'].ChildNod
es['SubPart1'].ChildNodes['Important_Info'];
for iIndex := 0 to ImportantInfo.ChildNodes.Count-1 do
begin
ANode := ImportantInfo.ChildNodes[iIndex];
//...
end;
//...
end;


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi XML All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.