八、属性、注释以及内容的添加
添加属性:
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