Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Conditional For issues

Reply
Thread Tools

Conditional For issues

 
 
Klauer
Guest
Posts: n/a
 
      07-18-2007
Hello all,

I'm kind of new to working with C++, and I have an issue in solving an
issue that maybe someone out here can help me with.

I have a piece of code in this project that I'm tasked to try to fix
some minor errors. One of these errors is dealing with XML and
loading in an XML file. The XML file is created by a couple different
applications, and one application leaves out a conditional tag:

<?xml version="1.0"?>
<catalog>
<department>
<dept_name>name of department - optional</dept_name>
<program>
<program_name>name of program</program_name>
<course_list>
<course> ... </course>
...
<course> ... </course>
</course_list>
</program>
...
<program>
...
</program>
</department>
...
<department>...</department>
</catalog>

The program that loads this XML works fine for .xml files that contain
that <dept_name> tag, but will fail horribly on files that don't
contain this.

What is the most troublesome is that this package is using TinyXML
(http://code.google.com/p/ticpp/). This wouldn't be a problem, except
that the project uses FirstChildElement("dept_name") and then
NextSiblingElement("program") for a for loop. Now since the
<dept_name> tag is optional, this seems to crash due to a lack of a
sibling in the xml at all:

ticpp::Element *elt3 = child->FirstChildElement("dept_name");
std::string dept = elt3->GetText();

for(ticpp::Iterator<ticpp::Element> it = elt3-
>NextSiblingElement("program"); it != it.end(); it++)

{
.....
}
.....

Rather than duplicate code that would be in the for loop for the first
<program>, how can I make this portion of the code more robust in
handling an optional <dept_name> tag? I guess I'm asking for help on
how to make a solution that is elegant and not simply a hack to fix a
bug.

What I am considering doing is to do copy and paste of what is
contained in the for loop for the first element. Secondarily, I was
considering taking out the contents of the for loop and putting into
another method that would be called conditionally dependent on the
presence of that <dept_name> tag or not:

if ( dept_name exists)
{
FirstChildElement "dept_name"
}
else
{
FirstChildElement "program"
callContents of ForLoop
}
for ....
{
callContents of ForLoop
}


Neither of my solutions seem elegant or anything that someone would
want to debug later.

Any suggestions?

Apologies in advance for anything lacking in context, completeness, or
whatnot. I assume at this point that what I'm giving is enough to
give a rough idea of where to go. If not, please advise and I'll give
more details, etc.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-18-2007
Klauer wrote:
> [..] the project uses FirstChildElement("dept_name") and then
> NextSiblingElement("program") for a for loop. Now since the
> <dept_name> tag is optional, this seems to crash due to a lack of a
> sibling in the xml at all:
>
> ticpp::Element *elt3 = child->FirstChildElement("dept_name");
> std::string dept = elt3->GetText();
>
> for(ticpp::Iterator<ticpp::Element> it = elt3-
>> NextSiblingElement("program"); it != it.end(); it++)

^^^^^^^^^^^^^^
This doesn't look right, but I'll take your word for it

> {
> .....
> }
> .....
> [..]


Your question has really NOTHING to do with C++ language. Please
consider posting to 'comp.programming' next time. For now, it seems
that this

ticpp::Element *elt3 = child->FirstChildElement("dept_name");
std::string dept; // default - no dept name
if (elt3) { // the child 'dept_name' is found
dept = elt3->GetText();
elt3->NextSiblingElement("program");
}
else { // 'dept_name' is NOT found
elt3 = child->FirstChildElement("program");
if (elt3)
... service the first found 'program'
}

if (elt3) {
// same for loop here...
}

should get it closer to what you want. Since I don't know what
other functionality is available, I cannot rewrite it for you to
make it less ugly.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows XP Pro clean install issues, SP2 issues too... Howie Computer Support 0 07-06-2005 07:12 PM
Re: Windows XP Pro clean install issues, SP2 issues too... pcbutts1 Computer Support 0 07-06-2005 04:58 PM
Re: Windows XP Pro clean install issues, SP2 issues too... pcbutts1 Computer Support 0 07-06-2005 04:52 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
SNMP Issues in Cisco Routers; Vulnerability Issues in TCP =?iso-8859-1?Q?Frisbee=AE?= MCSE 0 04-21-2004 03:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57