用c读取XML文件
可以将XML文件的树(只有一个顶层节点).于是理所当然的可以用树作为XML的一种存储结构.
我将在这里用C++实现对简单的XML文件的解析.
1.选择存储结构:
树型数据结构有多种存储方式,我将用"孩子兄弟表示法",定义如下:
typedef struct CSNode
{
int subNodes;
string data;
string name;
struct CSNode *firstChild,*nextsibling,*parent;
CSNode* operator=(CSNode cnode)
{
this->data = cnode.data;
this->firstChild = cnode.firstChild;
this->name = cnode.name;
this->nextsibling = cnode.nextsibling;
this->subNodes = cnode.subNodes;
return this;
}
}CSNode,*CSTree;
2.定义一个ADT,提供操作的公共接口.取名为 xml
class xml
{
public:
xml();
xml(char *fileName);
~xml();
CSNode& CreateTree(); // 建立存储结构
bool findData(const char *nodepos); // 查找节点值
bool findData(const char *partent, const char *child,string *data); // 查找节点值
bool readfile_(); // 读取xml源文件
void allocate(); // 释放节点资源
private:
string _fileCope;
char* _filename;
CSNode *head;
};
3.具体实现
#include "stdafx.h"
#include "xmlCreate.h"
#include "wstack.h"
#include <algorithm>
using namespace std;
xml::xml()
{
}
xml::xml(char *fileName) : _filename(fileName)
{
head = new CSNode;
}
xml::~xml()
{
delete head;
}
CSNode& xml::CreateTree()
{
CSNode *p = new CSNode;
CSNode *q = new CSNode;
CSNode *s = new CSNode;
wstack<string> nameStack;
wstack<CSNode> nodeStack;
string tmpstr;
string name,tempname,rootName,headName;
int noods = 0;
bool subroot = true,next = false,poproot = false;
unsigned short int enoods = 0;
char ps;
for (size_t i = 0; i < _fileCope.size(); ++i){
ps = _fileCope[i];
if (_fileCope[i] == ' ' || _fileCope[i] == '' || _fileCope[i] == '\t' || _fileCope[i] == 0x09 || _fileCope[i] == 0x0d)continue;
if (_fileCope[i] == '<' && _fileCope[i+1] != '/') {
s = new CSNode;
s->subNodes = 0;
enoods = 0;
}
else if (_fileCope[i] == '>') {
enoods = 0;
s->name = tmpstr;
nameStack.push(
相关文档:
本文来自: http://blog.pfan.cn/xiuandfang/24935.html
标签:C C ++ string String 数据结构
C字符串处理函数的实现
C字符串处理函数的实现(Linux)
#include <stddef.h>
char * ___strtok = NULL;
char * strcpy(char * dest,const char *src)
&nb ......
1、 一些头文件的作用:
<assert.h>:ANSI C。提供断言,assert(表达式)
<glib.h>:GCC。GTK,GNOME的基础库,提供很多有用的函数,如有数据结构操作函数。使用glib只需要包含<glib.h>
<dirent.h>:GCC。文件夹操作函数。struct dirent,struct DIR,opendir(),closedir(),readdir(),readdi ......
这两天学习C++学累了,看了很多的网站论坛,突然感觉迷茫了,c/c++到底能做什么呢?现在JAVA很热,也很好找工作,而且学起来还听说很容易入门。不用学计算机基础类的知识,可C/C++就不同了,只学编程还不行,还得学什么数据结构,算法,计算机原理,操作系统,汇编语言,编程用具等等,需要好多,感觉一 ......
1、C/C++程序员请注意,不能在case语句不为空时“向下执行”。
2、值类型和引用类型之间的区别:C#的基本类型(int,char等)都是值类型,是在栈中创建的。而对象是引用类型,创建于堆中,需要使用关键字new。
3、在C#中通过实例访问静态方法或成员变量是不合法的,会生成编译器错误。但是我们可以通过声 ......