XML心得
aNode := NodeNew('input');
aNode.Value := ' '; //是个空格字符,要不然 这个节点不能显示 = <input/>
var
aDOC, xml: TNativeXml;
aNode,aNode1: TXmlNode;
declaration: TsdDeclaration;
begin
aDOC := TNativeXml.CreateName('business');
declaration := aDOC.RootNodes[0] as TsdDeclaration;
declaration.Encoding := 'gbk';
aDOC.XmlFormat := xfReadable;
aNode := aDOC.Root;
aNode.AttributeAdd('comment', '发票打印设置');
aNode.AttributeAdd('id', 'FP_DYSZ');
aNode1 := aNode.NodeNew('body');
with aNode1 do
begin
AttributeAdd('yylxdm', '1');
with NodeNew('input') do
begin
// WriteString('','');
// NodeNew('kpzdbs').Value := '开票终端标识';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aDoc: TNativeXml;
aXmlNode: TXmlNode;
sList: TList;
i, j, k, l: Integer;
ainput: TXmlNode;
begin
try
sList := TList.Create;
aDoc := TNativeXml.Create(Application);
aDoc.ExternalEncoding := seUTF8;
//or
aDoc.Charset := 'gbk';
aDoc.XmlFormat := xfReadable;
aDoc.ReadFromString(AnsiToUtf8(Memo1.Text));
for i := 0 to aDoc.Root.NodeCount - 1 do
begin
Memo2.Lines.Add(IntToStr(i) + ' ' + aDoc.Root.Nodes[i].Name);
for j := 0 to aDoc.Root.Nodes[i].NodeCount - 1 do
begin
Memo2.Lines.Add(IntToStr(i) + IntToStr(j) + ' ' + aDoc.Root.Nodes[i].Nodes[j].Name);
for k := 0 to aDoc.Root.Nodes[i].Nodes[j].NodeCount - 1 do
begin
Memo2.Lines.Add(IntToStr(i) + IntToStr(j) + IntToStr(k) + ' ' + aDoc.Root.Nodes[i].Nodes[j].Nodes[k].Name);
end;
end;
end;
Memo2.Lines.Add('====================================');
aXmlNode := aDoc.RootNodes.Items[1];
for i := 0 to aXmlNode.NodeCount - 1 do
begin
Memo2.Lines.Add(aXmlNode.Nodes[i].Name);
end;
Memo2.Lines.Add('====================================');
Memo2.Lines.Add(aXmlNode.FindNode('body').Name + ' :' + IntToStr(aXmlNode.FindNode('body').NodeCount));
ainput := aXmlNode.FindNode('body').FindNode('output');
Memo2.Lines.Add('====================================');
Memo2.Lines.Add(Utf8ToAnsi(ainput.FindNode('nsrsbh').Name) + ' : ' + Utf8ToAnsi(ainput.FindNode('nsrsbh').Value));
Memo2.Lines.Add('====================================');
finally
FreeAndNil(aDoc);
FreeAndNil(sList);
end;
end;