有人知道怎么用CB把XML文档中的某个字段的值取出来?

alen35532119 2006-10-09 06:59:21
比如,一个XML文件
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<TransactionID xmlns="http://www.monternet.com/dsmp/schemas/">
110000000001
</TransactionID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<SyncOrderRelationReq xmlns='12345.com'>

<Version>1.5.0</Version>

</SyncOrderRelationReq>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我怎样把<Version>1.5.0</Version>中的1.5.0取出赋给变量Version?

...全文
308 4 打赏 收藏 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
alen35532119 2006-10-10
  • 打赏
  • 举报
回复
1楼的那个网址只见有DELPHI版本。
请问2楼的,正则表达式怎么写呢?举个例子吧
柯本 2006-10-10
  • 打赏
  • 举报
回复
假如你的xml为d:\temp\t.xml
#include <regexp.h>
...
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TStringList *t=new TStringList();
t->LoadFromFile("d:\\temp\\t.xml");
TRegexp *r=new TRegexp("<Version>");
unsigned int l;
int s1=r->find(t->Text.c_str(),&l,0);
delete r;
r=new TRegexp("</Version>");
int s2=r->find(t->Text.c_str(),&l,0);
if ((s1!=-1) && (s2!=-1))
{
s1=s1+strlen("<Version>")+1;
l=s2-s1+1;
String ver=t->Text.SubString(s1,l);
ShowMessage(ver);

}
delete t;
}
ver中就为1.5.0
其它值可用同样的方法(可写一个函数)
当然,BCB自带的TRegexp功能非常有限,如果你用boost库.那就更方便了
#include <boost\regex.hpp>
#include <string>
using namespace boost;
using namespace std;
...
void __fastcall TForm1::Button7Click(TObject *Sender)
{
TStringList *t=new TStringList();
t->LoadFromFile("d:\\temp\\t.xml");
regex reg("<Version>([^<]+)");
cmatch what;
if(regex_search(t->Text.c_str(),what,reg))
{
AnsiString version(what.str(1).c_str());
Edit1->Text=version;
}
delete t;
}
有关boost的使用,见boost官方网
http://www.boost.org/
而关于正则表达式的使用,网上有太多,搜一下
柯本 2006-10-09
  • 打赏
  • 举报
回复
如果只要读取有限简单的值,也可使用正则表达式
do2008 2006-10-09
  • 打赏
  • 举报
回复
1.SimDesign NativeXml v2.01 (XML操作)

一款delphi和bcb的XML控件。它帮助你在程序中方便的加入对xml文档的读写操作。
http://www.delphifans.com/SoftView/SoftView_1295.html

2.直接读取xml,示例代码
http://www.delphifans.com/SoftView/SoftView_671.html

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧