Conditional For issues

K

Klauer

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 said:
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.
 
V

Victor Bazarov

Klauer said:
[..] 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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top