C++读取并比较xml文件

2025-03-20 21:24:31
推荐回答(4个)
回答1:

C++读取并比较xml文件代码如下:
#include
#include
#include
int getNameValue(char *name, char *value)
{
std::ifstream file;
std::string buf, bufFromFile, beginName, endName;

int begin, end;
if (name == NULL || value == NULL) {
std::cout << "name and value is invalid" << std::endl;
exit(1);
}
beginName.append("<");
beginName.append(name);
beginName.append(">");
endName.append(" endName.append(name);
endName.append(">");

file.open("MyIp.config");
if (!file.is_open()) {
std::cout << "error in open file ";
exit(1);
}

while (!file.eof()) {
file >> bufFromFile;
buf += bufFromFile;
}
file.close();
std::cout << buf << std::endl;
begin = buf.find(beginName);
end = buf.find(endName);
//std::cout << begin << " " << end << std::endl;
if (begin < 0 || end < 0) {
std::cout << "can't find " << name << " Item" << std::endl;
exit(1);
}
if (begin >= end) {
std::cout << "error in find " << name << " Item" << std::endl;
exit(1);
}
for (int i = 0; i < end - begin - beginName.length(); i++) {
value[i] = buf.at(begin + beginName.length() + i);
}
value[end - begin - beginName.length()] = '\0';
}
int main(int argc, char* argv[])
{
char *name = "IP";
char *value = NULL;
int port;

value = new char[100];
getNameValue(name, value);
}

MyIp.config文件是一下内容:


10.21.243.133


5000

c++读取XML文件(2)
导读: lable = false; _lable += _tfc[i]; _lable += _tfc[i+1]; i += 2; continue; } else if (_tfc[i] == '' _tfc[i+1] == '!' _tfc[i+2] == '-') { lable = true; } else if (_tfc[i] == '-' _tfc[i+1] == '-' _tfc[i+

lable = false;
_lable += _tfc[i];
_lable += _tfc[i+1];
i += 2;
continue;
}
else if (_tfc[i] == '<' && _tfc[i+1] == '!' && _tfc[i+2] == '-') {
lable = true;
}
else if (_tfc[i] == '-' && _tfc[i+1] == '-' && _tfc[i+2] == '>'){
lable = false;
_lable += _tfc[i];
_lable += _tfc[i+1];
_lable += _tfc[i+2];
i += 3;
}
if (lable) _lable += _tfc[i];
else _fileCope += _tfc[i];
}
return true;
}
bool xml::findData(const char *nodeName)
{
CSNode *p = head;
string _nodeName = nodeName;
return true;
}
bool xml::findData(const char *parent, const char *child,string *data)
{
CSNode *p = head->firstChild;
string _parent(""),_child("");
bool isfound = false;
while (p != NULL) {
if (p->name == parent) {
p = p->firstChild;
while (p != NULL) {
if (p->name == child) {
*data = p->data;
return true;
}
else p = p->nextsibling;
}
}
else p = p->nextsibling;
}
return false;
}
void xml::allocate()
{
CSNode *p = head->firstChild;
while (p != NULL) {
CSNode *q = p->firstChild;
while (q != NULL) {
CSNode *s = q;
q = q->nextsibling;
if (s->name == "RecvPort") {
cout << "addd" << endl;
}
delete s;
}
CSNode *m = p;
p = p->nextsibling;
delete m;
}
delete head;
}

回答2:

如果你不想调用库,只想手工写的话 ,那么这就是一个漫长的字符串解析的过程,可以巧秒用运用正则表达式
调用DOM树 当然可以生成树.. 如果要比较的字符少的话,直接用string.h里的 strstr()找到
然后 提取 字符到 char[n].. 这东西根本没有任何难度,只不过是想起来有点麻烦,如果动手做,也不一定麻烦到哪里去.
比如 找这个节点tomtom55
就是手动找到 对应节点名的开始和结速 然后算出起始指针和结速指针,再将其便利复制到一个固定的串中做比较..
char soucestring[]="tom";
char*start,*end;
start=strstr(soucestring,"");
end=strstr(soucestring,"
");
char resultbuffer[50];
start=start+strlen("");
int i=0;
while(start!=end)
{
resultbuffer[i]=start[0];
i++;
start++;
}
resultbuffer[i]='\0';

回答3:

使用纯C++的话是相当麻烦的 即使是使用上 boost的正则库
建议你使用下其他的第三方库吧 比如tinyxml
本身就是将xml存储成为树结构

另外 如果本身xml较为简单的话 用c++采用一些限定性非常强的办法 也是可以的

回答4:

如果你不想调用库,只想手工写的话 ,那么这就是一个漫长的字符串解析的过程,可以巧秒用运用正则表达式