读书人

Delphi中怎么对XML中任意节点进行添加

发布时间: 2012-04-15 18:39:21 作者: rapoo

Delphi中如何对XML中任意节点进行添加、修改、删除、查询等操作?急。
如题,

[解决办法]
http://60.28.222.210/dispbbs.asp?boardID=11&ID=23546
[解决办法]
不知道,帮顶!
[解决办法]
NativeXML
[解决办法]
参考:(也可以注册成控件用)

unit XmlHelper;

interface

uses
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.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.

读书人网 >.NET

热点推荐