• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi操作XML(三)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

八、属性、注释以及内容的添加

添加属性:

 node := xml.Root.NodeNew('item');

node.AttributeAdd('ID', '1');

node.AttributeAdd('age', '11');

node.Value := '张三';

node := xml.NodeNewTextAttr('item', '张三', [xml.AttrText('ID', '1'), xml.AttrInt('age', 11)]);

xml.Root.NodeAdd(node);

{更详细代码,引自万一}

uses NativeXml; 
 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  xml: TNativeXml; 
  node: TXmlNode; 
begin 
  xml := TNativeXml.CreateName('List'); 
  xml.XmlFormat := xfReadable; 
 
  node := xml.Root.NodeNew('item'); 
  node.AttributeAdd('ID', '1'); 
  node.AttributeAdd('age', '11'); 
  node.Value := '张三'; 
 
  with xml.Root.NodeNew('item') do 
  begin 
    AttributeAdd('ID', '2'); 
    AttributeAdd('age', '22'); 
    Value := '李四'; 
  end; 
 
  with xml.Root.NodeNew('item') do 
  begin 
    AttributeAdd('ID', '3'); 
    AttributeAdd('age', '33'); 
    Value := '王五'; 
  end; 
 
  Memo1.Text := xml.WriteToString; 
  xml.Free; 
end; 
{*************************************** 
<?xml version="1.0" encoding="UTF-8"?> 
<List> 
	<item ID="1" age="11">张三</item> 
	<item ID="2" age="22">李四</item> 
	<item ID="3" age="33">王五</item> 
</List> 
*****************************************} 
 
procedure TForm1.Button2Click(Sender: TObject); 
var 
  xml: TNativeXml; 
  node: TXmlNode; 
  attr1,attr2: TsdAttribute; 
begin 
  xml := TNativeXml.CreateName('List'); 
  xml.XmlFormat := xfReadable; 
 
  attr1 := TsdAttribute.Create(xml); 
  attr1.Name := 'ID'; 
  attr1.Value := '1'; 
  attr2 := TsdAttribute.Create(xml); 
  attr2.Name := 'age'; 
  attr2.ValueAsInteger := 11; 
  node := xml.Root.NodeNew('item'); 
  node.AttributeAdd(attr1); 
  node.AttributeAdd(attr2); 
  node.Value := '张三'; 
 
  attr1 := TsdAttribute.CreateName(xml, 'ID'); 
  attr1.Value := '2'; 
  attr2 := TsdAttribute.CreateNameValue(xml, 'age', '22'); 
  node := xml.Root.NodeNew('item'); 
  node.AttributesAdd([attr1, attr2]); 
  node.Value := '李四'; 
 
  node := xml.Root.NodeNew('item'); 
  node.AttributesAdd([TsdAttribute.CreateNameValue(xml, 'ID', '3'), TsdAttribute.CreateNameValue(xml, 'age', '33')]); 
  node.Value := '王五'; 
 
  Memo1.Text := xml.WriteToString; 
  xml.Free; 
end; 
{结果同上} 
 
procedure TForm1.Button3Click(Sender: TObject); 
var 
  xml: TNativeXml; 
  node: TXmlNode; 
  attr1,attr2: TsdAttribute; 
begin 
  xml := TNativeXml.CreateName('List'); 
  xml.XmlFormat := xfReadable; 
 
  attr1 := xml.AttrText('ID', '1'); 
  attr2 := xml.AttrInt('age', 11); 
  node := xml.Root.NodeNew('item'); 
  node.AttributesAdd([attr1, attr2]); 
  node.Value := '张三'; 
 
  node := xml.Root.NodeNew('item'); 
  node.AttributesAdd([xml.AttrText('ID', '2'), xml.AttrInt('age', 22)]); 
  node.Value := '李四'; 
 
  with xml.Root.NodeNew('item') do begin 
    AttributesAdd([xml.AttrText('ID', '3'), xml.AttrInt('age', 33)]); 
    Value := '王五'; 
  end; 
 
  Memo1.Text := xml.WriteToString; 
  xml.Free; 
end; 
{结果同上} 
 
procedure TForm1.Button4Click(Sender: TObject); 
var 
  xml: TNativeXml; 
  node: TXmlNode; 
begin 
  xml := TNativeXml.CreateName('List'); 
  xml.XmlFormat := xfReadable; 
 
  node := xml.NodeNewTextAttr('item', '张三', [xml.AttrText('ID', '1'), xml.AttrInt('age', 11)]); 
  xml.Root.NodeAdd(node); 
 
  node := xml.NodeNewTextAttr('item', '李四', [xml.AttrText('ID', '2'), xml.AttrInt('age', 22)]); 
  xml.Root.NodeAdd(node); 
 
  node := xml.NodeNewTextAttr('item', '王五', [xml.AttrText('ID', '3'), xml.AttrInt('age', 33)]); 
  xml.Root.NodeAdd(node); 
 
  Memo1.Text := xml.WriteToString; 
  xml.Free; 
end; 
{结果同上} 
 
procedure TForm1.Button5Click(Sender: TObject); 
var 
  xml: TNativeXml; 
begin 
  xml := TNativeXml.CreateName('List'); 
  xml.XmlFormat := xfReadable; 
 
  xml.Root.NodesAdd([ 
    xml.NodeNewTextAttr('item', '张三', [xml.AttrText('ID', '1'), xml.AttrInt('age', 11)]), 
    xml.NodeNewTextAttr('item', '李四', [xml.AttrText('ID', '2'), xml.AttrInt('age', 22)]), 
    xml.NodeNewTextAttr('item', '王五', [xml.AttrText('ID', '3'), xml.AttrInt('age', 33)]) 
  ]); 
 
  Memo1.Text := xml.WriteToString; 
  xml.Free; 
end; 
{结果同上} 
 
procedure TForm1.Button6Click(Sender: TObject); 
var 
  xml: TNativeXml; 
  node: TXmlNode; 
begin 
  xml := TNativeXml.CreateName('List'); 
  xml.XmlFormat := xfReadable; 
 
  node := xml.NodeNewAttr('item', [xml.AttrText('name', '张三'), xml.AttrInt('age', 11)]); 
  xml.Root.NodeAdd(node); 
 
  node := xml.NodeNewAttr('item', [xml.AttrText('name', '李四'), xml.AttrInt('age', 22)]); 
  xml.Root.NodeAdd(node); 
 
  node := xml.NodeNewAttr('item', [xml.AttrText('name', '王五'), xml.AttrInt('age', 33)]); 
  xml.Root.NodeAdd(node); 
 
  Memo1.Text := xml.WriteToString; 
  xml.Free; 
end; 
{*************************************** 
<?xml version="1.0" encoding="UTF-8"?> 
<List> 
	<item name="张三" age="11"/> 
	<item name="李四" age="22"/> 
	<item name="王五" age="33"/> 
</List> 
*****************************************} 
 
procedure TForm1.Button7Click(Sender: TObject); 
var 
  xml: TNativeXml; 
  node: TXmlNode; 
begin 
  xml := TNativeXml.CreateName('List'); 
  xml.XmlFormat := xfReadable; 
 
  xml.Root.NodesAdd([ 
    xml.NodeNewAttr('item', [xml.AttrText('name', '张三'), xml.AttrInt('age', 11)]), 
    xml.NodeNewAttr('item', [xml.AttrText('name', '李四'), xml.AttrInt('age', 22)]), 
    xml.NodeNewAttr('item', [xml.AttrText('name', '王五'), xml.AttrInt('age', 33)]) 
  ]); 
 
  Memo1.Text := xml.WriteToString; 
  xml.Free; 
end; 
{结果同上}

 

添加注释:

node := xml.NodeNewTextType('注释名称', '注释内容', xeComment);

xml.Root.NodeAdd(node);

添加CDATA内容

node.NodeAdd(xml.NodeNewTextType('DataName', 'XXXXXXXXXXXX', xeCData));

{更详细代码 引自万一}

uses NativeXml;

procedure TForm1.Button1Click(Sender: TObject);
var
  xml: TNativeXml;
  node: TXmlNode;
begin
  xml := TNativeXml.CreateName('List');
  xml.XmlFormat := xfReadable;

  node := xml.NodeNewTextType('注释名称', '注释内容', xeComment);
  xml.Root.NodeAdd(node);

  node := xml.NodeNewTextType('item', '张三', xeElement);
  xml.Root.NodeAdd(node);

  Memo1.Text := xml.WriteToString;
  xml.Free;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List><!--注释内容-->
	<item>张三</item>
</List>
*****************************************}

procedure TForm1.Button2Click(Sender: TObject);
var
  xml: TNativeXml;
  node: TXmlNode;
begin
  xml := TNativeXml.CreateName('List');
  xml.XmlFormat := xfReadable;

  node := xml.NodeNewTextType('item', '张三', xeElement, [xml.AttrText('ID', '1'), xml.AttrInt('age', 11)]);
  xml.Root.NodeAdd(node);

  Memo1.Text := xml.WriteToString;
  xml.Free;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
	<item ID="1" age="11">张三</item>
</List>
*****************************************}

procedure TForm1.Button3Click(Sender: TObject);
var
  xml: TNativeXml;
  node: TXmlNode;
begin
  xml := TNativeXml.CreateName('List');
  xml.XmlFormat := xfReadable;

  node := xml.NodeNewTextTypeAttr('item', '', xeElement, [xml.AttrText('ID', '1')], [xml.NodeNewText('name', '张三'), xml.NodeNewInt('age', 11)]);
  xml.Root.NodeAdd(node);

  Memo1.Text := xml.WriteToString;
  xml.Free;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
	<item ID="1">
		<name>张三</name>
		<age>11</age>
	</item>
</List>
*****************************************}

procedure TForm1.Button4Click(Sender: TObject);
var
  xml: TNativeXml;
  node: TXmlNode;
begin
  xml := TNativeXml.CreateName('List');
  xml.XmlFormat := xfReadable;

  node := xml.Root.NodeNew('Data1');
  node.NodeAdd(xml.NodeNewTextType('DataName', 'XXXXXXXXXXXX', xeCData));

  Memo1.Text := xml.WriteToString;
  xml.Free;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List>
	<Data1><![CDATA[XXXXXXXXXXXX]]></Data1>
</List>
*****************************************}

procedure TForm1.Button5Click(Sender: TObject);
var
  xml: TNativeXml;
begin
  xml := TNativeXml.CreateName('List');
  xml.XmlFormat := xfReadable;

  xml.Root.NodeAdd(xml.NodeNewTextType('aaa', 'AAAAAAA', xeElement));
  xml.Root.NodeAdd(xml.NodeNewTextType('bbb', 'BBBBBBB', xeAttribute));
  xml.Root.NodeAdd(xml.NodeNewTextType('ccc', 'CCCCCCC', xeComment));
  xml.Root.NodeAdd(xml.NodeNewTextType('ddd', 'DDDDDDD', xeCData));
  xml.Root.NodeAdd(xml.NodeNewTextType('eee', 'EEEEEEE', xeCondSection));
  xml.Root.NodeAdd(xml.NodeNewTextType('fff', 'FFFFFFF', xeStylesheet));
  xml.Root.NodeAdd(xml.NodeNewTextType('ggg', 'GGGGGGG', xeDocType));
  xml.Root.NodeAdd(xml.NodeNewTextType('hhh', 'HHHHHHH', xeDtdElement));
  xml.Root.NodeAdd(xml.NodeNewTextType('iii', 'IIIIIII', xeDtdAttList));
  xml.Root.NodeAdd(xml.NodeNewTextType('jjj', 'JJJJJJJ', xeDtdEntity));
  xml.Root.NodeAdd(xml.NodeNewTextType('kkk', 'KKKKKKK', xeDtdNotation));
  xml.Root.NodeAdd(xml.NodeNewTextType('lll', 'LLLLLLL', xeInstruction));
  xml.Root.NodeAdd(xml.NodeNewTextType('mmm', 'MMMMMMM', xeCharData));
  xml.Root.NodeAdd(xml.NodeNewTextType('nnn', 'NNNNNNN', xeWhiteSpace));
  xml.Root.NodeAdd(xml.NodeNewTextType('ooo', 'OOOOOOO', xeQuotedText));

  Memo1.Text := xml.WriteToString;
  xml.Free;
end;
{***************************************
<?xml version="1.0" encoding="UTF-8"?>
<List bbb="BBBBBBB"NNNNNNN>
	<aaa>AAAAAAA</aaa>
	<!--CCCCCCC-->
	<![CDATA[DDDDDDD]]>
	<!--EEEEEEE-->
		<?xml-stylesheetFFFFFFF?>

 [
GGGGGGG]>
<!ELEMENT hhh HHHHHHH>
<!ATTLIST iii IIIIIII>
<!ENTITY jjj JJJJJJJ>
<!NOTATION kkk KKKKKKK>
		<?LLLLLLL?>

	MMMMMMM
	"OOOOOOO"
</List>
*****************************************}

 

九、NativeXml 读取

NativeXml打开xml文本

Var

Xml:TnativeXml;

Begin

Xml:=TnativeXml.Create(nil);

Xml.loadformFile(‘xml_file_path’);

End;

读取declaration

  declaration: TsdDeclaration;

  declaration := xml.RootNodes[0] as TsdDeclaration;

  ShowMessageFmt('%s, %s', [declaration.Version, declaration.Encoding]);  //1.0, UTF-8

节点信息以及读取

xml.RootNodes[1]代表根节点,xml.Root相同

xml.Root.Name:根节点名称

xml.Root.NodeCount:根节点子节点的总数

xml.Root.WriteToString读取根节点下的所有内容

xml.Root[0]:第一个子节点

{以下 引自万一}

SourceURL:http://www.cnblogs.com/del/archive/2011/03/25/1995661.html

var xml: TNativeXml; 
 
const 
  xmlStr = '<?xml version="1.0" encoding="UTF-8"?>' + 
           '<List>' + 
           '<item ID="No.1" age="11">寮犱笁</item>' + 
           '<item ID="No.2" age="22">鏉庡洓</item>' + 
           '<item ID="No.3" age="33">鐜嬩簲</item>' + 
           '</List>'; 
 
procedure TForm1.FormCreate(Sender: TObject); 
begin 
  xml := TNativeXml.Create(Self); 
  xml.XmlFormat := xfReadable; 
  xml.ReadFromString(xmlStr);     {!} 
  Memo1.Text := xml.WriteToString; 
end; 
 
{RootNodes} 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  declaration: TsdDeclaration; 
  root: TXmlNode; 
begin 
  ShowMessageFmt('%s, %s', [xml.VersionString, xml.EncodingString]);      //1.0, UTF-8 
  declaration := xml.RootNodes[0] as TsdDeclaration; 
  ShowMessageFmt('%s, %s', [declaration.Version, declaration.Encoding]);  //1.0, UTF-8 
 
  ShowMessageFmt('%s, %d', [xml.Root.Name, xml.Root.NodeCount]); //List, 3 
  root := xml.RootNodes[1]; 
  ShowMessageFmt('%s, %d', [root.Name, root.NodeCount]);         //List, 3 
end; 
 
procedure TForm1.Button2Click(Sender: TObject); 
var 
  node: TXmlNode; 
begin 
  node := xml.Root; 
  ShowMessage(node.WriteToString); 
end; 
{*************************************** 
<List> 
	<item ID="No.1" age="11">寮犱笁</item> 
	<item ID="No.2" age="22">鏉庡洓</item> 
	<item ID="No.3" age="33">鐜嬩簲</item> 
</List> 
*****************************************} 
 
procedure TForm1.Button3Click(Sender: TObject); 
var 
  node: TXmlNode; 
begin 
  node := xml.Root[0]; 
  ShowMessage(node.WriteToString); 
end; 
{*************************************** 
<item ID="No.1" age="11">寮犱笁</item> 
*****************************************} 
 
procedure TForm1.Button4Click(Sender: TObject); 
var 
  node: TXmlNode; 
begin 
  node := xml.Root[0]; 
  ShowMessage(node.Name);  //Item 
  ShowMessage(node.Value); //寮犱笁 
  ShowMessage(node.Attributes[0].Name);  //ID 
  ShowMessage(node.Attributes[0].Value); //No.1 
  ShowMessage(node.Attributes[0].WriteToString); //ID="No.1" 
end; 
 
procedure TForm1.Button5Click(Sender: TObject); 
var 
  node: TXmlNode; 
begin 
  node := xml.Root[0]; 
  ShowMessage(node.AttributeByName['ID'].Value); //No.1 
  ShowMessage(node.AttributeValueByName['ID']);  //No.1 
end; 
 
procedure TForm1.Button6Click(Sender: TObject); 
var 
  nodeList: TsdNodeList; 
  node: TXmlNode; 
  i: Integer; 
begin 
  node := xml.Root; 
  ShowMessage(node.ReadString('item'));       //寮犱笁 
  ShowMessage(node.FindNode('item').Value);   //寮犱笁 
  ShowMessage(node.NodeByName('item').Value); //寮犱笁 
  //node.NodesByName(); 鐢ㄦ硶鍚� FindNodes 
 
  nodeList := TsdNodeList.Create(False); 
  node.FindNodes('item', nodeList); 
  for i := 0 to nodeList.Count - 1 do 
  begin 
    ShowMessage(nodeList[i].Value); //寮犱笁/鏉庡洓/鐜嬩簲 
  end; 
  nodeList.Free; 
  // 
  for i := 0 to node.NodeCount - 1 do 
  begin 
    ShowMessage(node[i].Value);     //寮犱笁/鏉庡洓/鐜嬩簲 
  end; 
end; 
 
procedure TForm1.Button7Click(Sender: TObject); 
var 
  node: TXmlNode; 
begin 
  node := xml.Root[0]; 
  ShowMessage(node.Parent.Name);             //List 
  ShowMessage(node.Document.EncodingString); //UTF-8 
end; 
 
{StringTable} 
procedure TForm1.Button8Click(Sender: TObject); 
const 
  path = 'c:\temp\test.txt'; 
begin 
  xml.StringTable.SaveToFile(path); 
  Memo1.Lines.LoadFromFile(path, TEncoding.UTF8); 
end; 
{*************************************** 
22	1 
33	1 
11	1 
age	3 
ID	3 
UTF-8	1 
List	1 
1.0	1 
No.2	1 
item	1 
No.1	1 
No.3	1 
寮犱笁	1 
item	2 
鐜嬩簲	1 
鏉庡洓	1 
encoding	1 
version	1 
*****************************************}

 


 

 

 

 

var xml: TNativeXml; 
 
const 
  xmlStr = 
                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab符号运算的一些笔记发布时间:2022-07-18
下一篇:
Delphi 分辨千位、百位、十位和个位数值发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap