有关xml问题,在线等待 ( 积分: 100 )

  • 主题发起人 zff197982
  • 开始时间
Z

zff197982

Unregistered / Unconfirmed
GUEST, unregistred user!
本人要用delphi7 读取xml文件,但不知道如何使用,望高手给与解答。
xml文件如下:
<?xml version='1.0' encoding=&quot;gb2312&quot;
?>
<CONFIGURE>
<title>Configuration Files</title>
<SERVICE>
<APPLICATION_NAME>test</APPLICATION_NAME>
<TRANSCALL_URL>http://www.tom.com</TRANSCALL_URL>
<INFO_URL>http://baidu.com</INFO_URL>
<IVR_NUMBER>1234567</IVR_NUMBER>
<DEFAULT_URL>http://google.com</DEFAULT_URL>
</SERVICE>
<SERVICE>
<APPLICATION_NAME>BUSINESS</APPLICATION_NAME>
<TRANSCALL_URL></TRANSCALL_URL>
<INFO_URL></INFO_URL>
<IVR_NUMBER></IVR_NUMBER>
<DEFAULT_URL></DEFAULT_URL>
</SERVICE>
</CONFIGURE>
我要做的事情很简单,就是能够读取到
http://www.tom.com
http://baidu.com
1234567
http://google.com
以上这四个值,请教如何读取。
 
两种办法
1。用msxmldom来读取,用xpath查询得到节点,然后遍历该节点下的属性
2.用delphi自身的xmldom,将xml转换为树,用树的遍历方法来遍历节点。
 
能否说的具体点,本人从来没有接触过xml编程,先谢谢了
 
使用TXMLDocument类,在控件板上可以找到;
m_doc : TXMLDocument;
m_doc.LoadFromFile('C:/a.xml');
 
如楼上所说,再把TXMLDocument类的Active设为true,我在CB中使用过,delphi还没用过,不过你可以查下TXMLDocument的帮助,应该不难
它是按节点解析的,首先获取rootNode,然后获取nodelist,再挨个获取childnode,判断当前是不是叶节点,是的话可以获取text值或者attribute值
我现在没有ide没办法给你代码
 
google 搜索控制xml 多的很呢
 
delphi高手qq群:23981160
 
delphi高手qq群:23981160
 
用NativeXML
 
参考一下我的函数吧,没时间给你改得更有针对性了
function TFormSch.LoadFormSch(const Axml: TXMLDocument;
const AFileName: string): Byte;
{载入格式文件方案
第一个参数为窗体上拖拉的一个XML控件,如果临时变量实例化,有些代码运行时出错,原因未知
}
var
RootNode : IXMLNode;
SubNode : IXMLNode;
i : Integer;
Inst : TFormSchItem;
ListVerify: TStrings;
function ReadNodePrptValue(const Axml: TXMLDocument;
const ANodeName, ANodePrpt: string): Variant;
{函数的内部函数,只用于读取XML节点属性}
var
v : Integer;
begin
Result := Null;
RootNode := Axml.DocumentElement;
for v := 0 to RootNode.ChildNodes.Count - 1do
begin
SubNode := RootNode.ChildNodes;
if SubNode.NodeName = ANodeName then
begin
if SubNode.HasAttribute(ANodePrpt) then
begin
Result := SubNode.Attributes[ANodePrpt];
Exit;
end else
begin
raise Exception.CreateFmt('格式文件方案配置文件无效:区段中的参数没有定义(%0:s/%1:s)', [ANodeName, ANodePrpt]);
end;
end;
end;
raise Exception.CreateFmt('格式文件方案配置文件无效:缺少区段定义(%0:s)', [ANodeName]);
end;
begin
Result := $00;
ClearItems;
Axml.LoadFromFile(AFileName);
ListVerify := TStringList.Create;
for i := 0 to Axml.ChildNodes[0].ChildNodes.Count - 1do
begin
Inst := Add;
Inst.FName := Axml.ChildNodes[0].ChildNodes.NodeName;
Inst.FStart := ReadNodePrptValue(Axml, Inst.Name, '起始位');
Inst.FLong := ReadNodePrptValue(Axml, Inst.Name, '长度');
Inst.FVerify := ReadNodePrptValue(Axml, Inst.Name, '校验');
Inst.FInf := ReadNodePrptValue(Axml, Inst.Name, '信息');
Inst.FFill := ReadNodePrptValue(Axml, Inst.Name, '卡面');
Inst.FManualGroup := ReadNodePrptValue(Axml, Inst.Name, '编组');
Inst.FListInf.CommaText := Inst.Inf;
Inst.FListValue.CommaText := Inst.Fill;
if Inst.FListInf.Count <> Inst.FListValue.Count then
begin
raise Exception.CreateFmt('格式文件方案配置文件无效: &quot;信息&quot;属性与&quot;卡面&quot;指定的值个数不匹配'#13#10'(&quot;%0:s&quot;, &quot;%1:s&quot;)', [Inst.Inf, Inst.Fill]);
end;
end;
ListVerify.Free;
end;
 
多人接受答案了。
 

Similar threads

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