本帖最后由 lumj 于 2012-01-27 20:17:57 编辑

解决方案 »

  1.   


    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.