473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Simple XML tag validator

Hi

The program is suppose to search tags from xml file and determine does
every tag have a pair, meaning if there is a <startthere must be
</start>, and make count how many there is that particular type. Or in
case <foo/just take notice and count.

Ok, I have managed to make the program return the tags like below, and
return the value how many tags there is:

things
basket
fruit
/fruit
bug/
/basket
foo/
/things
Now my problem is that I really don't now how to compare is there a
match, and if found how to make sure the found pair is no longer compared.

Now I have put the tags into a two-dimensional array. But I'm wondering
should I use stack or what, and how? My head is empty at the moment, so
any help would be appreciate.

bool getTag( ifstream &in, char tag[], int taglen ) ;
void cleanTag( char tag[], int taglen, int &count ) ;
void printTags( char tags[][30], int count ) ;

int main()
{
int count = 0 ;
char tag[150] ;
char tags[50][30];

ifstream in ;
in.open( "Test1.xml" ) ;

if( !in.is_open() )
cout << "Can't open the file!\n" ;
else
{
while( getTag( in, tag, 150 ) )
{
cleanTag( tag, 30, count ) ;
if( strlen( tag ) != 0 )
strcpy( tags[count-1], tag ) ;
}
}
printTags( tags, count ) ;
return 0 ;
}

void printTags( char tags[][30], int count )
{
for( int i=0; i<count; i++ )
{
for( int j=0; tags[i][j] != '\0'; j++ )
{
cout << tags[i][j] ;
}
cout << endl ;
}
}
Apr 8 '07 #1
1 2782
kapa wrote:
Hi
hi!
The program is suppose to search tags from xml file and determine does
every tag have a pair, meaning if there is a <startthere must be
</start>, and make count how many there is that particular type. Or in
case <foo/just take notice and count.

Ok, I have managed to make the program return the tags like below, and
return the value how many tags there is:

things
basket
fruit
/fruit
bug/
/basket
foo/
/things
Now my problem is that I really don't now how to compare is there a
match, and if found how to make sure the found pair is no longer compared.
If you just want to check the pairs, a simple method would be this:
* you have threee types of tags: opening (<foo>), closing (</foo>),
and alone (<foo />).
* you create a stack of tag that are opened and that are waiting for
the closing tag.
* for each tag, you read it:
- if the tag is alone, ignore it and go ahead;
- if the tag is opening, you put it in the top of the stack and go ahead;
- if the tag is closing, it has to match to the opening tag at the
top of the stack. If it does, everything is ok and you pop the top of
the stack and go ahead. Otherwise (empty stack or unmatching elements)
there is an error and the xml is badly formatted.

Example:
things
basket
fruit
/fruit
bug/
/basket
foo/
/things
step1: insert things

things <-

step2: insert basket

basket <-
things

step3: insert fruit

fruit <-
basket
things

step 4: /fruit matches with the top, remove fruit

basket <-
things

step5: /bug doesn't match with the top: there is an error

Of course, now everything is bad and you should end your analysis. If
you want to implement some intelligence and some reasoning on the error
cause, it becomes more complicated.
Now I have put the tags into a two-dimensional array. But I'm wondering
should I use stack or what, and how? My head is empty at the moment, so
any help would be appreciate.
The stack was a good intuition! Use that one defined by the stl
(#include <stack>).

Another good idea would be to define a simple class for the problem

class Tag
{
public:
enum Type {OPENING, CLOSING, ALONE};
public:
Tag(Type t, std::string name);

std::string GetName(); // returns the pure name

Type GetType(); // returns the type of the tag

bool IsMatching(const Tag& t); // returns true if t is closing and
this is opening with the same name and viceversa
};

Good work!

Bye,

Zeppe

Apr 8 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bud Curtis | last post by:
For some reason on my system I can't get the following elementry web.xml to validate. It looks like JonAS will only process an XML if it is valid. For some reason a very similar web.xml on JBoss...
3
by: Gwen Morse | last post by:
I'd like to alternate the rows in a table between having a shaded background and being transparent to the background. It's a table of people's names, phone numbers, and beeper numbers. So, by...
14
by: Akseli Mäki | last post by:
Hi, Hopefully this is not too much offtopic. I'm working on a FAQ. I want to make two versions of it, plain text and HTML. I'm looking for a tool that will make a plain text doc out of the...
8
by: Steve K | last post by:
I have a table with 61 rows and 4 columns. Names, email, phone, affiliation. I want to have a solid 1px bottom border for each row. But I am not smart enough with CSS to figure out how to do...
0
by: Tom Pearson | last post by:
I create controls and validators dynamically dependent on data at runtime. I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate. These...
24
by: chri_schiller | last post by:
I have a home-made website that provides a free 1100 page physics textbook. It is written in html and css. I recently added some chinese text, and since that day there are problems. The entry...
37
by: chandy | last post by:
Hi, I have an Html document that declares that it uses the utf-8 character set. As this document is editable via a web interface I need to make sure than high-ascii characters that may be...
2
by: SimonH | last post by:
Hi All, I'm having problems getting a custom validator to work. I'm hoping someone could perhaps notice what I've missed. I only want to provide a server side validation method. The problem is...
0
by: Neal | last post by:
Hi All, Help please Cross browser and W3C issues: Having major hassles trying to get my site working and looking decent in FFox (great in IE), MOzzie support said to try the W3C Validator so...
4
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I need to do a simple asych post back to validate that an id is unique. I do not want to post back the entire page for this, but i want to make this part of the clientside validators. 1. i...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.