[在线等]如何提取XML文件中的字段的值?[300分求助](300)

  • 主题发起人 主题发起人 kypansky
  • 开始时间 开始时间
K

kypansky

Unregistered / Unconfirmed
GUEST, unregistred user!
如何提取XML文件的字段的值?如:下面这段代码中,我要取Addr字段,FromAddr字段的值,如何读取XML并处理?最好有代码.<?xml version="1.0" encoding="GB2312" ?><MSG_MSG_SEND_REQ> <TranID>27</TranID> <RetRec>0</RetRec> <MsgList> <MSG_STRUCT> <SID>27301</SID> <SN>1</SN> <Channel>CHANNEL_EMAIL</Channel> <AddrList> <ADDR_STRUCT> <AddrType>AT_EMAILTO</AddrType> <ClientID></ClientID> <Addr>test@test.com</Addr> </ADDR_STRUCT> <ADDR_STRUCT> <AddrType>AT_EMAILTO</AddrType> <ClientID></ClientID> <Addr>test2@test2.com</Addr> </ADDR_STRUCT> </AddrList> <BusinessType></BusinessType> <Priority>PRIO_NORMAL</Priority> <DeliverWay>DELIVERY_INSTANT</DeliverWay> <POTList></POTList> <LifeTime>0</LifeTime> <BodyFormat>FORMAT_TEXT</BodyFormat> <AtcIDList></AtcIDList> <FromAddr>userid@message.cmbchina.com</FromAddr> <EmailSubject>email test</EmailSubject> <BBody>aGVsbG8gZW1haWwu</BBody> <ReceiptAddr></ReceiptAddr> <ReplyAddr></ReplyAddr> <SubCode></SubCode> </MSG_STRUCT> </MsgList></MSG_MSG_SEND_REQ>请给详细代码.300分求助,在线等.
 
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc;type TForm1 = class(TForm) XMLDocument1: TXMLDocument; Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var RootNode, MsgListNode, MSG_STRUCT_node, AddrList_node, ADDR_STRUCT_node: IXMLNode; sFromAddr, sAddr: string;begin RootNode := XMLDocument1.DocumentElement; MsgListNode := RootNode.ChildNodes.FindNode('MsgList'); MSG_STRUCT_node := MsgListNode.ChildNodes.First; AddrList_node := MSG_STRUCT_node.ChildNodes.FindNode('AddrList'); sFromAddr := MSG_STRUCT_node.ChildNodes.FindNode('FromAddr').Text; ADDR_STRUCT_node := AddrList_node.ChildNodes.First; while ADDR_STRUCT_node <> nil do begin sAddr := sAddr + #10 + ADDR_STRUCT_node.ChildNodes.FindNode('Addr').Text; ADDR_STRUCT_node := ADDR_STRUCT_node.NextSibling; end; ShowMessage(sAddr + #10 +sfromaddr);end;procedure TForm1.FormCreate(Sender: TObject);begin XMLDocument1.XML.Text := '<?xml version="1.0" encoding="GB2312" ?>' + '<MSG_MSG_SEND_REQ>' + '<TranID>27</TranID>' + '<RetRec>0</RetRec>' + '<MsgList>' + '<MSG_STRUCT>' + '<SID>27301</SID>' + '<SN>1</SN>' + '<Channel>CHANNEL_EMAIL</Channel>' + '<AddrList>' + '<ADDR_STRUCT>' + '<AddrType>AT_EMAILTO</AddrType>' + '<ClientID></ClientID>' + '<Addr>test@test.com</Addr>' + '</ADDR_STRUCT>' + '<ADDR_STRUCT>' + '<AddrType>AT_EMAILTO</AddrType>' + '<ClientID></ClientID>' + '<Addr>test2@test2.com</Addr>' + '</ADDR_STRUCT>' + '</AddrList>' + '<BusinessType></BusinessType>' + '<Priority>PRIO_NORMAL</Priority>' + '<DeliverWay>DELIVERY_INSTANT</DeliverWay>' + '<POTList></POTList>' + '<LifeTime>0</LifeTime>' + '<BodyFormat>FORMAT_TEXT</BodyFormat>' + '<AtcIDList></AtcIDList>' + '<FromAddr>userid@message.cmbchina.com</FromAddr>' + '<EmailSubject>email test</EmailSubject>' + '<BBody>aGVsbG8gZW1haWwu</BBody>' + '<ReceiptAddr></ReceiptAddr>' + '<ReplyAddr></ReplyAddr>' + '<SubCode></SubCode>' + '</MSG_STRUCT>' + '</MsgList>' + '</MSG_MSG_SEND_REQ>'; XMLDocument1.Active := True;end;end.
 
delphi7递归实现读XMl结点的函数http://www.delphibbs.com/delphibbs/dispq.asp?lid=3958083
 
//打开一个存在的Filename XML文档procedure TXMLOption.OpenXml(Filename: string);begin if not Assigned(FXMLDoc) then begin FXMLDoc := CoDOMDocument.Create; if FXMLDoc.Load(Filename) then FActive:=true else FActive:=false; if FActive then FFilename:=Filename else FFilename:=''; end;end;//查找节点值function TXMLOption.FindNodes(userid:widestring):boolean;var xuser:IXMLDOMNode; xpath:string;begin result:=false; if not FActive then exit; xpath:=UsersTag+'['+U_Id+'="'+userid+'"]'; xuser:=FXMLDoc.documentElement.selectSingleNode(xpath); if xuser<>nil then result:=true;end;
 
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, XMLIntf, XMLDoc, StdCtrls; type TStudent = Class(TObject) public Name: string; Sex: string; Age: Integer; Phone: string; end; TForm1 = class(TForm) Button1: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } FXMLDoc: IXMLDocument; // XML文档 FDocElement: IXMLNode; // 根节点 FList: TList; public { Public declarations } end; var Form1: TForm1; implementation uses Student; {$R *.dfm} const XMLString = '<?xml version="1.0" encoding="GB2312"?>' + '<Students>' + ' <Student Sex = "男">' + ' <Name>文文</Name>' + ' <Age>18</Age>' + ' <Phone>4745723</Phone>' + ' </Student>' + ' <Student Sex = "女">' + ' <Name>喃喃</Name>' + ' <Age>17</Age>' + ' <Phone>4644563</Phone>' + ' </Student>' + '</Students>'; procedure TForm1.FormCreate(Sender: TObject); begin FXMLDoc := TXMLDocument.Create(nil); FXMLDoc.XML.Text := XMLString; FXMLDoc.Active := True; FList := TList.Create; FDocElement := FXMLDoc.DocumentElement; end; procedure TForm1.FormDestroy(Sender: TObject); var i: Integer; begin FXMLDoc.Active := False; for i := 0 to Flist.Count - 1 do TStudent(FList.Items).Free; FList.Free; end; procedure TForm1.Button1Click(Sender: TObject); var i: Integer; Stu: TStudent; StuNode: IXMLNode; begin for i := 0 to FDocElement.ChildNodes.Count - 1 do begin StuNode := FDocElement.ChildNodes; Stu := TStudent.Create; Stu.Name := StuNode.ChildValues['Name']; Stu.Sex := StuNode.Attributes['Sex']; Stu.Age := StuNode.ChildValues['Age']; Stu.Phone := StuNode.ChildValues['Phone']; FList.Add(Stu); end; end; end.
 
使用xpath直接取得
 
推荐使用nativeXml
 
如果你确信 XML 文档足够简单,而且准确性要求并不是很苛刻,可以把 XML文件当作文本文件来处理uses StrUtils;const cFile = 'c:/12.xml'; cSection = 'FromAddr';procedure TForm1.Button1Click(Sender: TObject);const cBegin = '<' + cSection + '>'; cEnd = '</' + cSection + '>'; cLenBegin = Length(cBegin);var s: TStrings; iBegin,iEnd: Integer;begin s := TStringList.Create; try s.LoadFromFile(cFile); iBegin := Pos(cBegin,s.Text); if iBegin <= 0 then Exit; iEnd := PosEx(cEnd,s.Text,iBegin); if iEnd <= iBegin then Exit; iBegin := iBegin + cLenBegin; ShowMessage(Copy(s.Text,iBegin,iEnd - iBegin)); finally s.Free; end;end;如果不是,那么上面的兄弟们已经提供的足够的解决方法了。
 
后退
顶部