• Delphi操作XML(三)


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

    添加属性:

     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 
    *****************************************}


     

  • 相关阅读:
    汇编代码中db,dw,dd的区别
    利用汇编详解栈结构
    80X86指令总结
    【原创】自己动手写的一个查看函数API地址的小工具
    【初学破解】暴力破解绕过程序认证
    OD基本汇编指令
    排序算法
    数据聚类算法-K-means算法
    数据预测算法-ARIMA预测
    数据预测算法-指数平滑法-1
  • 原文地址:https://www.cnblogs.com/acuier/p/2352184.html
Copyright © 2020-2023  润新知