菜鸟请教有关XML操作的问题 ( 积分: 50 )

B

bluepro

Unregistered / Unconfirmed
GUEST, unregistred user!
Delphi中,定义一个IXMLDocument和IXMLNode变量,IXMLDocument可以用LoadXMLData(sXML:String)来将一个如‘<?xml version=&quot;1.0&quot;
encoding=&quot;GBK&quot;?><Req></Req>’load进来,IXMLNode变量有没有类似方法load一个已知的字符串呢,也就是,如果我想将‘<QD><Id>01</Id><Name>Book</Name></QD>’这个串赋到<Req></Req>之中去,用什么方法?因为这个子串是固定的,而且有多个不同的,所以不想用ChildNodes.Add之类函数,想直接赋到<Req></Req>,然后IXMLDocument变量可以正常使用,各位大侠,我刚接触XML,请不吝赐教!
 
Delphi中,定义一个IXMLDocument和IXMLNode变量,IXMLDocument可以用LoadXMLData(sXML:String)来将一个如‘<?xml version=&quot;1.0&quot;
encoding=&quot;GBK&quot;?><Req></Req>’load进来,IXMLNode变量有没有类似方法load一个已知的字符串呢,也就是,如果我想将‘<QD><Id>01</Id><Name>Book</Name></QD>’这个串赋到<Req></Req>之中去,用什么方法?因为这个子串是固定的,而且有多个不同的,所以不想用ChildNodes.Add之类函数,想直接赋到<Req></Req>,然后IXMLDocument变量可以正常使用,各位大侠,我刚接触XML,请不吝赐教!
 
[blue]哪位大侠知道啊!救命啊![/blue]
 
晕哦。我也遇到这个问题了。老大解决了么?
 
没人解答么?
 
似乎不能吧。。。。。你要先读出‘<QD><Id>01</Id><Name>Book</Name></QD>
然后循环取他属性写入<Req></Req>内

for i:=0 to xmlnode.AttributeNodes.Count-1do
begin
xmlnode.AttributeNodes.Get(i);
end;
 
这个问题我解决了。哈哈哈~我来我来~
如果使用临时文件,然后把需要添加进来的源node用OmniXMLUtils单元中提供的CopyNode函数实现复制到目标文档中作为一个node,我做了改进,使用一个新xmldoc的loadXML方法,直接把源node.xml加载进来,然后再使用CopyNode来实现复制过来,这样避免了创建临时文件的过程,更加爽~
对了。这里用到了OmniXML这个第三方的XML组件,可以到www.omnixml.com下载。
参考我那个帖子。
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3151114
 
晕~~~~crazycock老兄。。无语。。。
 
不要晕啊。我花了100分悬赏我那个问题,现在解决了,当然要捞点分赚回来啊。
 
呵呵~~对了 能不能帮个忙 把那个OmniXML的copynode部分的代码贴出来好吗
 
{:mad:param sourceNode Source of the copy operation.
@param targetNode Target of the copy operation. Already existing child
nodes will be cleared.
@param copySubnodes If true (default), subnodes will be copied too.
@param filterProc If assigned, filtering procedure that can prevent a
(child) node from being copied to the target node.
@since 2002-12-26
}
procedure CopyNode(sourceNode, targetNode: IXMLNode;
copySubnodes: boolean;
filterProc: TFilterXMLNodeEvent);
var
alreadyDeleted: TStringList;
attrib : IXMLNode;
do
Copy : boolean;
iAttrib : integer;
iNode : integer;
sourceChild : IXMLNode;
targetChild : IXMLNode;
targetElement : IXMLElement;
begin
if targetNode.NodeType = ELEMENT_NODE then
begin
while targetNode.Attributes.Length > 0do
targetNode.Attributes.RemoveNamedItem(targetNode.Attributes.Item[0].NodeName);
if sourceNode.NodeType = ELEMENT_NODE then
begin
targetElement := (targetNode as IXMLElement);
for iAttrib := 0 to sourceNode.Attributes.Length-1do
begin
attrib := sourceNode.Attributes.Item[iAttrib];
targetElement.SetAttribute(attrib.NodeName, (attrib as IXMLAttr).Value);
end;
end;
end;
alreadyDeleted := TStringList.Create;
try
for iNode := 0 to sourceNode.ChildNodes.Length-1do
begin
sourceChild := sourceNode.ChildNodes.Item[iNode];
if sourceChild.NodeType = TEXT_NODE then
begin
targetChild := targetNode.AppendChild(
OwnerDocument(targetNode).CreateTextNode(sourceChild.Text));
end
else
begin
do
Copy := true;
if assigned(filterProc) then
filterProc(sourceChild,do
Copy);
ifdo
Copy then
begin
if alreadyDeleted.IndexOf(sourceChild.NodeName) < 0 then
begin
DeleteAllChildren(targetNode, sourceChild.NodeName);
alreadyDeleted.Add(sourceChild.NodeName);
end;
targetChild := targetNode.AppendChild(
OwnerDocument(targetNode).CreateElement(sourceChild.NodeName));
if copySubnodes then
CopyNode(sourceChild, targetChild, copySubnodes, filterProc);
end;
end;
end;
finally FreeAndNil(alreadyDeleted);
end;
end;
{ CopyNode }
 
建议你还是下载一个OmniXML(http://www.omnixml.com),因为OmniXML所使用的XML接口声明单元是他自己定义的,和XMLIntf的有所出入。
 
呵呵~~~我不太习惯用第3方控件,特别是定义不太一样的,追加的不算。。。
除了显示效果外,我觉得其他功能都能用delphi自带的实现。。只是需要参考一下其他的方法而已 ^_^
 
xixi:) 我曾经也有一段时间有你这样的想法,不过只要是有源码的第三方控件,都是可以接受他的。毕竟Delphi自己带的东西不是最优秀的东西,第三方控件有源码的话可以自己分析,修改,如果没有源码的优秀第三方控件,一般说来不建议使用。
 
有源码确实不错。。。可以直接copy过来 呵呵~
其实说功能。。。Delphi自带的XMLDocument完全可以作到上面的copynode的功能。。
没源码我根本不用。。。用着也糊涂,万一再有点什么BUG改都没法改
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
605
import
I
顶部