type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
uses NativeXml; {$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); var xml: TNativeXml; Node: TXmlNode; ss: TStringStream; i: Integer; begin try ss := TStringStream.Create('', TEncoding.UTF8); ss.WriteString('<?xml version="1.0" encoding="UTF-8"?><error></error><page rowcount=''2'' colcount=''3''><row><d>430000001</d><d>A类商品</d><d>10</d></row><row><d>430000002</d><d>B类商品</d><d>10</d></row></page>'); ss.WriteString ('<?xml version="1.0" encoding="UTF-8"?><error></error><page rowcount=''2'' colcount=''15''><row><d>字段1</d><d>字段2</d><d>字段3</d><d>字段4</d></row>><row><d>字段1</d><d>字段2</d><d>字段3</d><d>字段4</d></row>><row><d>字段1</d><d>字段2</d><d>字段3</d><d>字段4</d></row></page>'); ss.Position := 0; xml := TNativeXml.Create(nil); xml.LoadFromStream(ss); Node := xml.FindFirst; while assigned(Node) do begin if Node.Name = 'row' then begin for i := 0 to Node.ElementCount - 1 do begin if Node.Elements[i].Name = 'd' then Memo1.Lines.Add(format('%s%d=%s', [Node.Elements[i].Name, i + 1, Node.Elements[i].Value])); end; end; Node := xml.FindNext(Node); end; finally ss.Free; xml.Free; end; end;