 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alejandro Castro Guest
|
Posted: Tue May 15, 2007 8:13 am Post subject: How to iterate |
|
|
I have the following XML file:
<?xml version = "1.0" encoding = "UTF-8"?>
<SQL>
<SCRIPT>
SELECT q1.*
FROM MyDataBase q1
WHERE q1.Year=:Year AND q1.Month=:Month
</SCRIPT>
<PARAM>
<NAME>Year</NAME>
<TYPE>Integer</TYPE>
<VALUE>2007</VALUE>
</PARAM>
<PARAM>
<NAME>Month</NAME>
<TYPE>Integer</TYPE>
<VALUE>4</VALUE>
</PARAM>
</SQL>
Using TXMLDocument, how can I iterate to find all PARAM nodes? (in this
example 2 nodes)
If I want to find the SCRIPT node I wrote
XML1.LoadFromFile(Open1.FileName);
xNode:= XML1.DocumentElement.ChildNodes.FindNode('SCRIPT');
if xNode<>nil then begin
Memo1.Lines.Text:=WrapText(xNode.Text,128);
end;
And its working OK, but now I need to find all PARAM nodes, (NextSibling?
FindNode?)
Thanks
Alejandro |
|
| Back to top |
|
 |
Eddie Shipman Guest
|
Posted: Tue May 15, 2007 7:22 pm Post subject: Re: How to iterate |
|
|
Alejandro Castro wrote:
| Quote: | I have the following XML file:
?xml version = "1.0" encoding = "UTF-8"?
SQL
SCRIPT
SELECT q1.*
FROM MyDataBase q1
WHERE q1.Year=:Year AND q1.Month=:Month
/SCRIPT
PARAM
NAME>Year</NAME
TYPE>Integer</TYPE
VALUE>2007</VALUE
/PARAM
PARAM
NAME>Month</NAME
TYPE>Integer</TYPE
VALUE>4</VALUE
/PARAM
/SQL
Using TXMLDocument, how can I iterate to find all PARAM nodes? (in
this example 2 nodes)
If I want to find the SCRIPT node I wrote
XML1.LoadFromFile(Open1.FileName);
xNode:= XML1.DocumentElement.ChildNodes.FindNode('SCRIPT');
if xNode<>nil then begin
Memo1.Lines.Text:=WrapText(xNode.Text,128);
end;
And its working OK, but now I need to find all PARAM nodes,
(NextSibling? FindNode?)
Thanks
Alejandro
|
You know, I have never liked working with TXMLDocument because it
really screws up the terminology of the XML DOM.
This is done using straight MSXML and is easier to understand and work.
Let me also make a suggestion; enclose your SCRIPT tag values in a
CDATASection. It may someday contain invalid chars, such as < or >.
This is your reformatted XML showing how:
<?xml version = "1.0" encoding = "UTF-8"?>
<SQL>
<SCRIPT>
<![CDATA[
SELECT q1.*
FROM MyDataBase q1
WHERE q1.Year=:Year AND q1.Month=:Month
]]>
</SCRIPT>
<PARAM>
<NAME>Year</NAME>
<TYPE>Integer</TYPE>
<VALUE>2007</VALUE>
</PARAM>
<PARAM>
<NAME>Month</NAME>
<TYPE>Integer</TYPE>
<VALUE>4</VALUE>
</PARAM>
</SQL>
Here is the code for parsing:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls, MSXML2_TLB, ComObj;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
oXMLDoc: IXMLDOMDocument2;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
oXMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
IXMLDOMDocument2;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
oXMLDoc := nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
oScriptNode: IXMLDOMNode;
oScriptValue: IXMLDOMCDATASection;
oParamNodes: IXMLDOMNodeList;
oParamNode: IXMLDOMNode;
i, j: Integer;
begin
Memo1.Clear;
Memo2.Clear;
oXMLDoc.load('c:\jig.xml');
oScriptNode := oXMLDoc.selectSingleNode('//SCRIPT');
if oScriptNode <> nil then
begin
oScriptValue := oScriptNode.firstChild as IXMLDOMCDATASection;
Memo1.Lines.text := Trim(oScriptValue.data);
end;
oParamNodes := oXMLDoc.selectNodes('//PARAM');
for i := 0 to oParamNodes.length-1 do
begin
oParamNode := oParamNodes.item[i];
for j := 0 to oParamNode.childNodes.length-1 do
begin
Memo2.Lines.Add(oParamNode.childNodes.item[j].nodeName + ':' +
oParamNode.childNodes.item[j].text);
end;
end;
end;
end.
-- |
|
| Back to top |
|
 |
Eddie Shipman Guest
|
Posted: Tue May 15, 2007 7:25 pm Post subject: Re: How to iterate |
|
|
Oh, BTW, this is how ytou would clear all values from the document:
oXMLDoc.LoadXML('<?xml version = "1.0" encoding = "UTF-8"?>'); |
|
| Back to top |
|
 |
Alejandro Castro Guest
|
Posted: Tue May 15, 2007 7:28 pm Post subject: Re: How to iterate |
|
|
Thank you Eddie
"Eddie Shipman" <mr_delphi_developer (AT) nospamyahoo (DOT) com> escribió en el mensaje
news:xn0f68atvh7ex8l001 (AT) forums (DOT) borland.com...
| Quote: | Alejandro Castro wrote:
I have the following XML file:
?xml version = "1.0" encoding = "UTF-8"?
SQL
SCRIPT
SELECT q1.*
FROM MyDataBase q1
WHERE q1.Year=:Year AND q1.Month=:Month
/SCRIPT
PARAM
NAME>Year</NAME
TYPE>Integer</TYPE
VALUE>2007</VALUE
/PARAM
PARAM
NAME>Month</NAME
TYPE>Integer</TYPE
VALUE>4</VALUE
/PARAM
/SQL
Using TXMLDocument, how can I iterate to find all PARAM nodes? (in
this example 2 nodes)
If I want to find the SCRIPT node I wrote
XML1.LoadFromFile(Open1.FileName);
xNode:= XML1.DocumentElement.ChildNodes.FindNode('SCRIPT');
if xNode<>nil then begin
Memo1.Lines.Text:=WrapText(xNode.Text,128);
end;
And its working OK, but now I need to find all PARAM nodes,
(NextSibling? FindNode?)
Thanks
Alejandro
You know, I have never liked working with TXMLDocument because it
really screws up the terminology of the XML DOM.
This is done using straight MSXML and is easier to understand and work.
Let me also make a suggestion; enclose your SCRIPT tag values in a
CDATASection. It may someday contain invalid chars, such as < or >.
This is your reformatted XML showing how:
?xml version = "1.0" encoding = "UTF-8"?
SQL
SCRIPT
![CDATA[
SELECT q1.*
FROM MyDataBase q1
WHERE q1.Year=:Year AND q1.Month=:Month
]]
/SCRIPT
PARAM
NAME>Year</NAME
TYPE>Integer</TYPE
VALUE>2007</VALUE
/PARAM
PARAM
NAME>Month</NAME
TYPE>Integer</TYPE
VALUE>4</VALUE
/PARAM
/SQL
Here is the code for parsing:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls, MSXML2_TLB, ComObj;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
oXMLDoc: IXMLDOMDocument2;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
oXMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
IXMLDOMDocument2;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
oXMLDoc := nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
oScriptNode: IXMLDOMNode;
oScriptValue: IXMLDOMCDATASection;
oParamNodes: IXMLDOMNodeList;
oParamNode: IXMLDOMNode;
i, j: Integer;
begin
Memo1.Clear;
Memo2.Clear;
oXMLDoc.load('c:\jig.xml');
oScriptNode := oXMLDoc.selectSingleNode('//SCRIPT');
if oScriptNode <> nil then
begin
oScriptValue := oScriptNode.firstChild as IXMLDOMCDATASection;
Memo1.Lines.text := Trim(oScriptValue.data);
end;
oParamNodes := oXMLDoc.selectNodes('//PARAM');
for i := 0 to oParamNodes.length-1 do
begin
oParamNode := oParamNodes.item[i];
for j := 0 to oParamNode.childNodes.length-1 do
begin
Memo2.Lines.Add(oParamNode.childNodes.item[j].nodeName + ':' +
oParamNode.childNodes.item[j].text);
end;
end;
end;
end.
--
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed May 16, 2007 1:20 am Post subject: Re: How to iterate |
|
|
"Eddie Shipman" <mr_delphi_developer (AT) nospamyahoo (DOT) com> wrote in message
news:xn0f68aw4h7i3ln002 (AT) forums (DOT) borland.com...
| Quote: | Oh, BTW, this is how ytou would clear all values from the document:
|
Or you could just set the Active property to False instead, or Clear()
the XML property.
Gambit |
|
| Back to top |
|
 |
Alejandro Castro Guest
|
Posted: Wed May 16, 2007 4:35 am Post subject: Re: How to iterate |
|
|
And how can I create a CDATA section, I could read i tbut I dont know how
can I create it
Thanks
Alejandro
"Eddie Shipman" <mr_delphi_developer (AT) nospamyahoo (DOT) com> escribió en el mensaje
news:xn0f68atvh7ex8l001 (AT) forums (DOT) borland.com...
| Quote: | Alejandro Castro wrote:
I have the following XML file:
?xml version = "1.0" encoding = "UTF-8"?
SQL
SCRIPT
SELECT q1.*
FROM MyDataBase q1
WHERE q1.Year=:Year AND q1.Month=:Month
/SCRIPT
PARAM
NAME>Year</NAME
TYPE>Integer</TYPE
VALUE>2007</VALUE
/PARAM
PARAM
NAME>Month</NAME
TYPE>Integer</TYPE
VALUE>4</VALUE
/PARAM
/SQL
Using TXMLDocument, how can I iterate to find all PARAM nodes? (in
this example 2 nodes)
If I want to find the SCRIPT node I wrote
XML1.LoadFromFile(Open1.FileName);
xNode:= XML1.DocumentElement.ChildNodes.FindNode('SCRIPT');
if xNode<>nil then begin
Memo1.Lines.Text:=WrapText(xNode.Text,128);
end;
And its working OK, but now I need to find all PARAM nodes,
(NextSibling? FindNode?)
Thanks
Alejandro
You know, I have never liked working with TXMLDocument because it
really screws up the terminology of the XML DOM.
This is done using straight MSXML and is easier to understand and work.
Let me also make a suggestion; enclose your SCRIPT tag values in a
CDATASection. It may someday contain invalid chars, such as < or >.
This is your reformatted XML showing how:
?xml version = "1.0" encoding = "UTF-8"?
SQL
SCRIPT
![CDATA[
SELECT q1.*
FROM MyDataBase q1
WHERE q1.Year=:Year AND q1.Month=:Month
]]
/SCRIPT
PARAM
NAME>Year</NAME
TYPE>Integer</TYPE
VALUE>2007</VALUE
/PARAM
PARAM
NAME>Month</NAME
TYPE>Integer</TYPE
VALUE>4</VALUE
/PARAM
/SQL
Here is the code for parsing:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls, MSXML2_TLB, ComObj;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
oXMLDoc: IXMLDOMDocument2;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
oXMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
IXMLDOMDocument2;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
oXMLDoc := nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
oScriptNode: IXMLDOMNode;
oScriptValue: IXMLDOMCDATASection;
oParamNodes: IXMLDOMNodeList;
oParamNode: IXMLDOMNode;
i, j: Integer;
begin
Memo1.Clear;
Memo2.Clear;
oXMLDoc.load('c:\jig.xml');
oScriptNode := oXMLDoc.selectSingleNode('//SCRIPT');
if oScriptNode <> nil then
begin
oScriptValue := oScriptNode.firstChild as IXMLDOMCDATASection;
Memo1.Lines.text := Trim(oScriptValue.data);
end;
oParamNodes := oXMLDoc.selectNodes('//PARAM');
for i := 0 to oParamNodes.length-1 do
begin
oParamNode := oParamNodes.item[i];
for j := 0 to oParamNode.childNodes.length-1 do
begin
Memo2.Lines.Add(oParamNode.childNodes.item[j].nodeName + ':' +
oParamNode.childNodes.item[j].text);
end;
end;
end;
end.
--
|
|
|
| Back to top |
|
 |
Eddie Shipman Guest
|
Posted: Wed May 16, 2007 6:40 pm Post subject: Re: How to iterate |
|
|
Alejandro Castro wrote:
| Quote: | And how can I create a CDATA section, I could read i tbut I dont know
how can I create it
Thanks
Alejandro
"Eddie Shipman" <mr_delphi_developer (AT) nospamyahoo (DOT) com> escribis en el
mensaje news:xn0f68atvh7ex8l001 (AT) forums (DOT) borland.com...
Alejandro Castro wrote:
I have the following XML file:
?xml version = "1.0" encoding = "UTF-8"?
SQL
SCRIPT
SELECT q1.*
FROM MyDataBase q1
WHERE q1.Year=:Year AND q1.Month=:Month
/SCRIPT
PARAM
NAME>Year</NAME
TYPE>Integer</TYPE
VALUE>2007</VALUE
/PARAM
PARAM
NAME>Month</NAME
TYPE>Integer</TYPE
VALUE>4</VALUE
/PARAM
/SQL
Using TXMLDocument, how can I iterate to find all PARAM nodes? (in
this example 2 nodes)
If I want to find the SCRIPT node I wrote
XML1.LoadFromFile(Open1.FileName);
xNode:= XML1.DocumentElement.ChildNodes.FindNode('SCRIPT');
if xNode<>nil then begin
Memo1.Lines.Text:=WrapText(xNode.Text,128);
end;
And its working OK, but now I need to find all PARAM nodes,
(NextSibling? FindNode?)
Thanks
Alejandro
You know, I have never liked working with TXMLDocument because it
really screws up the terminology of the XML DOM.
This is done using straight MSXML and is easier to understand and
work.
Let me also make a suggestion; enclose your SCRIPT tag values in a
CDATASection. It may someday contain invalid chars, such as < or >.
This is your reformatted XML showing how:
?xml version = "1.0" encoding = "UTF-8"?
SQL
SCRIPT
![CDATA[
SELECT q1.*
FROM MyDataBase q1
WHERE q1.Year=:Year AND q1.Month=:Month
]]
/SCRIPT
PARAM
NAME>Year</NAME
TYPE>Integer</TYPE
VALUE>2007</VALUE
/PARAM
PARAM
NAME>Month</NAME
TYPE>Integer</TYPE
VALUE>4</VALUE
/PARAM
/SQL
Here is the code for parsing:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls, MSXML2_TLB, ComObj;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
oXMLDoc: IXMLDOMDocument2;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
oXMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
IXMLDOMDocument2;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
oXMLDoc := nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
oScriptNode: IXMLDOMNode;
oScriptValue: IXMLDOMCDATASection;
oParamNodes: IXMLDOMNodeList;
oParamNode: IXMLDOMNode;
i, j: Integer;
begin
Memo1.Clear;
Memo2.Clear;
oXMLDoc.load('c:\jig.xml');
oScriptNode := oXMLDoc.selectSingleNode('//SCRIPT');
if oScriptNode <> nil then
begin
oScriptValue := oScriptNode.firstChild as IXMLDOMCDATASection;
Memo1.Lines.text := Trim(oScriptValue.data);
end;
oParamNodes := oXMLDoc.selectNodes('//PARAM');
for i := 0 to oParamNodes.length-1 do
begin
oParamNode := oParamNodes.item[i];
for j := 0 to oParamNode.childNodes.length-1 do
begin
Memo2.Lines.Add(oParamNode.childNodes.item[j].nodeName + ':' +
oParamNode.childNodes.item[j].text);
end;
end;
end;
end.
--
|
See your other thread.
-- |
|
| Back to top |
|
 |
|
|
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
|
|