Reputation: 69
I want to know that which factors make XML parser as a complete parser? I write a simple code which fetches the data from XML file, so can I called it as a parser? Or if I want to make it a complete parser then what kind of operations should I expect that should be performed by it? Here is my code:
void main()
{
clock_t start,end;
double cpu_time_used;
start=clock(); FILE *fp, *fp1;
char text[300],c,stk[5][5],tempTag[10],value[20],closeTag,flag,fClose,sStart,tagNo=1;
int i,j,n,top=-1;
fp=fopen("data1.txt","r");
fp1=fopen("cpyInput.txt","w");
while(strlen(fgets(text,300,fp))!=0)
{
closeTag=0; flag=0;
for(i=0;i<strlen(text);i++)
{
if(text[i]=='>')
{
closeTag++;
if(flag==0)
{
fClose=i;
flag=1;
}
}
if(flag==1 && text[i]=='<')
{
sStart=i;
}
}
if(closeTag==2)
{j=0;
for(i=(fClose+1);i<sStart;i++)
{
value[j]=text[i];
j++;
}
value[j]='\0';
switch(tagNo){
case 1:fputs("\nAuthor:",fp1);
fputs(value,fp1);
tagNo++;break;
case 2:fputs("\tTitle:",fp1);
fputs(value,fp1);
tagNo++;break;
case 3:fputs("\tGenre:",fp1);
fputs(value,fp1);
tagNo++;break;
case 4:fputs("\tprice:",fp1);
fputs(value,fp1);
tagNo++;break;
case 5:fputs("\tPub.Date:",fp1);
fputs(value,fp1);
tagNo++;break;
case 6:
fputs(value,fp1);
tagNo=1;
//printf("\n\n");break;
}
}
}
}
and this is my XML file:
<book>
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML</description>
</book>
Upvotes: 1
Views: 36
Reputation: 111686
A parser reads a stream of data and converts it into other, typically higher level, constructs: objects, events, procedure calls, etc. Your code does this and therefore qualifies as a parser.
Does it qualify as an XML parser? It does in the sense that is reading a stream of XML and converting it into another form.
However, there is a stricter sense to which your code falls short of being an XML parser: Commonly being an XML parser implies being a conformant XML processor, where in order to be conformant the parser has to report violations of the rules specified in the W3C XML Recommendation. Your parser does not do that and so is not a conformant XML processor; it is not an XML parser in the sense that an XML parser is commonly expected to be a conformant XML processor.
You could theoretically extend your code to be a fully conformant XML processor by taking on the official conformance tasks, but nearly no developers do that. It is a complicated endeavor and an already solved problem. Instead, they use an existing XML parsing library; you ought to too.
Upvotes: 1
Reputation: 163458
The terms "parsing" and "parser" are widely misused in the XML world. An XML parser (called an "XML processor" in the W3C specifications) is a program that reads a stream of characters or octets, typically from a file, analyzes its structure, checks for errors, and reports what it finds to an application. So the parser is looking for special characters such as angle brackets and equals signs, and effectively turns a stream of characters into a nested structure of elements and attributes. The result is that the application can then view the XML at a higher level (as elements and attributes), and it also knows that the input is well-formed.
Unfortunately a lot of people have started calling the application that is presented with the elements and attributes a "parser". This usage is quite incorrect.
The program you've presented us with is indeed a parser, in that it reads the raw input and tries to identify its structure. However, it only seems to be trying to handle a very small subset of XML, with a fixed vocabulary, and it has very little error checking, so it cannot really be called a true XML parser: rather it's a parser for some subset or dialect of XML which you haven't defined. Certainly it's not a conformant XML parser or processor in the W3C sense of the term.
You might be able to process some set of XML messages using this kind of approach, but it's not going to be at all maintainable. The whole point about XML is the "X" - eXtensible. Message formats change over time, and XML is designed to enable you to write applications that are resilient to such change. A home-grown parser like this that can only handle one particular message format is going to have to be rewritten every time the message format changes, which makes the whole thing a very bad investment.
Upvotes: 0