I want to write an xml parser by my own, i know to search for a string, load files into memory using vectors, but is theres a way i can use this to load and parse xml file, i were told to make a DOM or Document Object Model, what is it?? please tell what should i do, im currently loading the contents of a XML file into Vectors, and using a techninque to search for <tags>
and </tags>
please give me tips/suggestions and this is my code:
#include "stdafx.h"
#include <fstream.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <vector>
struct VFILE{
std::vector<char> FILE;
int Push(char ch){FILE.push_back(ch); return 0;}
int Size(){return FILE.size();}
};
struct ELEMENTS{
char *fname;
char *Element;
int SPos, EPos;
};
ELEMENTS InitElements(){
ELEMENTS Elements;
Elements.fname='\0';
Elements.Element='\0';
Elements.SPos=0;
Elements.EPos=0;
return Elements;
}
struct DOCUMENT
{
VFILE vfile;
ELEMENTS Element;
char *fname;
char fmem;
char *EID;
char ID[1024];
int fPosS;
int fPosE;
int LoadFile(char *Fname) {
fname=Fname;
fstream Gfile;
Gfile.open(fname, ios::in);
while (Gfile.get(fmem))
{
vfile.Push(fmem);
}
Gfile.close();
return 0;}
int GetSTag(int spos, char *tag)
{
int i=spos, p=0, tg=0;
tg=strlen(tag);
while(i<=vfile.Size())
{
if (vfile.FILE[i]==tag[p])
{
p++;
if (p>=tg)
{
goto end;
}
}
else
p=0;
i++;
}
end:
if (p==0)
{
i=0;
}
if (i==0)
{
tg=0;
}
else
tg=i+1;
return (tg);
}
int GetETag(int epos, char *tag)
{
int i=epos-1, p=0, tg=0;
tg=strlen(tag);
while(i<=vfile.Size())
{
if (vfile.FILE[i]==tag[p])
{
p++;
if (p>=tg)
{
goto end2;
}
}
else
p=0;
i++;
}
end2:
if (p==0)
{
i=0;
}
if (i==0)
{
tg=0;
}
else
{
tg=i-tg+1;
}
return (tg);
}
int CopyStr(int sp, int ep, char *string)
{
int i=sp+1, p=0, r=0;
string[0]='\0';
while(r<=vfile.Size() && i<=ep)
{
string[p]=vfile.FILE[i];
p++;
i++;
}
string[p]='\0';
if (strlen(string)>=1)
return 0;
else return 1;
}
char* GetAttributes(ELEMENTS ELMNT, char *Attri)
{
char Att[1024];
int TagP[2], Limit=0;
strcpy(Att, Attri);
strcat(Att, "=\"");
TagP[0]=GetSTag(ELMNT.SPos, Att);
Limit=GetETag(ELMNT.SPos, ">");
if (TagP[0]!=0 && TagP[0]<=Limit)
{
TagP[1]=GetETag(TagP[0]+1, "\"");
CopyStr(TagP[0]-1, TagP[1]-1, Att);
}
else
Att[0]='\0';
return (Att);
}
ELEMENTS ChildElement(ELEMENTS Elements, char *ElementName)
{
fstream file;
int StrCnt=strlen(ElementName);
char ELS[1024], ELE[1024];
strcpy(ELS, "<");
strcat(ELS, ElementName);
Elements.SPos=GetSTag(Elements.SPos, ELS);
if(Elements.SPos!=0){
strcpy(ELE, "</");
strcat(ELE, ElementName);
strcat(ELE, ">");
Elements.EPos=GetETag(Elements.SPos, ELE)+1;
}
else
{
Elements.SPos=0;
Elements.EPos=0;
}
Elements.fname=fname;
Elements.Element=ElementName;
return Elements;
}
ELEMENTS NextElement(ELEMENTS EL1, ELEMENTS Elements, char *ElementName)
{
fstream file;
int StrCnt=strlen(ElementName);
char ELS[1024], ELE[1024];
strcpy(ELS, "<");
strcat(ELS, ElementName);
Elements.SPos=GetSTag(Elements.SPos, ELS);
if(Elements.SPos!=0 && Elements.SPos<=EL1.EPos){
strcpy(ELE, "</");
strcat(ELE, ElementName);
strcat(ELE, ">");
Elements.EPos=GetETag(Elements.SPos, ELE)+1;
}
else
{
Elements.SPos=0;
Elements.EPos=0;
}
Elements.fname=fname;
Elements.Element=ElementName;
return Elements;
}
std::vector<int> RetInts(ELEMENTS Elements, XMLDoc doc)
{
int i=Elements.SPos+1, p=0, k=0;
std::vector<int> VInd;
char digitc[20];
while (i<=Elements.EPos-1)
{
if (vfile.FILE[i]!=' ' && vfile.FILE[i]!='<')
{
digitc[p]=vfile.FILE[i];
p++;
}
else
{
digitc[p]='\0';
p=0;
VInd.push_back(atoi(digitc));
k++;
}
i++;
}
return VInd;
}
};
and the main is something like this:
char fname[]="C:\\users\\rishab\\desktop\\cube.dae";
DOCUMENT doc;
ELEMENTS Elements=InitElements();
doc.LoadFile(fname);
Elements=doc.ChildElement(Elements, "collada");
Elements=doc.ChildElement(Elements, "library_geometries");
Elements=doc.ChildElement(Elements, "geometry");
char *ID=GetAttributes(Elements, "id")
Elements=doc.ChildElement(Elements, "mesh");
here im, parsing a collada file(just for an example, as most of you all have worked with this type of file) the functions works as follows:
DOCUMENT doc; // creates a document
ELEMENTS elements=InitElements() // InitElements() will initialise Elements'SPos and EPos to 0
Elements=doc.ChildElement(Elements, "collada"); // Elements will contain the SPos and EPos of <collada> to </collada>
char *ID=GetAttributs(Elements, "id"); // This will checkup the elements for having a string id= and will return charaters within the " and "(double quotes) eg. in a string <geometry id="ID1"> this will lookup for the string "id" and jump to the "" position and return ID1 to *ID;
So am i doing something wrong?? Please help me, thanks in advance!!