delphi做xml文件的高手帮一下忙! ( 积分: 200 )

T

tig1234

Unregistered / Unconfirmed
GUEST, unregistred user!
想用delphi中的tXMLDocument做个格式如下的xml文件
<MSG xmlns:xsi=&quot;http://www.wfc.org/2001/XMLSchema&quot;
xmlns:xsd=&quot;http://www.wfc.org/2001/XMLSchema-1&quot;
xmlns=&quot;http://www.mxx.com/2006/dcsi&quot;><META xmlns=&quot;&quot;>
用Attributes来做得到的xml文件msg下的child节点会多个xmlns=&quot;要让meta没有xmlns=&quot;&quot;如何处理
 
Q

QSmile

Unregistered / Unconfirmed
GUEST, unregistred user!
没看明白
 
T

tig1234

Unregistered / Unconfirmed
GUEST, unregistred user!
就是用delphi做xml文件格式是<MSG xmlns:xsi=&quot;http://www.wfc.org/2001/XMLSchema&quot;
xmlns:xsd=&quot;http://www.wfc.org/2001/XMLSchema-1&quot;
xmlns=&quot;http://www.mxx.com/2006/dcsi&quot;><META xmlns=&quot;&quot;>
要怎么做了只有MSG加有xmlns:xsi=&quot;http://www.wfc.org/2001/XMLSchema&quot;
xmlns:xsd=&quot;http://www.wfc.org/2001/XMLSchema-1&quot;
xmlns=&quot;http://www.mxx.com/2006/dcsi&quot;别的节点不增加任何东东
 

那年黄梅花开

Unregistered / Unconfirmed
GUEST, unregistred user!
授人以鱼不如渔
////////////////////////////////////////////////////////////////////////
创建IXMLDOMDocument对象的三种方法:
(1)[方法1] 直接创建 IXMLDOMDocument , 例(1)
uses msxml;
vardo
C:IXMLDOMDocument;
do
c := CoDOMDocument.create;
.....
do
c := nil;
A)doc.load('C:/temp.xml');
//从文件载入
B) 动态创建
var aElement,aElement2: IXMLDOMElement;
// [ aNode:IXMLDOMNode ==> .AppendChild() ]
//加入版本信息 ‘<?xml version=&quot;1.0&quot;
?> ’
do
c.AppendChild(doc.CreateProcessingInstruction('xml', 'version=&quot;1.0&quot;
encoding=&quot;GB2312&quot;'));
(*)因为此函数返回结果不包含 'encoding=&quot;GB2312&quot;' 故须保存前注意.
//加入根结点
do
c.AppendChild(doc.CreateElement('bootDocNode'));
//// == aElement
//加入子结点
aElement:=IXMLDOMElement(Doc.DocumentElement.AppendChild(Doc.CreateElement('ChileNode1')));
//设置接点属性
aElement.SetAttribute('ID', '11');
aElement.SetAttribute('Units', '元/m2');
//设置结点内容
aElement.AppendChild(Doc.CreateTextNode('结点内容'));
//子结点添加子结点
aElement2:=IXMLDOMElement(aElement.AppendChild(Doc.CreateElement('Child_ChileNode1')));
(2) [方法2] 直接创建 IXMLDocument ( 不是IXMLDOMDocument )
uses XMLIntf,XMLDoc;
var xmlDoc:IXMLDocument;
aNode:IXMLNode;
s:string;

xmlDoc := TXMLDocument.Create(nil);
try
//加入版本信息 ‘<?xml version=&quot;1.0&quot;
encoding=&quot;GB2312&quot;
?> ’
xmlDoc.Active := True;
xmlDoc.Version := '1.0';
xmlDoc.Encoding :='GB2312';
//加入根结点
aNode:=xmlDoc.AddChild('bootDocNode');
//加入子结点
aNode:=aNode.AddChild('ChileNode1');
//设置接点属性
aNode.SetAttribute('ID', '22');
aNode.SetAttribute('Units', '元/m2');
//设置结点内容
aNode.Text := '结点内容';
//子结点添加子结点
aNode:=aNode.AddChild('Child_ChileNode1') ;
aNode.Text := 'Child_ChileNod内容';

s := xmlDoc.XML.Text ;
// .XML 返回的是 Tstrings
finally
xmlDoc := nil ;
end;

(3)利用 XMLDataBinding
I) 准备好XML文件,此XML文件有较强的代表性,保证程序中所用的结点及其关系都存在
II)利用 file-->new-->XML Data Binding
III)创建XML对象
A)v:string;
//XML文件内容
do
c : IXMLBudgetDocTyp;
//IXMLBudgetDocTyp是XML文件的根结点
do
c := LoadXMLData(v).GetDocBinding('BudgetDoc', TXMLBudgetDocType) as IXMLBudgetDocType;
B)Doc := LoadBudgetDoc('C:/temp.xml');
IV) 应用
do
c.ChildNodes.FindNode('Docfile') as IXMLDocfileType;
(4)利用TXMLDocument控件
XMLDocument1.fileName:='C:/temp.xml';
XMLDocument1.active:=true;
// XMLDocument1 相当于do
c
(5)XML对象的其他方法
IXMLNodeList.FindNode(NodeName:do
MString): IXMLNode;
IXMLNodeList.FindNode(NodeName, NamespaceURI:do
MString): IXMLNode;
IXMLNodeList.FindNode(ChildNodeType: TGuid): IXMLNode;
IXMLNodeList.FindSibling(const Node: IXMLNode;
Delta: Integer): IXMLNode;
IXMLNodeList.First: IXMLNode;
IXMLNodeList.Last: IXMLNode;
... ...

////////////////////////////////////////////////////////////////////////
//例(1)
uses msxml;

do
c:IXMLDOMDocument;
budgetdoc:ixmlDomNode;
Rela:IxmlDOMNode;
rs:ixmldomnodelist;
//建立或取得XML结点
do
c := CoDOMDocument.create;
do
c.load('C:/temp.xml');
budgetDoc :=do
c.selectSingleNode('BudgetDoc');
rela := budgetdoc.SelectSingleNode('Relation');

//创建XML子结点
if not assigned(rela) then
begin
rela := do
c.createElement('Relation');
rela.setAttribute('BudgetId',0);
rela.setAttribute('name','名称');
budgetdoc.appendChild(rela);
end;

//取子结点(序列)
rs := rela.selectNodes('Rela[@BudgetId=&quot;2&quot;
and @TaskId=&quot;8&quot;]');
for i := 0 to rs.length -1do
begin
s:= s + rs.attributes.getnameditem('NewRate').nodeValue;
end;

rela:=doc.ChildNodes.FindNode('DOMNode') as IxmlDOMNode;
//移除子结点
for i := rs.length -1do
wnto 1do
rela.removeChild(rs)
//取父结点
if assigned(anode.parentNode) and (Anode.parentNode.nodeName='Task') then
result := Anode.parentNode;
//取属性
do
C.DocumentElement.Attributes['Name']
rela.Attributes['Name']
rs.Nodes.Attributes['Name']
Ajob:ixmldomnode;jobs:ixmldomNodeList;
jobs := Ajob.selectNodes('RCJ[@Attrib=&quot;'+AAttrib+'&quot;]')
 
T

tig1234

Unregistered / Unconfirmed
GUEST, unregistred user!
那年黄梅花开
问一下我用这种方法做的但是得到的xml是
<?xml version=&quot;1.0&quot;
encoding=&quot;utf-16&quot;?>
<MSG xmlns:xsi=&quot;http://www.mfc.org/2001/XMLSchema-instance&quot;
xmlns:xsd=&quot;http://www.mfc.org/2001/XMLSchema&quot;
xmlns=&quot;http://www.wcc.com/2006/dcsi&quot;><META xmlns=&quot;&quot;><SNDR>
要让<META xmlns=&quot;&quot;>改为<META>要怎么做?META是MSG的子节点


-------------------------------------------------------------------实现代码如下
XMLDoc1.Version := '1.0';
XMLDoc1.encoding:='GB2312';
aNodeG:=XMLDoc1.Node.AddChild('MSG');
aNodeG.SetAttribute('xmlns:xsi','http://www.wfc.org/2001/XMLSchema-instance');
aNodeG.setAttribute('xmlns:xsd','http://www.wfc.org/2001/XMLSchema');
aNodeG.setAttribute('xmlns','http://www.wooc.com/2006/dcsi');
aNode1:=xmldoc1.ChildNodes.FindNode('MSG');
aNode:=aNode1.AddChild('META');
 
T

tig1234

Unregistered / Unconfirmed
GUEST, unregistred user!
接受答案了.
 

Similar threads

D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部