参考:(也可以注册成控件用)unit XmlHelper;interfaceuses Windows, Messages, SysUtils, Variants, Classes,Dialogs,Graphics,Controls,Forms,ComCtrls, StdCtrls,ExtCtrls,xmldom, XMLDoc, XMLIntf,Math,Contnrs;type TXmlNodeObject = class(TObject) public XmlNode : IXmlNode; end; //----------------------------------------------- // Xml装载方式 // //----------------------------------------------- TXmlLoadType = (FromString,FromLocalFile,FromURL); TXmlHelper = class(TObject) private m_XmlDoc: IXmlDocument; m_sLastErrorMessage: WideString; function GetDocument:IXmlDocument; function GetEncoding:WideString; procedure SetEncoding(const Value: WideString); function GetRootNode: IXmlNode; public Constructor Create;overload; Constructor Create(xmlDoc:IXmlDocument);overload; Destructor Free; property Document:IXmlDocument read GetDocument; property Encoding:WideString read GetEncoding write SetEncoding; property RootNode:IXmlNode read GetRootNode; function SaveToFile(sTargetFileName : WideString):Boolean; function GetXmlString: WideString; function LoadXML(sourceXMLOrFile:WideString;loadType:TXmlLoadType):Boolean; function GetAttributeValue(node : IXmlNode; sAttributeName : WideString):WideString; function GetAttributeInt32(node : IXmlNode; sAttributeName : WideString):Integer; function GetAttributeDouble(node : IXmlNode; sAttributeName : WideString):Double; function GetAttributeBoolean(node : IXmlNode; sAttributeName : WideString):Boolean; function GetElementValue(node : IXmlNode):WideString; function GetElementInt32(node : IXmlNode):Integer; function GetElementDouble(node : IXmlNode):Double; function GetElementBoolean(node : IXmlNode):Boolean; function GetChildElementValue(parentNode : IXmlNode;sElementName : WideString):WideString; function GetChildElementInt32(parentNode : IXmlNode;sElementName : WideString):Integer; function GetChildElementDouble(parentNode : IXmlNode;sElementName : WideString):Double; function GetChildElementBoolean(parentNode : IXmlNode;sElementName : WideString):Boolean; function GetFirstChildXmlNodeFromRoot(sElementName : WideString) : TXmlNodeObject; function GetFirstChildXmlNode(parentNode : IXmlNode;sElementname : WideString) : IXmlNode; function GetChildNodesFromRoot( sElementName : WideString ) : TObjectList; function GetRecursiveChildNodesFromParent(parentNode : IXmlNode;sElementName : WideString) : TObjectList; function CreateNodeElement(parentNode : IXmlNode ; sElementName,sElementValue : WideString) : IXmlNode; //function CreateComment(insertAfterThisNode : IXmlNode;sVal : WideString) : IXmlNode; //function CreateXmlDeclaration(sVersion,sEncoding,sStandalone : WideString) : IXmlNode; function DeleteNodeElement(targetNode : IXmlNode) : Boolean; function ModifyNodeElementValue(targetNode : IXmlNode;sNewElementValue : WideString) : Boolean; function CreateNodeAttribute(targetNode : IXmlNode;sAttrName,sAttrValue : WideString) : Boolean; function DeleteNodeAttribute(targetNode : IXmlNode;sAttrName : WideString) : Boolean; function ModifyNodeAttributeValue(targetNode : IXmlNode;sAttrName,sNewAttrValue : WideString) : Boolean; function Encode(input : string) : string; function Decode(input : string) : string; end;
//实现部分: implementation{ TXmlHelper }constructor TXmlHelper.Create; begin Inherited Create; m_sLastErrorMessage := ''; m_XmlDoc := TXMLDocument.Create(nil); end;constructor TXmlHelper.Create(xmlDoc: IXmlDocument); begin Inherited Create; m_sLastErrorMessage := ''; if( xmlDoc = nil) then begin m_XmlDoc := TXMLDocument.Create(nil); end else begin m_XmlDoc := xmlDoc; end;end;destructor TXmlHelper.Free; begin if(m_XmlDoc <> nil) then begin //m_XmlDoc.Free; m_XmlDoc.Active := False; m_XmlDoc := nil; end; inherited Destroy; end;function TXmlHelper.GetDocument: IXmlDocument; begin Result := m_XmlDoc; end;function TXmlHelper.GetEncoding: WideString; begin if( m_XmlDoc = nil) then raise EInvalidArgument.Create('XmlHelper ÀïµÄDocument Ϊ¿Õ'); Result := m_XmlDoc.Encoding; end;procedure TXmlHelper.SetEncoding(const Value: WideString); begin if( m_XmlDoc = nil) then raise EInvalidArgument.Create('XmlHelper ÀïµÄDocument Ϊ¿Õ'); m_XmlDoc.Encoding := Value; end; function TXmlHelper.GetXmlString: WideString; begin if( m_XmlDoc = nil) then raise EInvalidArgument.Create('XmlHelper ÀïµÄDocument Ϊ¿Õ'); Result := m_XmlDoc.XML.Text;end;function TXmlHelper.LoadXML(sourceXMLOrFile: WideString; loadType: TXmlLoadType): Boolean; begin Result := false; try case loadType of FromString : m_XmlDoc.LoadFromXML(sourceXMLOrFile); FromLocalFile : m_XmlDoc.LoadFromFile(sourceXMLOrFile); FromURL : m_XmlDoc.FileName := sourceXMLOrFile; else raise EInvalidArgument.Create('XmlHelper 里的document为空'); end; m_XmlDoc.Active := true; Result := true; except Result := false; end;end;function TXmlHelper.SaveToFile(sTargetFileName: WideString): Boolean; begin Result := false; try m_XmlDoc.SaveToFile(sTargetFileName); Result := true; except Result := false; end; end;
function TXmlHelper.GetRootNode : IXmlNode; var nIndex : Integer; firstnode : IXmlNode;begin if(m_XmlDoc = nil) then raise EInvalidArgument.Create('XmlHelper ÀïµÄDocument Ϊ¿Õ'); nIndex := 0; firstnode := m_XmlDoc.ChildNodes[nIndex]; while (firstnode.NodeType <> ntElement) do begin nIndex := nIndex + 1; if nIndex >= m_XmlDoc.ChildNodes.Count then raise EOverflow.Create('XmlHelper 里的document为空'); firstnode := m_XmlDoc.ChildNodes[nIndex]; end; Result := firstnode;end; //节点属性为true or yes 返回真 //否则返回假 function TXmlHelper.GetAttributeBoolean(node: IXmlNode; sAttributeName: WideString): Boolean; var sAttrValue : string; begin Result := false; sAttrValue := GetAttributeValue(node,sAttributeName); if( (LowerCase(sAttrValue) = 'true') or (LowerCase(sAttrValue) ='yes') ) then Result := true ;end; function TXmlHelper.GetAttributeDouble(node: IXmlNode; sAttributeName: WideString): Double; var sAttrValue : string; dAttrValue : Double; begin Result := 0.00; sAttrValue := GetAttributeValue(node,sAttributeName); if( TryStrToFloat(sAttrValue,dAttrValue) ) then Result := dAttrValue;end;function TXmlHelper.GetAttributeInt32(node: IXmlNode; sAttributeName: WideString): Integer; var sAttrValue : string; nAttrValue : Integer; begin Result := 0; sAttrValue := GetAttributeValue(node,sAttributeName); if( TryStrToInt(sAttrValue,nAttrValue) ) then Result := nAttrValue; end;function TXmlHelper.GetAttributeValue(node: IXmlNode; sAttributeName: WideString): WideString; begin if ( node.HasAttribute(sAttributeName) ) then begin Result := Decode(node.Attributes[sAttributeName]); end else raise EXmlDocError.Create('节点没有指定的属性'); end; function TXmlHelper.GetChildElementBoolean(parentNode: IXmlNode; sElementName: WideString): Boolean; var sElementValue : string; bElementValue : Boolean; begin Result := false; sElementValue := GetChildElementValue(parentNode,sElementName); if( (LowerCase(sElementValue) = 'true') or (LowerCase(sElementValue) ='yes') ) then Result := true;end;function TXmlHelper.GetChildElementDouble(parentNode: IXmlNode; sElementName: WideString): Double; var sElementValue : string; dElementValue : Double; begin Result := 0.00; sElementValue := GetChildElementValue(parentNode,sElementName); if TryStrToFloat(sElementValue,dElementValue) then Result := dElementValue; end;function TXmlHelper.GetChildElementInt32(parentNode: IXmlNode; sElementName: WideString): Integer; var sElementValue : string; nElementValue : Integer; begin Result := 0; sElementValue := GetChildElementValue(parentNode,sElementName); if TryStrToInt(sElementValue,nElementValue) then Result := nElementValue;end;function TXmlHelper.GetChildElementValue(parentNode: IXmlNode; sElementName: WideString): WideString; var I : Integer; childNodeList : IXmlNodeList; childNode : IXmlNode; bFind : Boolean; begin Result := ''; bFind := false; childNodeList := parentNode.ChildNodes; for I := 0 to childNodeList.Count - 1 do // Iterate begin if(childNodeList[I].NodeName = sElementName) then begin childNode := childNodeList[I]; bFind := true; break; end end; // for if bFind then Result := GetElementValue(childNode);
end;
function TXmlHelper.GetElementBoolean(node: IXmlNode): Boolean; var sValue : string; begin Result := false; sValue := GetElementValue(node); if( (LowerCase(sValue) = 'true') or (LowerCase(sValue) ='yes') ) then Result := true;end;function TXmlHelper.GetElementDouble(node: IXmlNode): Double; var sValue : string; dValue : Double; begin Result := 0.00; sValue := GetElementValue(node); if( TryStrToFloat(sValue,dValue)) then Result := dValue;end;function TXmlHelper.GetElementInt32(node: IXmlNode): Integer; var sValue : string; nValue : Integer; begin Result := 0; sValue := GetElementValue(node); if( TryStrToInt(sValue,nValue)) then Result := nValue;end;function TXmlHelper.GetElementValue(node: IXmlNode): WideString; begin Result := Decode(node.NodeValue); end;function TXmlHelper.Decode(input: string): string; var output : string; begin Result := ''; output := input; output := StringReplace(output, '&','&',[rfReplaceAll]); output := StringReplace(output, '<', '<',[rfReplaceAll]); output := StringReplace(output, '>', '>',[rfReplaceAll]); output := StringReplace(output, '"', '"',[rfReplaceAll]); Result := output; end; function TXmlHelper.Encode(input: string): string; var output : string; begin Result := ''; if input = '' then Exit; output := input; output := StringReplace(output, '&', '&',[rfReplaceAll]); output := StringReplace(output, '<', '<',[rfReplaceAll]); output := StringReplace(output, '>', '>',[rfReplaceAll]); output := StringReplace(output, '"', '"',[rfReplaceAll]); Result := output; end; function TXmlHelper.GetChildNodesFromRoot( sElementName: WideString): TObjectList; var I : Integer; node : IXmlNode; nodeslists : TObjectList; selectNode : TXmlNodeObject; begin node := RootNode; nodeslists := TObjectList.Create; for I := 0 to node.ChildNodes.Count - 1 do // Iterate begin if node.ChildNodes[I].NodeName = sElementName then begin selectNode := TXmlNodeObject.Create; selectNode.XmlNode := node.ChildNodes[I]; nodeslists.Add(selectNode); end; end; // for Result := nodeslists;end;function TXmlHelper.GetFirstChildXmlNode(parentNode: IXmlNode; sElementname: WideString): IXmlNode; var I: Integer; foundNode : IXmlNode; begin foundNode := nil; for I := 0 to parentNode.ChildNodes.Count - 1 do // Iterate begin if parentNode.ChildNodes[I].NodeName = sElementName then begin foundNode := parentNode.ChildNodes[I]; break; end end; // for Result := foundNode;end;function TXmlHelper.GetFirstChildXmlNodeFromRoot( sElementName: WideString): TXmlNodeObject; var nodeslists : TObjectList; begin Result := nil; nodeslists := GetChildNodesFromRoot(sElementName); if nodeslists.Count > 0 then Result := nodeslists[0] as TXmlNodeObject;end;function TXmlHelper.GetRecursiveChildNodesFromParent(parentNode : IXmlNode; sElementName: WideString): TObjectList; var I,J,K: Integer; childNodeLists : TObjectList; tmpChildNodeLists : TObjectList; selectNode : TXmlNodeObject; begin childNodeLists := TObjectList.Create; for I := 0 to parentNode.ChildNodes.Count - 1 do // Iterate begin if parentNode.ChildNodes[I].NodeName = sElementName then begin selectNode := TXmlNodeObject.Create; selectNode.XmlNode := parentNode.ChildNodes[I]; childNodeLists.Add(selectNode); end; //Recursive if parentNode.ChildNodes[I].HasChildNodes then begin tmpChildNodeLists := GetRecursiveChildNodesFromParent(parentNode.ChildNodes[I],sElementName); for J := 0 to tmpChildNodeLists.Count - 1 do // Iterate childNodeLists.Add(tmpChildNodeLists.Items[J]); //这里如果不注释掉总会有异常抛出,不知道为什么,所以递归调用时,这里会有内存没有Free!!! //有内存息漏 {if tmpChildNodeLists <> nil then begin tmpChildNodeLists.Clear; tmpChildNodeLists.Free; end; } end; end; // for Result := childNodeLists;end;function TXmlHelper.CreateNodeElement(parentNode: IXmlNode; sElementName, sElementValue: WideString): IXmlNode; var childNode : IXmlNode; begin childNode := parentNode.AddChild(sElementName); if sElementValue <> '' then begin childNode.NodeValue := Encode(sElementValue); end; Result := childNode; end;function TXmlHelper.DeleteNodeElement(targetNode: IXmlNode): Boolean; var nRet : Integer; begin Result := false; try nRet := targetNode.ParentNode.ChildNodes.Remove(targetNode); if(nRet <> -1) then Result := true; except Result := false; end; end;
function TXmlHelper.ModifyNodeElementValue(targetNode: IXmlNode; sNewElementValue: WideString): Boolean; begin Result := false; try if(targetNode.ReadOnly) then exit; targetNode.NodeValue := Encode(sNewElementValue); Result := true; except Result := false; end;end;function TXmlHelper.CreateNodeAttribute(targetNode: IXmlNode; sAttrName, sAttrValue: WideString): Boolean; begin targetNode.SetAttributeNS(sAttrName,targetNode.NamespaceURI,Encode(sAttrValue)); end;function TXmlHelper.DeleteNodeAttribute(targetNode: IXmlNode; sAttrName: WideString): Boolean; var oAttrXmlNode : IXmlNode; nRet : Integer; begin Result := false; try oAttrXmlNode := targetNode.AttributeNodes[sAttrName]; if( (oAttrXmlNode <> nil) and (oAttrXmlNode.NodeType = ntAttribute) ) then begin nRet := targetNode.AttributeNodes.Remove(oAttrXmlNode); if(nRet <> -1) then Result := true; end; except Result := false; end; end;function TXmlHelper.ModifyNodeAttributeValue(targetNode: IXmlNode; sAttrName, sNewAttrValue: WideString): Boolean; begin targetNode.SetAttributeNS(sAttrName,targetNode.NamespaceURI,Encode(sNewAttrValue)); end;end.
Windows, Messages, SysUtils, Variants, Classes,Dialogs,Graphics,Controls,Forms,ComCtrls,
StdCtrls,ExtCtrls,xmldom, XMLDoc, XMLIntf,Math,Contnrs;type TXmlNodeObject = class(TObject)
public
XmlNode : IXmlNode;
end; //-----------------------------------------------
// Xml装载方式
//
//-----------------------------------------------
TXmlLoadType = (FromString,FromLocalFile,FromURL); TXmlHelper = class(TObject)
private
m_XmlDoc: IXmlDocument;
m_sLastErrorMessage: WideString; function GetDocument:IXmlDocument;
function GetEncoding:WideString;
procedure SetEncoding(const Value: WideString);
function GetRootNode: IXmlNode; public
Constructor Create;overload;
Constructor Create(xmlDoc:IXmlDocument);overload;
Destructor Free; property Document:IXmlDocument read GetDocument;
property Encoding:WideString read GetEncoding write SetEncoding;
property RootNode:IXmlNode read GetRootNode; function SaveToFile(sTargetFileName : WideString):Boolean;
function GetXmlString: WideString;
function LoadXML(sourceXMLOrFile:WideString;loadType:TXmlLoadType):Boolean;
function GetAttributeValue(node : IXmlNode; sAttributeName : WideString):WideString;
function GetAttributeInt32(node : IXmlNode; sAttributeName : WideString):Integer;
function GetAttributeDouble(node : IXmlNode; sAttributeName : WideString):Double;
function GetAttributeBoolean(node : IXmlNode; sAttributeName : WideString):Boolean; function GetElementValue(node : IXmlNode):WideString;
function GetElementInt32(node : IXmlNode):Integer;
function GetElementDouble(node : IXmlNode):Double;
function GetElementBoolean(node : IXmlNode):Boolean; function GetChildElementValue(parentNode : IXmlNode;sElementName : WideString):WideString;
function GetChildElementInt32(parentNode : IXmlNode;sElementName : WideString):Integer;
function GetChildElementDouble(parentNode : IXmlNode;sElementName : WideString):Double;
function GetChildElementBoolean(parentNode : IXmlNode;sElementName : WideString):Boolean;
function GetFirstChildXmlNodeFromRoot(sElementName : WideString) : TXmlNodeObject;
function GetFirstChildXmlNode(parentNode : IXmlNode;sElementname : WideString) : IXmlNode;
function GetChildNodesFromRoot( sElementName : WideString ) : TObjectList;
function GetRecursiveChildNodesFromParent(parentNode : IXmlNode;sElementName : WideString) : TObjectList; function CreateNodeElement(parentNode : IXmlNode ; sElementName,sElementValue : WideString) : IXmlNode;
//function CreateComment(insertAfterThisNode : IXmlNode;sVal : WideString) : IXmlNode;
//function CreateXmlDeclaration(sVersion,sEncoding,sStandalone : WideString) : IXmlNode;
function DeleteNodeElement(targetNode : IXmlNode) : Boolean;
function ModifyNodeElementValue(targetNode : IXmlNode;sNewElementValue : WideString) : Boolean; function CreateNodeAttribute(targetNode : IXmlNode;sAttrName,sAttrValue : WideString) : Boolean;
function DeleteNodeAttribute(targetNode : IXmlNode;sAttrName : WideString) : Boolean;
function ModifyNodeAttributeValue(targetNode : IXmlNode;sAttrName,sNewAttrValue : WideString) : Boolean; function Encode(input : string) : string;
function Decode(input : string) : string; end;
implementation{ TXmlHelper }constructor TXmlHelper.Create;
begin
Inherited Create;
m_sLastErrorMessage := '';
m_XmlDoc := TXMLDocument.Create(nil);
end;constructor TXmlHelper.Create(xmlDoc: IXmlDocument);
begin
Inherited Create;
m_sLastErrorMessage := '';
if( xmlDoc = nil) then
begin
m_XmlDoc := TXMLDocument.Create(nil);
end
else begin
m_XmlDoc := xmlDoc;
end;end;destructor TXmlHelper.Free;
begin
if(m_XmlDoc <> nil) then
begin
//m_XmlDoc.Free;
m_XmlDoc.Active := False;
m_XmlDoc := nil;
end;
inherited Destroy;
end;function TXmlHelper.GetDocument: IXmlDocument;
begin
Result := m_XmlDoc;
end;function TXmlHelper.GetEncoding: WideString;
begin
if( m_XmlDoc = nil) then
raise EInvalidArgument.Create('XmlHelper ÀïµÄDocument Ϊ¿Õ'); Result := m_XmlDoc.Encoding;
end;procedure TXmlHelper.SetEncoding(const Value: WideString);
begin
if( m_XmlDoc = nil) then
raise EInvalidArgument.Create('XmlHelper ÀïµÄDocument Ϊ¿Õ'); m_XmlDoc.Encoding := Value;
end;
function TXmlHelper.GetXmlString: WideString;
begin
if( m_XmlDoc = nil) then
raise EInvalidArgument.Create('XmlHelper ÀïµÄDocument Ϊ¿Õ'); Result := m_XmlDoc.XML.Text;end;function TXmlHelper.LoadXML(sourceXMLOrFile: WideString;
loadType: TXmlLoadType): Boolean;
begin
Result := false;
try
case loadType of
FromString : m_XmlDoc.LoadFromXML(sourceXMLOrFile);
FromLocalFile : m_XmlDoc.LoadFromFile(sourceXMLOrFile);
FromURL : m_XmlDoc.FileName := sourceXMLOrFile;
else
raise EInvalidArgument.Create('XmlHelper 里的document为空');
end; m_XmlDoc.Active := true;
Result := true;
except
Result := false;
end;end;function TXmlHelper.SaveToFile(sTargetFileName: WideString): Boolean;
begin
Result := false;
try
m_XmlDoc.SaveToFile(sTargetFileName);
Result := true;
except
Result := false;
end;
end;
function TXmlHelper.GetRootNode : IXmlNode;
var
nIndex : Integer;
firstnode : IXmlNode;begin if(m_XmlDoc = nil) then
raise EInvalidArgument.Create('XmlHelper ÀïµÄDocument Ϊ¿Õ'); nIndex := 0; firstnode := m_XmlDoc.ChildNodes[nIndex]; while (firstnode.NodeType <> ntElement) do
begin
nIndex := nIndex + 1;
if nIndex >= m_XmlDoc.ChildNodes.Count then
raise EOverflow.Create('XmlHelper 里的document为空'); firstnode := m_XmlDoc.ChildNodes[nIndex];
end; Result := firstnode;end;
//节点属性为true or yes 返回真
//否则返回假
function TXmlHelper.GetAttributeBoolean(node: IXmlNode;
sAttributeName: WideString): Boolean;
var
sAttrValue : string;
begin
Result := false;
sAttrValue := GetAttributeValue(node,sAttributeName);
if( (LowerCase(sAttrValue) = 'true') or (LowerCase(sAttrValue) ='yes') ) then
Result := true ;end;
function TXmlHelper.GetAttributeDouble(node: IXmlNode;
sAttributeName: WideString): Double;
var
sAttrValue : string;
dAttrValue : Double;
begin Result := 0.00; sAttrValue := GetAttributeValue(node,sAttributeName); if( TryStrToFloat(sAttrValue,dAttrValue) ) then
Result := dAttrValue;end;function TXmlHelper.GetAttributeInt32(node: IXmlNode;
sAttributeName: WideString): Integer;
var
sAttrValue : string;
nAttrValue : Integer;
begin
Result := 0; sAttrValue := GetAttributeValue(node,sAttributeName); if( TryStrToInt(sAttrValue,nAttrValue) ) then
Result := nAttrValue;
end;function TXmlHelper.GetAttributeValue(node: IXmlNode;
sAttributeName: WideString): WideString;
begin if ( node.HasAttribute(sAttributeName) ) then
begin
Result := Decode(node.Attributes[sAttributeName]);
end
else
raise EXmlDocError.Create('节点没有指定的属性');
end;
function TXmlHelper.GetChildElementBoolean(parentNode: IXmlNode;
sElementName: WideString): Boolean;
var
sElementValue : string;
bElementValue : Boolean;
begin
Result := false; sElementValue := GetChildElementValue(parentNode,sElementName);
if( (LowerCase(sElementValue) = 'true') or (LowerCase(sElementValue) ='yes') ) then
Result := true;end;function TXmlHelper.GetChildElementDouble(parentNode: IXmlNode;
sElementName: WideString): Double;
var
sElementValue : string;
dElementValue : Double;
begin
Result := 0.00; sElementValue := GetChildElementValue(parentNode,sElementName);
if TryStrToFloat(sElementValue,dElementValue) then
Result := dElementValue;
end;function TXmlHelper.GetChildElementInt32(parentNode: IXmlNode;
sElementName: WideString): Integer;
var
sElementValue : string;
nElementValue : Integer;
begin
Result := 0; sElementValue := GetChildElementValue(parentNode,sElementName);
if TryStrToInt(sElementValue,nElementValue) then
Result := nElementValue;end;function TXmlHelper.GetChildElementValue(parentNode: IXmlNode;
sElementName: WideString): WideString;
var
I : Integer;
childNodeList : IXmlNodeList;
childNode : IXmlNode;
bFind : Boolean;
begin
Result := ''; bFind := false;
childNodeList := parentNode.ChildNodes;
for I := 0 to childNodeList.Count - 1 do // Iterate
begin
if(childNodeList[I].NodeName = sElementName) then
begin
childNode := childNodeList[I];
bFind := true;
break;
end
end; // for if bFind then
Result := GetElementValue(childNode);
end;
function TXmlHelper.GetElementBoolean(node: IXmlNode): Boolean;
var
sValue : string;
begin
Result := false; sValue := GetElementValue(node);
if( (LowerCase(sValue) = 'true') or (LowerCase(sValue) ='yes') ) then
Result := true;end;function TXmlHelper.GetElementDouble(node: IXmlNode): Double;
var
sValue : string;
dValue : Double;
begin
Result := 0.00; sValue := GetElementValue(node);
if( TryStrToFloat(sValue,dValue)) then
Result := dValue;end;function TXmlHelper.GetElementInt32(node: IXmlNode): Integer;
var
sValue : string;
nValue : Integer;
begin
Result := 0; sValue := GetElementValue(node);
if( TryStrToInt(sValue,nValue)) then
Result := nValue;end;function TXmlHelper.GetElementValue(node: IXmlNode): WideString;
begin
Result := Decode(node.NodeValue);
end;function TXmlHelper.Decode(input: string): string;
var
output : string;
begin
Result := ''; output := input;
output := StringReplace(output, '&','&',[rfReplaceAll]);
output := StringReplace(output, '<', '<',[rfReplaceAll]);
output := StringReplace(output, '>', '>',[rfReplaceAll]);
output := StringReplace(output, '"', '"',[rfReplaceAll]); Result := output;
end;
function TXmlHelper.Encode(input: string): string;
var
output : string;
begin
Result := ''; if input = '' then
Exit; output := input; output := StringReplace(output, '&', '&',[rfReplaceAll]);
output := StringReplace(output, '<', '<',[rfReplaceAll]);
output := StringReplace(output, '>', '>',[rfReplaceAll]);
output := StringReplace(output, '"', '"',[rfReplaceAll]); Result := output;
end;
function TXmlHelper.GetChildNodesFromRoot(
sElementName: WideString): TObjectList;
var
I : Integer;
node : IXmlNode;
nodeslists : TObjectList;
selectNode : TXmlNodeObject;
begin node := RootNode;
nodeslists := TObjectList.Create;
for I := 0 to node.ChildNodes.Count - 1 do // Iterate
begin
if node.ChildNodes[I].NodeName = sElementName then
begin
selectNode := TXmlNodeObject.Create;
selectNode.XmlNode := node.ChildNodes[I];
nodeslists.Add(selectNode);
end;
end; // for Result := nodeslists;end;function TXmlHelper.GetFirstChildXmlNode(parentNode: IXmlNode;
sElementname: WideString): IXmlNode;
var
I: Integer;
foundNode : IXmlNode;
begin foundNode := nil;
for I := 0 to parentNode.ChildNodes.Count - 1 do // Iterate
begin
if parentNode.ChildNodes[I].NodeName = sElementName then
begin
foundNode := parentNode.ChildNodes[I];
break;
end
end; // for Result := foundNode;end;function TXmlHelper.GetFirstChildXmlNodeFromRoot(
sElementName: WideString): TXmlNodeObject;
var
nodeslists : TObjectList;
begin Result := nil; nodeslists := GetChildNodesFromRoot(sElementName); if nodeslists.Count > 0 then
Result := nodeslists[0] as TXmlNodeObject;end;function TXmlHelper.GetRecursiveChildNodesFromParent(parentNode : IXmlNode;
sElementName: WideString): TObjectList;
var
I,J,K: Integer;
childNodeLists : TObjectList;
tmpChildNodeLists : TObjectList;
selectNode : TXmlNodeObject;
begin childNodeLists := TObjectList.Create; for I := 0 to parentNode.ChildNodes.Count - 1 do // Iterate
begin
if parentNode.ChildNodes[I].NodeName = sElementName then
begin
selectNode := TXmlNodeObject.Create;
selectNode.XmlNode := parentNode.ChildNodes[I];
childNodeLists.Add(selectNode);
end; //Recursive
if parentNode.ChildNodes[I].HasChildNodes then
begin
tmpChildNodeLists := GetRecursiveChildNodesFromParent(parentNode.ChildNodes[I],sElementName);
for J := 0 to tmpChildNodeLists.Count - 1 do // Iterate
childNodeLists.Add(tmpChildNodeLists.Items[J]); //这里如果不注释掉总会有异常抛出,不知道为什么,所以递归调用时,这里会有内存没有Free!!!
//有内存息漏
{if tmpChildNodeLists <> nil then
begin
tmpChildNodeLists.Clear;
tmpChildNodeLists.Free;
end;
}
end;
end; // for Result := childNodeLists;end;function TXmlHelper.CreateNodeElement(parentNode: IXmlNode; sElementName,
sElementValue: WideString): IXmlNode;
var
childNode : IXmlNode;
begin
childNode := parentNode.AddChild(sElementName);
if sElementValue <> '' then
begin
childNode.NodeValue := Encode(sElementValue);
end; Result := childNode;
end;function TXmlHelper.DeleteNodeElement(targetNode: IXmlNode): Boolean;
var
nRet : Integer;
begin
Result := false;
try nRet := targetNode.ParentNode.ChildNodes.Remove(targetNode);
if(nRet <> -1) then
Result := true; except
Result := false;
end;
end;
function TXmlHelper.ModifyNodeElementValue(targetNode: IXmlNode;
sNewElementValue: WideString): Boolean;
begin
Result := false;
try if(targetNode.ReadOnly) then
exit; targetNode.NodeValue := Encode(sNewElementValue); Result := true;
except
Result := false;
end;end;function TXmlHelper.CreateNodeAttribute(targetNode: IXmlNode; sAttrName,
sAttrValue: WideString): Boolean;
begin
targetNode.SetAttributeNS(sAttrName,targetNode.NamespaceURI,Encode(sAttrValue));
end;function TXmlHelper.DeleteNodeAttribute(targetNode: IXmlNode;
sAttrName: WideString): Boolean;
var
oAttrXmlNode : IXmlNode;
nRet : Integer;
begin
Result := false;
try
oAttrXmlNode := targetNode.AttributeNodes[sAttrName];
if( (oAttrXmlNode <> nil) and (oAttrXmlNode.NodeType = ntAttribute) ) then
begin
nRet := targetNode.AttributeNodes.Remove(oAttrXmlNode);
if(nRet <> -1) then
Result := true;
end; except
Result := false;
end;
end;function TXmlHelper.ModifyNodeAttributeValue(targetNode: IXmlNode;
sAttrName, sNewAttrValue: WideString): Boolean;
begin
targetNode.SetAttributeNS(sAttrName,targetNode.NamespaceURI,Encode(sNewAttrValue));
end;end.