开发环境Delphi XE11.3
没有使用第三方控件
有的代码是抄来的!
<![CDATA[价格区间:100 < 预算 < 500]]> 的生成才是我想测试的.
-----------测试这个<![CDATA[]]>的生成------------------

-----------Unit
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, XMl.XMLIntf, XMl.XMLDoc, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Memo_read: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure XML_Process(const FileName: string);
var
XMLDoc: IXMLDocument;
RootNode, CurrentNode, CurrentNode_Next: IXMLNode;
i,j: Integer;
begin
XMLDoc := TXMLDocument.Create(nil);
try
XMLDoc.LoadFromFile(FileName);
XMLDoc.Active := True;
RootNode := XMLDoc.DocumentElement;
if Assigned(RootNode) then
begin
// 遍历根节点下的所有子节点
for i:= 0 to RootNode.ChildNodes.Count - 1 do
begin
CurrentNode := RootNode.ChildNodes[i];
Form1.Memo_read.Lines.add(CurrentNode.NodeName); //获取整个节点文本
// 遍历当前节点下的所有子节点
for j := 0 to CurrentNode.ChildNodes.Count - 1 do
begin
CurrentNode_Next := CurrentNode.ChildNodes[j];
Form1.Memo_read.Lines.add(CurrentNode_Next.XML); //获取整个节点文本
Form1.Memo_read.Lines.add(CurrentNode_Next.NodeName); //获取节点名字
if (CurrentNode_Next.NodeName = 'MOSI') then //MOSI节点里包含的是属性,跟其他节点有所差异
begin
Form1.Memo_read.Lines.add( CurrentNode_Next.Attributes['bytes']); //获取节点属性值
end else if (CurrentNode_Next.NodeName <> 'MISO') then
begin
Form1.Memo_read.Lines.add(CurrentNode_Next.NodeValue); //获取节点值
end;
end;
end;
end;
finally
XMLDoc := nil; // 释放XML文档
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo_read.Clear;
XML_Process(ComboBox1.Text); //output.xml
end;
procedure TForm1.Button2Click(Sender: TObject);
var
XMLDoc: IXMLDocument;
RootNode, ChildNode: IXMLNode;
DataNode, DataNodeSub: IXMLNode;
vvv: WideString;
begin
XMLDoc := NewXMLDocument; // 初始化 XML 文档
XMLDoc.Encoding := 'UTF-8';
//XMLDoc.Encoding := 'GB2312';
XMLDoc.Options := [doNodeAutoIndent]; // 设置自动缩进
// 创建根节点
RootNode := XMLDoc.AddChild('Root');
// 添加子节点及属性
ChildNode := RootNode.AddChild('User');
ChildNode.Attributes['id'] := '001'; // 设置属性
ChildNode.AddChild('Name').Text := '张三'; // 设置文本内容
ChildNode.AddChild('Age').Text := '30';
DataNode := ChildNode.AddChild('Description');
DataNodeSub := XMLDoc.CreateNode('价格区间:100 < 预算 < 500', ntCData);
DataNode.ChildNodes.Add(DataNodeSub);
{ DataNode := RootNode.AddChild('Description');
DataNodeSub := XMLDoc.CreateNode('价格区间:100 < 预算 < 500', ntCData);
DataNode.ChildNodes.Add(DataNodeSub);
}
XMLDoc.SaveToFile('output.xml'); // 保存为 XML 文件
end;
procedure TForm1.Button3Click(Sender: TObject);
var
XMLDoc: IXMLDocument;
OrderNode, ItemNode: IXMLNode;
begin
XMLDoc := NewXMLDocument;
// 添加订单根节点
OrderNode := XMLDoc.AddChild('Order');
OrderNode.Attributes['date'] := '2025-03-11'; // 根节点属性
// 添加商品子节点
ItemNode := OrderNode.AddChild('Item');
ItemNode.Attributes['id'] := 'A100';
ItemNode.AddChild('Name').Text := 'Delphi开发指南';
ItemNode.AddChild('Price').Text := '99.00';
XMLDoc.SaveToFile('order.xml');
end;
end.
------------Form
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 442
ClientWidth = 542
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = 'Segoe UI'
Font.Style = []
Position = poDesktopCenter
TextHeight = 15
object Memo_read: TMemo
Left = 0
Top = 0
Width = 305
Height = 442
Align = alLeft
ScrollBars = ssBoth
TabOrder = 0
ExplicitHeight = 441
end
object Button1: TButton
Left = 333
Top = 32
Width = 122
Height = 25
Caption = 'Button1_'#35835#21462
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 311
Top = 104
Width = 220
Height = 25
Caption = 'Button2_XML'#29983#25104
TabOrder = 2
OnClick = Button2Click
end
object Button3: TButton
Left = 311
Top = 159
Width = 220
Height = 25
Caption = 'Button3_XML'#29983#25104'2'
TabOrder = 3
OnClick = Button3Click
end
object ComboBox1: TComboBox
Left = 328
Top = 3
Width = 145
Height = 23
TabOrder = 4
Text = 'HJP_Config_Test.xml'
Items.Strings = (
'output.xml'
'HJP_Config_Test.xml'
'order.xml')
end
end
浙公网安备 33010602011771号