Code
{----------------------------------------------------------------------------------------------------------------------------
函数功能:直接读取xml文件中的某个节点第一次出现的值
入口参数:xmlFile xml文件
xmlnodepath 节点
xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
dep 节点的参数的分隔符,默认为.
返 回 值:末级点的值
}
function getXmlNodeValue(strEntityEngineFile: string; xmlNodePath: string;
const xmlattrname: string = ''; const dep: Char = '.'): string;
var
xmlDocument: IXMLDocument;
node: IXMLNode;
xmlnodeList: TStrings;
i: Integer;
urlcount: Integer;
begin
//xml节点路径
xmlnodeList := TStringList.Create;
xmlnodeList.Delimiter := dep;
xmlnodeList.DelimitedText := xmlnodepath;
urlcount := xmlnodeList.Count;
//xml对象
xmlDocument := TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active := true;
try
node := xmlDocument.DocumentElement;
if (node.NodeName = xmlnodeList[0]) then
begin
//扫描节点
for i := 1 to urlcount - 1 do
begin
if (node <> nil) then
node := getnodefromIXMLNodeList(node.ChildNodes, xmlnodeList[i])
else
Break;
end;
if (node = nil) then
begin
result := '';
end
else
begin
//判断是取属性还是取节点内容
if (Trim(xmlattrname) = '') then
result := node.Text
else
result := node.AttributeNodes.Nodes[xmlattrname].NodeValue;
end;
end
else
begin
result := '';
end;
except
result := 'error';
end;
xmlDocument.Active := false;
end;
{----------------------------------------------------------------------------------------------------------------------------
函数功能:保存xml文件中的某个节点或属性的值
入口参数:xmlFile xml文件
xmlnodepath 节点
xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
dep 节点的参数的分隔符,默认为.
返 回 值:末级点的值
}
function setXmlNodeValue(strEntityEngineFile: string; xmlNodePath: string;
const xmlattrname: string = ''; const value: string = ''; const dep: Char =
'.'): boolean;
var
xmlDocument: IXMLDocument;
node: IXMLNode;
xmlnodeList: TStrings;
i: Integer;
urlcount: Integer;
begin
//xml节点路径
xmlnodeList := TStringList.Create;
xmlnodeList.Delimiter := dep;
xmlnodeList.DelimitedText := xmlnodepath;
urlcount := xmlnodeList.Count;
//xml对象
xmlDocument := TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active := true;
try
node := xmlDocument.DocumentElement;
if (node.NodeName = xmlnodeList[0]) then
begin
//扫描节点
for i := 1 to urlcount - 1 do
begin
if (node <> nil) then
node := getnodefromIXMLNodeList(node.ChildNodes, xmlnodeList[i])
else
Break;
end;
if (node <> nil) then
begin
if (Trim(xmlattrname) = '') then
node.Text := value
else
node.AttributeNodes.Nodes[xmlattrname].NodeValue := value;
xmlDocument.SaveToFile(strEntityEngineFile);
end;
end;
result := true;
except
result := false;
end;
xmlDocument.Active := false;
end;
{----------------------------------------------------------------------------------------------------------------------------
函数功能:直接读取xml文件中的某个节点第一次出现的值
入口参数:xmlFile xml文件
xmlnodepath 节点
xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
dep 节点的参数的分隔符,默认为.
返 回 值:末级点的值
}
function getXmlNodeValue(strEntityEngineFile: string; xmlNodePath: string;
const xmlattrname: string = ''; const dep: Char = '.'): string;
var
xmlDocument: IXMLDocument;
node: IXMLNode;
xmlnodeList: TStrings;
i: Integer;
urlcount: Integer;
begin
//xml节点路径
xmlnodeList := TStringList.Create;
xmlnodeList.Delimiter := dep;
xmlnodeList.DelimitedText := xmlnodepath;
urlcount := xmlnodeList.Count;
//xml对象
xmlDocument := TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active := true;
try
node := xmlDocument.DocumentElement;
if (node.NodeName = xmlnodeList[0]) then
begin
//扫描节点
for i := 1 to urlcount - 1 do
begin
if (node <> nil) then
node := getnodefromIXMLNodeList(node.ChildNodes, xmlnodeList[i])
else
Break;
end;
if (node = nil) then
begin
result := '';
end
else
begin
//判断是取属性还是取节点内容
if (Trim(xmlattrname) = '') then
result := node.Text
else
result := node.AttributeNodes.Nodes[xmlattrname].NodeValue;
end;
end
else
begin
result := '';
end;
except
result := 'error';
end;
xmlDocument.Active := false;
end;
{----------------------------------------------------------------------------------------------------------------------------
函数功能:保存xml文件中的某个节点或属性的值
入口参数:xmlFile xml文件
xmlnodepath 节点
xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
dep 节点的参数的分隔符,默认为.
返 回 值:末级点的值
}
function setXmlNodeValue(strEntityEngineFile: string; xmlNodePath: string;
const xmlattrname: string = ''; const value: string = ''; const dep: Char =
'.'): boolean;
var
xmlDocument: IXMLDocument;
node: IXMLNode;
xmlnodeList: TStrings;
i: Integer;
urlcount: Integer;
begin
//xml节点路径
xmlnodeList := TStringList.Create;
xmlnodeList.Delimiter := dep;
xmlnodeList.DelimitedText := xmlnodepath;
urlcount := xmlnodeList.Count;
//xml对象
xmlDocument := TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active := true;
try
node := xmlDocument.DocumentElement;
if (node.NodeName = xmlnodeList[0]) then
begin
//扫描节点
for i := 1 to urlcount - 1 do
begin
if (node <> nil) then
node := getnodefromIXMLNodeList(node.ChildNodes, xmlnodeList[i])
else
Break;
end;
if (node <> nil) then
begin
if (Trim(xmlattrname) = '') then
node.Text := value
else
node.AttributeNodes.Nodes[xmlattrname].NodeValue := value;
xmlDocument.SaveToFile(strEntityEngineFile);
end;
end;
result := true;
except
result := false;
end;
xmlDocument.Active := false;
end;