如何使用TXMLDocument读写带xmlns属性的XML? 本帖最后由 lumj 于 2012-01-27 20:17:57 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc, ExtCtrls;type TForm1 = class(TForm) Panel1: TPanel; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var xd1: TXMLDocument; mm,nn: IXMLNode;begin //生成 xd1 := TXMLDocument.Create(application); xd1.Active := True; xd1.Encoding := 'utf-8'; xd1.StandAlone := 'yes'; xd1.Version := '1.0'; xd1.Options := [doNodeAutoIndent]; mm := xd1.Node.AddChild('package','http://www.idpf.org/2007/opf'); mm.SetAttributeNS('unique-identifier','','BookId'); mm.SetAttributeNS('version','','2.0'); nn := mm.AddChild('metadata',mm.NamespaceURI); nn.DeclareNamespace('','http://www.idpf.org/2007/opf'); nn.DeclareNamespace('dc','http://purl.org/dc/elements/1.1/'); nn.DeclareNamespace('opf','http://www.idpf.org/2007/opf'); nn.AttributeNodes.Delete('xmlns',''); xd1.SaveToFile('d:\test1.xml'); xd1.Active := False; xd1.Destroy;end;procedure TForm1.Button2Click(Sender: TObject);var xd1: TXMLDocument; i: integer; mNode,mm: IXMLNode;begin //读取 xd1 := TXMLDocument.Create(application); xd1.FileName := 'd:\test1.xml'; xd1.Active := True; mNode := xd1.ChildNodes.FindNode('package'); showmessage(mNode.NamespaceURI); mNode := mNode.ChildNodes.FindNode('metadata'); for i:=0 to mNode.AttributeNodes.Count -1 do begin mm := mNode.AttributeNodes.Get(i); showmessage(mm.LocalName); end; showmessage(mNode.FindNamespaceURI('dc')); showmessage(mNode.FindNamespaceURI('opf')); xd1.Active := False; xd1.Destroy;end;end. 如何打开dbf文件 帮忙看一下这个sql插入值的语句 ? 不装驱动,直接写串口打印。请教如何获得打印机状态?我用的打印机是Epson300A。 excel数据导入sqlserver2000数据库的问题. 新手求救(特别急):怎样释放ADODataSet占用的内存 感谢Nyf1220!接分! 问一个向数据库提交数据的问题 关于使用GetSystemMetrics(SM_NETWORK)获取计算机联网状态的疑问 Delphi中有没有能取任意几位小数的函数, 离线下载! vcl=mfc,fmx=qt 这个是不是xe2的bug
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc, ExtCtrls;type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
xd1: TXMLDocument;
mm,nn: IXMLNode;
begin
//生成
xd1 := TXMLDocument.Create(application);
xd1.Active := True;
xd1.Encoding := 'utf-8';
xd1.StandAlone := 'yes';
xd1.Version := '1.0';
xd1.Options := [doNodeAutoIndent];
mm := xd1.Node.AddChild('package','http://www.idpf.org/2007/opf');
mm.SetAttributeNS('unique-identifier','','BookId');
mm.SetAttributeNS('version','','2.0'); nn := mm.AddChild('metadata',mm.NamespaceURI);
nn.DeclareNamespace('','http://www.idpf.org/2007/opf');
nn.DeclareNamespace('dc','http://purl.org/dc/elements/1.1/');
nn.DeclareNamespace('opf','http://www.idpf.org/2007/opf');
nn.AttributeNodes.Delete('xmlns',''); xd1.SaveToFile('d:\test1.xml');
xd1.Active := False;
xd1.Destroy;
end;procedure TForm1.Button2Click(Sender: TObject);
var
xd1: TXMLDocument;
i: integer;
mNode,mm: IXMLNode;
begin
//读取
xd1 := TXMLDocument.Create(application);
xd1.FileName := 'd:\test1.xml';
xd1.Active := True;
mNode := xd1.ChildNodes.FindNode('package');
showmessage(mNode.NamespaceURI);
mNode := mNode.ChildNodes.FindNode('metadata');
for i:=0 to mNode.AttributeNodes.Count -1 do
begin
mm := mNode.AttributeNodes.Get(i);
showmessage(mm.LocalName);
end;
showmessage(mNode.FindNamespaceURI('dc'));
showmessage(mNode.FindNamespaceURI('opf'));
xd1.Active := False;
xd1.Destroy;end;end.