xml的一个示例

Function.h

 

CString openfile(CString);

CString getcontext(CString,int,int);

void str2array(CString);

struct xmlnode
{
 CString name;
 CString value;
 int id;
};

Function.cpp

#include <afx.h>
#include <stdio.h>
#include <iostream.h>

#include "Function.h"

extern xmlnode node[1000];
extern count;

CString openfile(CString file)
{
 CString context;
 char ch;
 FILE *of;
 of=fopen(file,"r");
 ch=fgetc(of);
 while(ch!=EOF)
 {
  context+=ch;
  ch=fgetc(of);
 }
 return context;
}

CString getcontext(CString context,int i,int j)
{
 CString str;
 for(int k=i+1;k<j;k++)
 {
  str+=context[k];
 }
 return str;
}

void str2array(CString context)
{
 CString firstname;
 CString nextname;
 CString tempname;
 CString tempvalue;
 CStringArray stack;
 stack.Add("");
 int max=0;
 int nid=1;
 int k=0;
 int i1,i2,j1,j2,k1,k2;
 while((k<context.GetLength())&&(context[k]!='<'))
 {
  k++;
 }
 i1=k;

 while((k<context.GetLength())&&(context[k]!=' ')&&(context[k]!='>'))
 {
  k++;
 }
 k1=k;

 while((k<context.GetLength())&&(context[k]!='>'))
 {
  k++;
 }
 j1=k;

 firstname=getcontext(context,i1,k1);

 while((k<context.GetLength())&&(context[k]!='<'))
 {
  k++;
 }
 i2=k;

 while((k<context.GetLength())&&(context[k]!=' ')&&(context[k]!='>'))
 {
  k++;
 }
 k2=k;

 while((k<context.GetLength())&&(context[k]!='>'))
 {
  k++;
 }
 j2=k;

 nextname=getcontext(context,i2,k2);
 tempname='/'+firstname;

 

 while(k<context.GetLength())
 {
  if(tempname.Compare(nextname)==0)
  {
   node[count].name=firstname;
   tempvalue=getcontext(context,j1,i2);
   tempvalue.TrimLeft();
   tempvalue.TrimRight();
   node[count].value=tempvalue;
   node[count].id=nid;
   count++;
   do
   {
    while((k<context.GetLength())&&(context[k]!='<'))
    {
     k++;
    }
    i2=k;

    while((k<context.GetLength())&&(context[k]!=' ')&&(context[k]!='>'))
    {
     k++;
    }
    k2=k;

    while((k<context.GetLength())&&(context[k]!='>'))
    {
     k++;
    }
    j2=k;

    nextname=getcontext(context,i2,k2);
    if(nextname.Compare(stack.GetAt(max))==0)
    {
     stack.RemoveAt(max);
     nid--;
     max--;
    }
   }while((nextname.GetLength()>0)&&(nextname[0]=='/'));
   //same
  }
  else
  {
   tempname='/'+firstname;
   max++;
   stack.Add(tempname);
   node[count].name=firstname;
   node[count].value="";
   node[count].id=nid;
   count++;
   nid++;
   //different
  }

  firstname=nextname;
  i1=i2;
  k1=k2;
  j1=j2;

  while((k<context.GetLength())&&(context[k]!='<'))
  {
   k++;
  }
  i2=k;

  while((k<context.GetLength())&&(context[k]!=' ')&&(context[k]!='>'))
  {
   k++;
  }
  k2=k;

  while((k<context.GetLength())&&(context[k]!='>'))
  {
   k++;
  }
  j2=k;

  nextname=getcontext(context,i2,k2);
  tempname='/'+firstname;
 }
}

main.cpp

#include <iostream.h>
#include <afx.h>
#include "Function.h"

#define FILENAME "web.xml"//初始xml文件

xmlnode node[1000];
int count=0;

void main()
{
 CString context=openfile(FILENAME);
 str2array(context);

 for(int i=0;i<count;i++)
  cout<<node[i].name<<"    "<<node[i].value<<"    "<<node[i].id<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值