如何读取字符串为xml的节点?不采用先存储xml文件然后再读取节点的方法,在线等哦................................(50分)

  • 主题发起人 softlong
  • 开始时间
S

softlong

Unregistered / Unconfirmed
GUEST, unregistred user!
已经写好一个动态链接库接口,返回的是一个很简单的xml文件,在delphi里读取的是一串为xml的格式字符串,现我不想采用先存储xml文件然后再读取节点的方法,因为反复读写的速度太频繁了,影响我下面的操作.所以有没有直接读取xml节点的好方法?请大家帮我想想办法,不胜感激....
 
unit XMLUtils;

interface

uses msxml, SysUtils;

function ChildNodeByName(const xParentNode: IXMLDOMNode; nodeName: WideString):
IXMLDOMNode;

function GetNodeAttributeValue(xNode: IXMLDOMNode; attributeName: WideString):
WideString;
procedure SetNodeAttributeValue(xDoc: IXMLDOMDocument; xNode: IXMLDOMNode;
attributeName, attributeValue: WideString);

function IsNodeHasAttributes(xNode: IXMLDOMNode; attributeName: WideString):
Boolean;

implementation

function ChildNodeByName(const xParentNode: IXMLDOMNode; nodeName: WideString):
IXMLDOMNode;
var
I: Integer;
xChildNode: IXMLDOMNode;
begin
Result := nil;
for I := 0 to xParentNode.childNodes.length - 1 do
begin
xChildNode := xParentNode.childNodes.item;
if SameText(xChildNode.nodeName, nodeName) then
begin
Result := xChildNode;
break;
end;
end;
end;

function GetNodeAttributeValue(xNode: IXMLDOMNode; attributeName: WideString):
WideString;
var
xAttribute: IXMLDOMNode;
begin
xAttribute := xNode.attributes.getNamedItem(attributeName);
Result := xAttribute.nodeValue;
end;

procedure SetNodeAttributeValue(xDoc: IXMLDOMDocument; xNode: IXMLDOMNode;
attributeName, attributeValue: WideString);
var
xAttribute: IXMLDOMNode;
begin
xAttribute := xNode.attributes.setNamedItem(xDoc.createAttribute(attributeName));
xAttribute.nodeValue := attributeValue;
end;

function IsNodeHasAttributes(xNode: IXMLDOMNode; attributeName: WideString):
Boolean;
var
xAttribute: IXMLDOMNode;
begin
xAttribute := xNode.attributes.getNamedItem(attributeName);
Result := xAttribute <> nil;
end;

end.
 
uses
xmldom, XMLIntf, msxmldom, XMLDoc;

var
xmldoc : IXMLDocument;
node : IXMLNode;
begin
XMLDoc := NewXMLDocument();
xmldoc.XML.Text := '你的XML字符串';
xmldoc.Active := True;
node := xmldoc.DocumentElement.ChildNodes.FindNode('abc');
if node <> nil then
ShowMessage(node.NodeName);
end;
 
谢谢你们,稍作修改可以使用了.非常感谢
 

Similar threads

回复
0
查看
523
不得闲
回复
0
查看
864
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
958
SUNSTONE的Delphi笔记
S
顶部