Connecting Tech Pros Worldwide Help | Site Map

Conditional For issues

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2007, 02:15 PM
Klauer
Guest
 
Posts: n/a
Default Conditional For issues

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_nametag, 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_nametag 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::Elementit = elt3-
Quote:
>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_nametag? 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_nametag 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.


  #2  
Old July 18th, 2007, 02:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Conditional For issues

Klauer wrote:
Quote:
[..] the project uses FirstChildElement("dept_name") and then
NextSiblingElement("program") for a for loop. Now since the
<dept_nametag 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::Elementit = elt3-
Quote:
>NextSiblingElement("program"); it != it.end(); it++)
^^^^^^^^^^^^^^
This doesn't look right, but I'll take your word for it
Quote:
{
.....
}
.....
[..]
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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.