L
ljf945
Unregistered / Unconfirmed
GUEST, unregistred user!
用delphi7合并两个xml文件的结点,为什么可以遍历到新加的结点而输出却不对??
注: DomVendor属性是 Xerces XML ,是不能改动的。
文件及用到的xml文件如下:
-----------------------------------------
program Project1;
uses
QForms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
-------------------------------------------
unit Unit1;
interface
uses
SysUtils, Types, Classes, Variants, QTypes, QGraphics, QControls, QForms,
QDialogs, QStdCtrls, QButtons, xmldom, XMLIntf, xercesxmldom, XMLDoc,
oxmldom;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
OpenDialog1: TOpenDialog;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
Button1: TButton;
Button2: TButton;
Button3: TButton;
XMLDocument1: TXMLDocument;
XMLDocument2: TXMLDocument;
Memo1: TMemo;
Button4: TButton;
ComboBox1: TComboBox;
SpeedButton4: TSpeedButton;
procedure SpeedButton1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.xfm}
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
if (Sender as TSpeedButton).Name='SpeedButton1' then
Edit1.Text:=OpenDialog1.FileName
else if (Sender as TSpeedButton).Name='SpeedButton2' then
Edit2.Text:=OpenDialog1.FileName
else
Edit3.Text:=OpenDialog1.FileName
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.Close;
end;
procedure MyNodeMerge(sour:IXMLNodeList; dest:IXMLNodeList);
var
i,j,k,l,m:integer;
HaveIt,HaveThisAttr:integer; //1 means find it in dest,need nest.
begin // and sour to dest
for i:=0 to sour.Count-1 do
begin
HaveIt:=0;
for j:=0 to dest.Count-1 do
begin
//showmessage( sour.NodeName+' dd '+dest[j].NodeName);
if (sour.NodeName=dest[j].NodeName) then
begin //find it //limit nodetype can be here
HaveIt:=1;
break;
end;
end;
if ((HaveIt=1) and (pos('#',sour.NodeName)<>1)) then
begin //有些接点读出来是以 #打头的,如 #text,但在xml文件内又看不到这种接点,所以替换的时候全部屏蔽掉了。
//update attr
for k:=0 to sour.AttributeNodes.Count-1 do
begin
HaveThisAttr:=0;
for l:=0 to dest[j].AttributeNodes.Count-1 do
begin
if sour.AttributeNodes[k].NodeName=dest[j].AttributeNodes[l].NodeName then
begin
HaveThisAttr:=1;
break;
end;
end;
if HaveThisAttr=1 then
dest[j].AttributeNodes[l].NodeValue:=sour.AttributeNodes[k].NodeValue
else
begin //not exist so add a new node
dest[j].SetAttribute(sour.AttributeNodes[k].NodeName,sour.AttributeNodes[k].NodeValue);
end;
end;
//end update attr
if ((sour.HasChildNodes) and (dest[j].HasChildNodes)) then
begin
//ShowMessage(sour.NodeName+' run '+dest[j].NodeName);
MyNodeMerge(sour.ChildNodes,dest[j].ChildNodes)
end
else
begin
dest.Add(sour);
end;
end
else
begin
if (pos('#',sour.NodeName)<>1) then
begin
showmessage(sour.NodeName+' s+d '+dest[j-1].nodename);
// for m:=0 to dest.Count-1 do
// showmessage('before: '+dest[m].NodeName);
dest.BeginUpdate;
dest.Delete('shishu');
dest.Add(sour);
dest.Add(sour[i+1]);
dest.EndUpdate;
// for m:=0 to dest.Count-1 do
// showmessage('after: '+dest[m].NodeName);
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
flag:integer;
begin
flag:=0;
if trim(edit1.Text)=trim(edit2.text) then
begin
ShowMessage('更新文件和被更新文件相同!');
flag:=1;
Edit2.SetFocus;
end;
//open XMLDocument1 & 2 here
if flag=0 then
begin //add XMLDocument1 to modify XMLDocument2
//for i:=0 to XMLDocument1.ChildNodes.Count-1 do
// ShowMessage(' child '+XMLDocument1.ChildNodes.nodename);
MyNodeMerge(XMLDocument1.ChildNodes,XMLDocument2.ChildNodes);
XMLDocument2.SaveToFile(edit3.text);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
procedure shownode(nd:IXMLnode);
var
i,j:integer;
begin
for j:=0 to nd.AttributeNodes.Count-1 do
begin
//nd.AttributeNodes.Nodes[j].Nodevalue:='dddf';
showmessage('attr: '+nd.AttributeNodes.Nodes[j].NodeName+nd.AttributeNodes.Nodes[j].Nodevalue);
end;
if nd.HasChildNodes then
begin
if nd.NodeType=ntText then
ShowMessage(nd.NodeName+'%%node value:'+nd.nodevalue+' text:'+nd.Text)
else
begin
ShowMessage('haschilds+nodename: '+nd.NodeName);
if nd.NodeName='Site' then
begin
for i:=0 to nd.ChildNodes.Count-1 do
ShowMessage('site_child '+nd.ChildNodes.NodeName);
end;
end;
for i:=0 to nd.ChildNodes.Count-1 do
ShowNode(nd.ChildNodes)
end
else
begin
if ((nd.NodeType=ntText) and (trim(nd.NodeName)<>'#text')) then
ShowMessage(' nochild+ value:'+nd.NodeName+' '+nd.nodevalue)
else showmessage('nochild+text '+nd.NodeName);
end;
end;
begin
if ComboBox1.ItemIndex=0 then
shownode(XMLDocument1.Node)
else
shownode(XMLDocument2.Node);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if ComboBox1.ItemIndex=0 then
begin
memo1.Lines.add('----111----------add xml 111------- ');
memo1.Lines.AddStrings(XMLDocument1.XML)
end
else
begin
memo1.Lines.add('----222----------add xml 222------- ');
memo1.lines.AddStrings(XMLDocument2.XML);
end;
end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
XMLDocument2.ChildNodes.Clear;
memo1.Lines.Clear;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if Combobox1.ItemIndex=0 then
begin
button3.Caption:='遍历 1';
button4.Caption:='显示 1';
end
else
begin
button3.Caption:='遍历 2';
button4.Caption:='显示 2';
end;
end;
end.
------------------------------------------------------------------
object Form1: TForm1
Left = 180
Top = 171
Width = 722
Height = 554
VertScrollBar.Range = 545
HorzScrollBar.Range = 713
ActiveControl = Edit1
Caption = 'Form1'
Color = clBackground
Position = poScreenCenter
WindowState = wsMinimized
PixelsPerInch = 81
TextHeight = 12
TextWidth = 6
object SpeedButton1: TSpeedButton
Left = 400
Top = 16
Width = 23
Height = 22
Caption = '..'
OnClick = SpeedButton1Click
end
object SpeedButton2: TSpeedButton
Left = 400
Top = 56
Width = 23
Height = 22
Caption = '..'
OnClick = SpeedButton1Click
end
object SpeedButton3: TSpeedButton
Left = 400
Top = 96
Width = 23
Height = 22
Caption = '..'
OnClick = SpeedButton1Click
end
object SpeedButton4: TSpeedButton
Left = 632
Top = 187
Width = 63
Height = 22
Caption = 'clear'
OnClick = SpeedButton4Click
end
object Label1: TLabel
Left = 16
Top = 24
Width = 48
Height = 12
Caption = '更新文件'
end
object Edit1: TEdit
Left = 94
Top = 16
Width = 305
Height = 20
TabOrder = 1
Text = 'a.xml'
end
object Label2: TLabel
Left = 16
Top = 64
Width = 60
Height = 12
Caption = '被更新文件'
end
object Edit2: TEdit
Left = 94
Top = 56
Width = 305
Height = 20
TabOrder = 3
Text = 'b.xml'
end
object Label3: TLabel
Left = 16
Top = 104
Width = 60
Height = 12
Caption = '更新后文件'
end
object Edit3: TEdit
Left = 94
Top = 96
Width = 305
Height = 20
TabOrder = 5
Text = 'c.xml'
end
object Button1: TButton
Left = 352
Top = 136
Width = 75
Height = 25
Caption = '转 化'
TabOrder = 6
OnClick = Button1Click
end
object Button2: TButton
Left = 352
Top = 176
Width = 75
Height = 25
Caption = '退 出'
TabOrder = 7
OnClick = Button2Click
end
object Button3: TButton
Left = 232
Top = 176
Width = 75
Height = 25
Caption = '遍历 2'
TabOrder = 8
OnClick = Button3Click
end
object Memo1: TMemo
Left = 8
Top = 208
Width = 705
Height = 337
ScrollBars = ssAutoBoth
TabOrder = 9
WordWrap = False
end
object Button4: TButton
Left = 112
Top = 176
Width = 97
Height = 25
Caption = '显示 2'
TabOrder = 10
OnClick = Button4Click
end
object ComboBox1: TComboBox
Left = 8
Top = 176
Width = 105
Height = 20
Style = csOwnerDrawFixed
ItemHeight = 14
Items.Strings = (
'1'
'2')
TabOrder = 11
OnChange = ComboBox1Change
end
object OpenDialog1: TOpenDialog
FilterIndex = 0
Height = 0
Width = 0
Left = 248
Top = 136
end
object XMLDocument1: TXMLDocument
Active = True
FileName = 'a.xml'
Left = 280
Top = 136
DOMVendorDesc = 'Xerces XML'
end
object XMLDocument2: TXMLDocument
Active = True
FileName = 'b.xml'
Left = 312
Top = 136
DOMVendorDesc = 'Xerces XML'
end
end
-------------另存为a.xml--------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 U (http://www.xmlspy.com) by donquixote (topMaster) -->
<!--璇存槑淇℃伅-->
<Site IP="%%IaaP%%" username="%%username%%" version="2.0" siteID="%%siteID%%" sitsseID="%%sitggeID%%">
<license>
<daxuesheng>
<MAXCONNECT>%%MAXCONNECT%%</MAXCONNECT>
<starttime>%%starttime%%</starttime>
<endtime>%%endTime%%</endtime>
</daxuesheng>
</license>
<shishu>
<MAXCONNECT>%%MAXCONNECT%%</MAXCONNECT>
<starttime>%%starttime%%</starttime>
<endtime>%%endTime%%</endtime>
</shishu>
</Site>
--------------另存为 b.xml---------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 U (http://www.xmlspy.com) by donquixote (topMaster) --><!--璇存槑淇℃伅--><Site IP="%%IbbP%%" siteID="%%siteID%%" version="2.0" username="%%username%%">
<license>
<shishu>
<MAXCONNECT>%%MAXCONNECT%%</MAXCONNECT>
<starttime>%%starttime%%</starttime>
<endtime>%%endTime%%</endtime>
</shishu>
</license>
</Site>
对任何关注和帮助,表示感谢。
注: DomVendor属性是 Xerces XML ,是不能改动的。
文件及用到的xml文件如下:
-----------------------------------------
program Project1;
uses
QForms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
-------------------------------------------
unit Unit1;
interface
uses
SysUtils, Types, Classes, Variants, QTypes, QGraphics, QControls, QForms,
QDialogs, QStdCtrls, QButtons, xmldom, XMLIntf, xercesxmldom, XMLDoc,
oxmldom;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
OpenDialog1: TOpenDialog;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
Button1: TButton;
Button2: TButton;
Button3: TButton;
XMLDocument1: TXMLDocument;
XMLDocument2: TXMLDocument;
Memo1: TMemo;
Button4: TButton;
ComboBox1: TComboBox;
SpeedButton4: TSpeedButton;
procedure SpeedButton1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.xfm}
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
if (Sender as TSpeedButton).Name='SpeedButton1' then
Edit1.Text:=OpenDialog1.FileName
else if (Sender as TSpeedButton).Name='SpeedButton2' then
Edit2.Text:=OpenDialog1.FileName
else
Edit3.Text:=OpenDialog1.FileName
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.Close;
end;
procedure MyNodeMerge(sour:IXMLNodeList; dest:IXMLNodeList);
var
i,j,k,l,m:integer;
HaveIt,HaveThisAttr:integer; //1 means find it in dest,need nest.
begin // and sour to dest
for i:=0 to sour.Count-1 do
begin
HaveIt:=0;
for j:=0 to dest.Count-1 do
begin
//showmessage( sour.NodeName+' dd '+dest[j].NodeName);
if (sour.NodeName=dest[j].NodeName) then
begin //find it //limit nodetype can be here
HaveIt:=1;
break;
end;
end;
if ((HaveIt=1) and (pos('#',sour.NodeName)<>1)) then
begin //有些接点读出来是以 #打头的,如 #text,但在xml文件内又看不到这种接点,所以替换的时候全部屏蔽掉了。
//update attr
for k:=0 to sour.AttributeNodes.Count-1 do
begin
HaveThisAttr:=0;
for l:=0 to dest[j].AttributeNodes.Count-1 do
begin
if sour.AttributeNodes[k].NodeName=dest[j].AttributeNodes[l].NodeName then
begin
HaveThisAttr:=1;
break;
end;
end;
if HaveThisAttr=1 then
dest[j].AttributeNodes[l].NodeValue:=sour.AttributeNodes[k].NodeValue
else
begin //not exist so add a new node
dest[j].SetAttribute(sour.AttributeNodes[k].NodeName,sour.AttributeNodes[k].NodeValue);
end;
end;
//end update attr
if ((sour.HasChildNodes) and (dest[j].HasChildNodes)) then
begin
//ShowMessage(sour.NodeName+' run '+dest[j].NodeName);
MyNodeMerge(sour.ChildNodes,dest[j].ChildNodes)
end
else
begin
dest.Add(sour);
end;
end
else
begin
if (pos('#',sour.NodeName)<>1) then
begin
showmessage(sour.NodeName+' s+d '+dest[j-1].nodename);
// for m:=0 to dest.Count-1 do
// showmessage('before: '+dest[m].NodeName);
dest.BeginUpdate;
dest.Delete('shishu');
dest.Add(sour);
dest.Add(sour[i+1]);
dest.EndUpdate;
// for m:=0 to dest.Count-1 do
// showmessage('after: '+dest[m].NodeName);
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
flag:integer;
begin
flag:=0;
if trim(edit1.Text)=trim(edit2.text) then
begin
ShowMessage('更新文件和被更新文件相同!');
flag:=1;
Edit2.SetFocus;
end;
//open XMLDocument1 & 2 here
if flag=0 then
begin //add XMLDocument1 to modify XMLDocument2
//for i:=0 to XMLDocument1.ChildNodes.Count-1 do
// ShowMessage(' child '+XMLDocument1.ChildNodes.nodename);
MyNodeMerge(XMLDocument1.ChildNodes,XMLDocument2.ChildNodes);
XMLDocument2.SaveToFile(edit3.text);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
procedure shownode(nd:IXMLnode);
var
i,j:integer;
begin
for j:=0 to nd.AttributeNodes.Count-1 do
begin
//nd.AttributeNodes.Nodes[j].Nodevalue:='dddf';
showmessage('attr: '+nd.AttributeNodes.Nodes[j].NodeName+nd.AttributeNodes.Nodes[j].Nodevalue);
end;
if nd.HasChildNodes then
begin
if nd.NodeType=ntText then
ShowMessage(nd.NodeName+'%%node value:'+nd.nodevalue+' text:'+nd.Text)
else
begin
ShowMessage('haschilds+nodename: '+nd.NodeName);
if nd.NodeName='Site' then
begin
for i:=0 to nd.ChildNodes.Count-1 do
ShowMessage('site_child '+nd.ChildNodes.NodeName);
end;
end;
for i:=0 to nd.ChildNodes.Count-1 do
ShowNode(nd.ChildNodes)
end
else
begin
if ((nd.NodeType=ntText) and (trim(nd.NodeName)<>'#text')) then
ShowMessage(' nochild+ value:'+nd.NodeName+' '+nd.nodevalue)
else showmessage('nochild+text '+nd.NodeName);
end;
end;
begin
if ComboBox1.ItemIndex=0 then
shownode(XMLDocument1.Node)
else
shownode(XMLDocument2.Node);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if ComboBox1.ItemIndex=0 then
begin
memo1.Lines.add('----111----------add xml 111------- ');
memo1.Lines.AddStrings(XMLDocument1.XML)
end
else
begin
memo1.Lines.add('----222----------add xml 222------- ');
memo1.lines.AddStrings(XMLDocument2.XML);
end;
end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
XMLDocument2.ChildNodes.Clear;
memo1.Lines.Clear;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if Combobox1.ItemIndex=0 then
begin
button3.Caption:='遍历 1';
button4.Caption:='显示 1';
end
else
begin
button3.Caption:='遍历 2';
button4.Caption:='显示 2';
end;
end;
end.
------------------------------------------------------------------
object Form1: TForm1
Left = 180
Top = 171
Width = 722
Height = 554
VertScrollBar.Range = 545
HorzScrollBar.Range = 713
ActiveControl = Edit1
Caption = 'Form1'
Color = clBackground
Position = poScreenCenter
WindowState = wsMinimized
PixelsPerInch = 81
TextHeight = 12
TextWidth = 6
object SpeedButton1: TSpeedButton
Left = 400
Top = 16
Width = 23
Height = 22
Caption = '..'
OnClick = SpeedButton1Click
end
object SpeedButton2: TSpeedButton
Left = 400
Top = 56
Width = 23
Height = 22
Caption = '..'
OnClick = SpeedButton1Click
end
object SpeedButton3: TSpeedButton
Left = 400
Top = 96
Width = 23
Height = 22
Caption = '..'
OnClick = SpeedButton1Click
end
object SpeedButton4: TSpeedButton
Left = 632
Top = 187
Width = 63
Height = 22
Caption = 'clear'
OnClick = SpeedButton4Click
end
object Label1: TLabel
Left = 16
Top = 24
Width = 48
Height = 12
Caption = '更新文件'
end
object Edit1: TEdit
Left = 94
Top = 16
Width = 305
Height = 20
TabOrder = 1
Text = 'a.xml'
end
object Label2: TLabel
Left = 16
Top = 64
Width = 60
Height = 12
Caption = '被更新文件'
end
object Edit2: TEdit
Left = 94
Top = 56
Width = 305
Height = 20
TabOrder = 3
Text = 'b.xml'
end
object Label3: TLabel
Left = 16
Top = 104
Width = 60
Height = 12
Caption = '更新后文件'
end
object Edit3: TEdit
Left = 94
Top = 96
Width = 305
Height = 20
TabOrder = 5
Text = 'c.xml'
end
object Button1: TButton
Left = 352
Top = 136
Width = 75
Height = 25
Caption = '转 化'
TabOrder = 6
OnClick = Button1Click
end
object Button2: TButton
Left = 352
Top = 176
Width = 75
Height = 25
Caption = '退 出'
TabOrder = 7
OnClick = Button2Click
end
object Button3: TButton
Left = 232
Top = 176
Width = 75
Height = 25
Caption = '遍历 2'
TabOrder = 8
OnClick = Button3Click
end
object Memo1: TMemo
Left = 8
Top = 208
Width = 705
Height = 337
ScrollBars = ssAutoBoth
TabOrder = 9
WordWrap = False
end
object Button4: TButton
Left = 112
Top = 176
Width = 97
Height = 25
Caption = '显示 2'
TabOrder = 10
OnClick = Button4Click
end
object ComboBox1: TComboBox
Left = 8
Top = 176
Width = 105
Height = 20
Style = csOwnerDrawFixed
ItemHeight = 14
Items.Strings = (
'1'
'2')
TabOrder = 11
OnChange = ComboBox1Change
end
object OpenDialog1: TOpenDialog
FilterIndex = 0
Height = 0
Width = 0
Left = 248
Top = 136
end
object XMLDocument1: TXMLDocument
Active = True
FileName = 'a.xml'
Left = 280
Top = 136
DOMVendorDesc = 'Xerces XML'
end
object XMLDocument2: TXMLDocument
Active = True
FileName = 'b.xml'
Left = 312
Top = 136
DOMVendorDesc = 'Xerces XML'
end
end
-------------另存为a.xml--------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 U (http://www.xmlspy.com) by donquixote (topMaster) -->
<!--璇存槑淇℃伅-->
<Site IP="%%IaaP%%" username="%%username%%" version="2.0" siteID="%%siteID%%" sitsseID="%%sitggeID%%">
<license>
<daxuesheng>
<MAXCONNECT>%%MAXCONNECT%%</MAXCONNECT>
<starttime>%%starttime%%</starttime>
<endtime>%%endTime%%</endtime>
</daxuesheng>
</license>
<shishu>
<MAXCONNECT>%%MAXCONNECT%%</MAXCONNECT>
<starttime>%%starttime%%</starttime>
<endtime>%%endTime%%</endtime>
</shishu>
</Site>
--------------另存为 b.xml---------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 U (http://www.xmlspy.com) by donquixote (topMaster) --><!--璇存槑淇℃伅--><Site IP="%%IbbP%%" siteID="%%siteID%%" version="2.0" username="%%username%%">
<license>
<shishu>
<MAXCONNECT>%%MAXCONNECT%%</MAXCONNECT>
<starttime>%%starttime%%</starttime>
<endtime>%%endTime%%</endtime>
</shishu>
</license>
</Site>
对任何关注和帮助,表示感谢。