用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(
相关文档:
文件
文件的基本概念
所谓“文件”是指一组相关数据的有序集合。 这个数据集有一个名称,叫做文件名。 实际上在前面的各章中我们已经多次使用了文件,例如源程序文件、目标文件、可执行文件、库文件 (头文件)等。文件通常是驻留在外部介质(如磁盘等)上的, 在使用时才调入内存中来。从 ......
今天在写到用c来解析post数据的时候需要用到一个数组变量来放post的所有数据等着来解析,不想太浪费内存了。于是想着先申请一个最大威尔哦content_length大小的数组再说。但是不允许用变量来。比如
int length = atoi(getenv("CONTENT_LENGTH"));
char params[length];
memset(params, '\0', length);
那么char para ......
一、获取日历时间
time_t是定义在time.h中的一个类型,表示一个日历时间,也就是从1970年1月1日0时0分0秒到此时的秒数,原型是:
typedef long time_t; /* time value */
可以看出time_t其实是一个长整型,由于长整型能表示的数值有限,因此它能表示的最迟时间是2038年 ......
由于现在在公司负责制作标准的静态页面,为了增强客户体验,所以经常要做些AJAX效果,也学你也和我一样在,学习AJAX。而设计AJAX时使用的一个重要的技术(工具)就是XMLHTTPRequest对象了。这里海啸把我学习XMLHTTPRequest对象的一点资料拿出来跟大家一起分享。文中的资料都是海啸在学习时在网上收集的,如果您开过,那就再 ......
1.首先是获得linux内核源码,好像是废话,下载地址如下:ftp://ftp.kernel.org/pub/linux/kernel/v2.6/下载:
linux-2.6.16.22.tar.bz2 patch-2.6.22.6.bz2
上面一步需要说明的是一般而言,linux内核的各个补丁文件是根据某个linux内核的版本号来作的patch。
将上面的两个压缩文件解压:
tar jxvf linux-2.6.22.ta ......