请高手帮忙给出分析这个xml的代码 ( 积分: 200 )

  • 主题发起人 主题发起人 qdlover
  • 开始时间 开始时间
Q

qdlover

Unregistered / Unconfirmed
GUEST, unregistred user!
我需要读出每个topic的name,cn,跟以下的sql里面的内容
因为只是为了做个接口,没空研究xml根式跟XMLDocument控件的用法了,请高手直接给出代码吧,多谢了,在线等。

<?xml version=&quot;1.0&quot; encoding=&quot;GBK&quot;?>
<conf>
<topic name=&quot;KSCDYDY&quot; cn=&quot;8.1.2.5 综合电压合格率(DY)&quot;>
<sql>
select
col_1_1_3_134 as KSCDYDY01,
'200704' as KSCDYDY02,
col_1_1_3_246 as KSCDYDY03,
col_1_1_3_248 as KSCDYDY04,
col_1_1_3_250 as KSCDYDY05,
col_1_1_3_252 as KSCDYDY06,
col_1_1_3_245 as KSCDYDY07,
col_1_1_3_255 as KSCDYDY08,
col_1_1_3_257 as KSCDYDY09,
col_1_1_3_259 as KSCDYDY10,
col_1_1_3_261 as KSCDYDY11,
col_1_1_3_254 as KSCDYDY12,
col_1_1_3_247 as KSCDYDY13,
col_1_1_3_249 as KSCDYDY14,
col_1_1_3_251 as KSCDYDY15,
col_1_1_3_253 as KSCDYDY16,
col_1_1_3_256 as KSCDYDY17,
col_1_1_3_258 as KSCDYDY18,
col_1_1_3_260 as KSCDYDY19,
col_1_1_3_262 as KSCDYDY20
from 南方电网静态表 s,南方电网动态表 d where s.col_1_1_3_1=d.col_1_1_3_1 and d.it_state='ITSTATE'
and col_1_1_3_134 is not null and col_1_1_3_246 is not null
</sql>
</topic>

<topic name=&quot;KSCSCSC&quot; cn=&quot;全省用电情况(SC)&quot;>
<sql>
select
col_1_1_3_134 as KSCSCSC01,
'200705' as KSCSCSC02,
col_1_1_3_284 as KSCSCSC05,
col_1_1_3_288 as KSCSCSC07,
col_1_1_3_292 as KSCSCSC08,
col_1_1_3_296 as KSCSCSC09,
col_1_1_3_300 as KSCSCSC03,
col_1_1_3_304 as KSCSCSC06,
col_1_1_3_308 as KSCSCSC04,
col_1_1_3_351 as KSCSCSC10,
col_1_1_3_355 as KSCSCSC11,
col_1_1_3_367 as KSCSCSC12,
col_1_1_3_371 as KSCSCSC13,
col_1_1_3_375 as KSCSCSC14,
col_1_1_3_379 as KSCSCSC15,
col_1_1_3_474 as KSCSCSC16
from 南方电网静态表 s,南方电网动态表 d where s.col_1_1_3_1=d.col_1_1_3_1 and d.it_state='ITSTATE'
and col_1_1_3_134 is not null
</sql>
</topic>

<topic name=&quot;KSCZGZG&quot; cn=&quot;供电综合情况&quot;>
<sql>
select
col_1_1_3_134 as KSCZGZG01,
'200704' as KSCZGZG02,
col_1_1_3_2041 as KSCZGZG03,
col_1_1_3_280 as KSCZGZG04,
col_1_1_3_2033 as KSCZGZG05,
col_1_1_3_2029 as KSCZGZG06,
col_1_1_3_2030 as KSCZGZG07,
col_1_1_3_2031 as KSCZGZG08,
col_1_1_3_2032 as KSCZGZG09,
col_1_1_3_2039 as KSCZGZG10,
col_1_1_3_2040 as KSCZGZG11,
col_1_1_3_355 as KSCZGZG12,
col_1_1_3_2035 as KSCZGZG13,
'0' as KSCZGZG14,
col_1_1_3_359 as KSCZGZG15,
col_1_1_3_363 as KSCZGZG16
from 南方电网静态表 s,南方电网动态表 d where s.col_1_1_3_1=d.col_1_1_3_1 and d.it_state='ITSTATE'
and col_1_1_3_134 is not null
</sql>
</topic>

<topic name=&quot;KSCDCDC&quot; cn=&quot;电厂生产情况&quot;>
<sql>
select
col_1_1_6_2096 as KSCDCDC01,
'200704' as KSCDCDC02,
col_1_1_6_478 as KSCDCDC03,
col_1_1_6_479 as KSCDCDC04,
col_1_1_6_342 as KSCDCDC05,
col_1_1_6_479 as KSCDCDC06,
col_1_1_6_523 as KSCDCDC07,
col_1_1_6_547 as KSCDCDC08,
col_1_1_6_527 as KSCDCDC09,
col_1_1_6_543 as KSCDCDC10,
col_1_1_6_683 as KSCDCDC11,
col_1_1_6_714 as KSCDCDC12,
col_1_1_6_486 as KSCDCDC13,
col_1_1_6_487 as KSCDCDC14,
col_1_1_6_528 as KSCDCDC15,
col_1_1_6_542 as KSCDCDC16,
col_1_1_6_525 as KSCDCDC17,
col_1_1_6_545 as KSCDCDC18,
col_1_1_6_526 as KSCDCDC19,
col_1_1_6_544 as KSCDCDC20,
col_1_1_6_682 as KSCDCDC21,
col_1_1_6_713 as KSCDCDC22
from 电力生产静态表 s,电力生产动态表 d where s.col_1_1_6_1=d.col_1_1_6_1 and d.it_state='ITSTATE'
and col_1_1_6_2096 is not null
</sql>
</topic>


<topic name=&quot;KSCHYHY&quot; cn=&quot;行业用电分类&quot;>
<sql>
select
col_1_1_7_2001 as KSCHYHY01,
'200704' as KSCHYHY02,
col_1_1_7_4 as KSCHYHY03,
col_1_1_7_5 as KSCHYHY04,
col_1_1_7_10 as KSCHYHY05,
col_1_1_7_11 as KSCHYHY06
from 用电分类静态表 s,用电分类动态表 d where s.col_1_1_7_1=d.col_1_1_7_1 and d.it_state='ITSTATE'
and col_1_1_7_2001 is not null
</sql>
</topic>

</conf>
 
新建一个Application
在Form上放一个ComboBox,一个Label,一个Memo

把如下代码复制到到单元中,再关联一下ComboBox以及Form的相关事件.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, msxml, ComObj, ActiveX, StdCtrls;

const
XmlFileName = 'D:/新建 文本文档 (2).xml';//改成你的文件名

type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Label1: TLabel;
Memo1: TMemo;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ComboBox1Select(Sender: TObject);
private
{ Private declarations }
m_Xml : IXMLDomDocument;
m_MainNode: IXMLDomNode;
procedure ReadXml();
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}


{ TForm1 }

procedure TForm1.ReadXml;
var
node: IXMLDomNode;
I : Integer;
begin
m_MainNode := m_Xml.selectSingleNode('conf');
if nil = m_MainNode then
Raise Exception.Create('Main Section &quot;conf&quot; not found!');
for I := 0 to m_MainNode.childNodes.length - 1 do
begin
node := m_MainNode.childNodes.item;
node := node.attributes.getNamedItem('name');
if node <> nil then
ComboBox1.Items.Add(node.text);
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
m_Xml := nil;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
m_Xml := CreateComObject(CLASS_DOMDocument) as IXMLDomDocument;
if nil = m_Xml then
Halt(0);
if not m_Xml.load('D:/新建 文本文档 (2).xml') then
Halt(0);
ReadXml();
end;

procedure TForm1.ComboBox1Select(Sender: TObject);
var
node: IXMLDomNode;
begin
if m_MainNode = nil then
Exit;
node := m_MainNode.selectSingleNode('topic[@name=''' + ComboBox1.Text + ''']');
if node = nil then
Exit;
if node.attributes.getNamedItem('cn') <> nil then
Label1.Caption := Format('cn:%s', [node.attributes.getNamedItem('cn').text]);
node := node.selectSingleNode('sql');
if node <> nil then
Memo1.Text := node.text;
end;

initialization
begin
CoInitialize(nil);
end;

finalization
begin
CoUninitialize();
end;

end.
 
已调试ok

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,XMLDoc,XMLIntf;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
function getText(aXMLDocument: IXMLDocument;iChoose: integer): String;
end;
var
Form1: TForm1;

implementation

{$R *.dfm}

const
sFileName = 'xx.xml'; //xml文件名

procedure TForm1.Button1Click(Sender: TObject);
var
aXMLDocument: IXMLDocument;
begin
aXMLDocument:= TXMLDocument.Create(nil);
try
Memo1.Lines.add(getText(aXMLDocument,1)); //1 : name
Memo2.Lines.add(getText(aXMLDocument,2)); //2 : cn
Memo3.Lines.add(getText(aXMLDocument,3)); //3 : sql
finally
aXMLDocument:= nil;
end;
end;

function TForm1.getText(aXMLDocument: IXMLDocument;iChoose: integer): String;
var
aXMLNode: IXMLNode;
aStrings: TStrings;
i: integer;
begin
Result:= '';
if Assigned(aXMLDocument)then
begin
aStrings:= TStringList.Create;
try
aXMLDocument.LoadFromFile(sFileName);
aXMLDocument.Active:= true;
aXMLNode:= aXMLDocument.ChildNodes.FindNode('conf');
if aXMLNode<> nil then
for i:= 0 to aXMLNode.ChildNodes.Count-1 do
begin
if aXMLNode.ChildNodes.NodeName = 'topic' then
if iChoose = 1 then
aStrings.Add(aXMLNode.ChildNodes.Attributes['cn'])
else if iChoose = 2 then
aStrings.Add(aXMLNode.ChildNodes.Attributes['name'])
else if iChoose = 3 then
begin
aStrings.Add('第'+ IntToStr(i+1)+'条 sql 语句:');
aStrings.Add(aXMLNode.ChildNodes.ChildNodes[0].NodeValue);
aStrings.Add('===========================================');
end;
end;
Result:= aStrings.Text;
finally
aStrings.Free;
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
for i:= 0 to ComponentCount-1 do
if Components is TMemo then
TMemo(Components).Lines.Clear;
end;

end.
 
后退
顶部